From 1218b22b6a4d83c7572d3639b65f7cdbe5c0db44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Lemeunier?= Date: Mon, 18 Dec 2017 11:17:34 +0100 Subject: [PATCH] Revert "Merge pull request #78 from remyLemeunier/rework-list-application" This reverts commit e5ec5347df770a967e2aa2066a5c408541b2d31b, reversing changes made to ac93aaf663fb0537ee2e8c09f0f11be9dee054dd. --- commands/command_registry.go | 8 +- commands/contact_key.go | 32 +++++-- commands/env_service.go | 26 +++--- utils/config.go | 43 ++-------- utils/config_test.go | 98 ++-------------------- utils/testdata/dirOne/confa.yaml | 0 utils/testdata/dirOne/confb.yaml | 0 utils/testdata/dirOne/subDirOne/confc.yaml | 0 utils/testdata/dirTwo/confd.yaml | 0 9 files changed, 55 insertions(+), 152 deletions(-) delete mode 100644 utils/testdata/dirOne/confa.yaml delete mode 100644 utils/testdata/dirOne/confb.yaml delete mode 100644 utils/testdata/dirOne/subDirOne/confc.yaml delete mode 100644 utils/testdata/dirTwo/confd.yaml diff --git a/commands/command_registry.go b/commands/command_registry.go index 335bc23..6cb6c27 100644 --- a/commands/command_registry.go +++ b/commands/command_registry.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "path" "github.com/remyLemeunier/contactkey/context" "github.com/remyLemeunier/contactkey/utils" @@ -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 } @@ -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) diff --git a/commands/contact_key.go b/commands/contact_key.go index 3d2291f..d49b079 100644 --- a/commands/contact_key.go +++ b/commands/contact_key.go @@ -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 @@ -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() diff --git a/commands/env_service.go b/commands/env_service.go index 258ac9c..775c780 100644 --- a/commands/env_service.go +++ b/commands/env_service.go @@ -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 } @@ -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 } diff --git a/utils/config.go b/utils/config.go index cde083f..672135e 100644 --- a/utils/config.go +++ b/utils/config.go @@ -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 @@ -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") @@ -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) { diff --git a/utils/config_test.go b/utils/config_test.go index f109c94..3ddb5c6 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -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]) } } diff --git a/utils/testdata/dirOne/confa.yaml b/utils/testdata/dirOne/confa.yaml deleted file mode 100644 index e69de29..0000000 diff --git a/utils/testdata/dirOne/confb.yaml b/utils/testdata/dirOne/confb.yaml deleted file mode 100644 index e69de29..0000000 diff --git a/utils/testdata/dirOne/subDirOne/confc.yaml b/utils/testdata/dirOne/subDirOne/confc.yaml deleted file mode 100644 index e69de29..0000000 diff --git a/utils/testdata/dirTwo/confd.yaml b/utils/testdata/dirTwo/confd.yaml deleted file mode 100644 index e69de29..0000000