Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from remyLemeunier/revert-list2
Browse files Browse the repository at this point in the history
Revert "Merge pull request #78 from remyLemeunier/rework-list-applica…
  • Loading branch information
remyLemeunier authored Dec 18, 2017
2 parents e5ec534 + 1218b22 commit 970bbf4
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 152 deletions.
8 changes: 5 additions & 3 deletions commands/command_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"path"

"github.com/remyLemeunier/contactkey/context"
"github.com/remyLemeunier/contactkey/utils"
Expand All @@ -10,14 +11,14 @@ import (

var typeRegistry = make(map[string]CckCommand)

func makeInstance(cfg *utils.Config, name string, service string, env string, filePath string) (CckCommand, error) {
func makeInstance(cfg *utils.Config, name string, service string, env string) (CckCommand, error) {
if _, ok := typeRegistry[name]; !ok {
return nil, fmt.Errorf("Struct not found %s", name)

}

cckCommand := typeRegistry[name]
err := fill(cckCommand, cfg, service, env, filePath)
err := fill(cckCommand, cfg, service, env)
if err != nil {
return nil, err
}
Expand All @@ -37,7 +38,8 @@ type CckCommand interface {
execute()
}

func fill(cck CckCommand, config *utils.Config, service string, env string, filePath string) error {
func fill(cck CckCommand, config *utils.Config, service string, env string) error {
filePath := path.Join(config.WorkPath, fmt.Sprintf("%s.yml", service))
manifestFile, err := utils.ReadFile(filePath)
if err != nil {
return fmt.Errorf("Unable to read file: %q with err: %q", filePath, err)
Expand Down
32 changes: 24 additions & 8 deletions commands/contact_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func Execute() {
log.Fatalln(fmt.Sprintf("Failed load config: %q", err))
return
}
tree, err := cfg.DiscoverServices()

services, err := cfg.DiscoverServices()
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to find services: %q", err))
return
Expand All @@ -27,14 +28,29 @@ func Execute() {
}

rootCmd.PersistentFlags().BoolVarP(&cfg.Verbose, "verbose", "v", false, "verbose output")
rootCmd.AddCommand(deployCmd)
rootCmd.AddCommand(diffCmd)
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(rollbackCmd)

deployEnvsCmd := addEnvironmentToCommand(deployCmd, cfg.GlobalEnvironments)
for _, deployEnvCmd := range deployEnvsCmd {
addServiceNameToCommand(deployEnvCmd, cfg, services, deployCmd.Name(), deployEnvCmd.Name())
}

diffEnvsCmd := addEnvironmentToCommand(diffCmd, cfg.GlobalEnvironments)
for _, diffEnvCmd := range diffEnvsCmd {
addServiceNameToCommand(diffEnvCmd, cfg, services, diffCmd.Name(), diffEnvCmd.Name())
}

listEnvsCmd := addEnvironmentToCommand(listCmd, cfg.GlobalEnvironments)
for _, listEnvCmd := range listEnvsCmd {
addServiceNameToCommand(listEnvCmd, cfg, services, listCmd.Name(), listEnvCmd.Name())
}

verbsCommands := []*cobra.Command{deployCmd, diffCmd, listCmd, rollbackCmd}
for _, command := range verbsCommands {
rootCmd.AddCommand(command)
envsCmd := addEnvironmentToCommand(command, cfg.GlobalEnvironments)
for _, envCmd := range envsCmd {
addServiceNameToCommand(tree, envCmd, cfg, command.Name(), envCmd.Name())
}
rollbackEnvsCmd := addEnvironmentToCommand(rollbackCmd, cfg.GlobalEnvironments)
for _, rollbackEnvCmd := range rollbackEnvsCmd {
addServiceNameToCommand(rollbackEnvCmd, cfg, services, rollbackCmd.Name(), rollbackEnvCmd.Name())
}

err = rootCmd.Execute()
Expand Down
26 changes: 10 additions & 16 deletions commands/env_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ func addEnvironmentToCommand(cmd *cobra.Command, envs []string) map[int]*cobra.C
return envCommands
}

func addServiceNameToCommand(serviceTree utils.ServiceTree, cmd *cobra.Command, cfg *utils.Config, commandName string, env string) {
for serviceName, filePath := range serviceTree.Service {
serviceCmd2 := &cobra.Command{
Use: serviceName,
Short: "Run command for " + serviceName,
func addServiceNameToCommand(cmd *cobra.Command, cfg *utils.Config, services []string, commandName string, env string) map[int]*cobra.Command {
serviceNameCommands := make(map[int]*cobra.Command)
for index, service := range services {
serviceCmd := &cobra.Command{
Use: service,
Short: "Run command for " + service,
RunE: func(cmd *cobra.Command, args []string) error {
cckCommand, err := makeInstance(cfg, commandName, cmd.Name(), env, filePath)
cckCommand, err := makeInstance(cfg, commandName, cmd.Name(), env)
if err != nil {
return err
}
Expand All @@ -34,16 +35,9 @@ func addServiceNameToCommand(serviceTree utils.ServiceTree, cmd *cobra.Command,
return nil
},
}

cmd.AddCommand(serviceCmd2)
cmd.AddCommand(serviceCmd)
serviceNameCommands[index] = serviceCmd
}

for name, child := range serviceTree.Child {
serviceCmd1 := &cobra.Command{
Use: name,
Short: "Run command for " + name,
}
cmd.AddCommand(serviceCmd1)
addServiceNameToCommand(child, serviceCmd1, cfg, commandName, env)
}
return serviceNameCommands
}
43 changes: 7 additions & 36 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package utils

import (
"io/ioutil"
"strings"

"path/filepath"
"strings"

"github.com/spf13/viper"
)

type ServiceTree struct {
Child map[string]ServiceTree
Service map[string]string
}

type Config struct {
WorkPath string `mapstructure:"workPath"`
Verbose bool
Expand Down Expand Up @@ -101,14 +95,6 @@ type PrometheusConfig struct {
Url string `mapstructure:"url"`
}

func newServiceTree() ServiceTree {
st := ServiceTree{}
st.Child = make(map[string]ServiceTree)
st.Service = make(map[string]string)

return st
}

func LoadConfig() (*Config, error) {
cfg := &Config{}
viper.SetConfigType("yaml")
Expand All @@ -125,42 +111,27 @@ func LoadConfig() (*Config, error) {
return cfg, nil
}

func (c Config) DiscoverServices() (ServiceTree, error) {
mainTree := newServiceTree()
err := runThroughDir(&mainTree, c.WorkPath)
if err != nil {
return mainTree, err
}
func (c Config) DiscoverServices() ([]string, error) {
services := make([]string, 0)

return mainTree, nil
}

func runThroughDir(serviceTree *ServiceTree, path string) error {
files, err := ioutil.ReadDir(path)
files, err := ioutil.ReadDir(c.WorkPath)
if err != nil {
return err
return nil, err
}

for _, file := range files {
if file.IsDir() == true {
st := newServiceTree()
err := runThroughDir(&st, path+"/"+file.Name())
if err != nil {
return err
}

serviceTree.Child[file.Name()] = st
continue
}

ext := filepath.Ext(file.Name())
if ext == ".yaml" || ext == ".yml" {
baseNameWithoutExt := strings.TrimSuffix(filepath.Base(file.Name()), ext)
serviceTree.Service[baseNameWithoutExt] = path + "/" + file.Name()
services = append(services, baseNameWithoutExt)
}
}

return nil
return services, nil
}

func ReadFile(path string) ([]byte, error) {
Expand Down
98 changes: 9 additions & 89 deletions utils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,104 +79,24 @@ func TestLoadConfig(t *testing.T) {

func TestDiscoverServices(t *testing.T) {
c := Config{WorkPath: "./testdata"}
serviceTree, err := c.DiscoverServices()
services, err := c.DiscoverServices()
if err != nil {
t.Fatalf("DiscoverServices failed with err %q", err)
}

if len(serviceTree.Service) != 3 {
t.Errorf("ServiceTree length should be 3 instead got %d", len(serviceTree.Service))
if len(services) != 3 {
t.Errorf("Services'lenght should be 3 instead got %d", len(services))
}

if _, ok := serviceTree.Service["config"]; !ok {
t.Error("Key 'config' not found.")
if services[0] != "config" {
t.Errorf("Should be 'config' instead got: %q", services[0])
}

if _, ok := serviceTree.Service["manifest-k8s"]; !ok {
t.Error("Key 'manifest-k8s' not found.")
}

if _, ok := serviceTree.Service["manifest"]; !ok {
t.Error("Key 'manifest' not found.")
}

if len(serviceTree.Child) != 2 {
t.Fatalf("ServiceTree child length should be 2 instead got %d", len(serviceTree.Child))
}

dirOne, ok := serviceTree.Child["dirOne"]
if !ok {
t.Fatal("Key 'dirOne' not found in Child.")
}

dirTwo, ok := serviceTree.Child["dirTwo"]
if !ok {
t.Fatal("Key 'dirTwo' not found in Child.")
}

if len(dirOne.Service) != 2 {
t.Fatalf("dirOne should have 2 services, found %s", len(dirOne.Service))
}

if len(dirOne.Child) != 1 {
t.Fatalf("dirOne should have 1 child, found %s", len(dirOne.Child))
}

if len(dirTwo.Service) != 1 {
t.Fatalf("dirTwo should have 1 service, found %s", len(dirTwo.Service))
}

if len(dirTwo.Child) != 0 {
t.Fatalf("dirTwo have no child, found %s", len(dirTwo.Child))
}

confa, ok := dirOne.Service["confa"]
if !ok {
t.Fatal("Key 'confa' not found in dirOne's service")
}

if confa != "./testdata/dirOne/confa.yaml" {
t.Errorf("confa's path should be './testdata/dirOne/confa.yaml' instead found %s", confa)
}

confb, ok := dirOne.Service["confb"]
if !ok {
t.Fatal("Key 'confb' not found in dirOne's service")
}

if confb != "./testdata/dirOne/confb.yaml" {
t.Errorf("confb's path should be './testdata/dirOne/confa.yaml' instead found %s", confb)
}

confd, ok := dirTwo.Service["confd"]
if !ok {
t.Fatal("Key 'confd' not found in dirTwo's service")
}

if confd != "./testdata/dirTwo/confd.yaml" {
t.Errorf("confd's path should be './testdata/dirTwo/confd.yaml' instead found %s", confd)
}

subDirOne, ok := dirOne.Child["subDirOne"]
if !ok {
t.Fatal("subDirOne not found in dirOne child.")
}

if len(subDirOne.Child) != 0 {
t.Fatalf("subDirOne have no child, found %s", len(subDirOne.Child))
}

if len(subDirOne.Service) != 1 {
t.Fatalf("subDirOne should have 1 service, found %s", len(subDirOne.Service))
}

confc, ok := subDirOne.Service["confc"]
if !ok {
t.Fatal("Key 'confc' not found in subDirOne's service")
if services[1] != "manifest-k8s" {
t.Errorf("Should be 'manifest' instead got: %q", services[1])
}

if confc != "./testdata/dirOne/subDirOne/confc.yaml" {
t.Errorf("confc's path should be './testdata/dirOne/subDirOne/confc.yaml' instead found %s", confc)
if services[2] != "manifest" {
t.Errorf("Should be 'manifest' instead got: %q", services[1])
}
}

Expand Down
Empty file removed utils/testdata/dirOne/confa.yaml
Empty file.
Empty file removed utils/testdata/dirOne/confb.yaml
Empty file.
Empty file.
Empty file removed utils/testdata/dirTwo/confd.yaml
Empty file.

0 comments on commit 970bbf4

Please sign in to comment.