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 #78 from remyLemeunier/rework-list-application
Browse files Browse the repository at this point in the history
Rework list application
  • Loading branch information
remyLemeunier authored Dec 15, 2017
2 parents ac93aaf + 6d02ae8 commit e5ec534
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 55 deletions.
8 changes: 3 additions & 5 deletions commands/command_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"path"

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

var typeRegistry = make(map[string]CckCommand)

func makeInstance(cfg *utils.Config, name string, service string, env string) (CckCommand, error) {
func makeInstance(cfg *utils.Config, name string, service string, env string, filePath 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)
err := fill(cckCommand, cfg, service, env, filePath)
if err != nil {
return nil, err
}
Expand All @@ -38,8 +37,7 @@ type CckCommand interface {
execute()
}

func fill(cck CckCommand, config *utils.Config, service string, env string) error {
filePath := path.Join(config.WorkPath, fmt.Sprintf("%s.yml", service))
func fill(cck CckCommand, config *utils.Config, service string, env string, filePath string) error {
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: 8 additions & 24 deletions commands/contact_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func Execute() {
log.Fatalln(fmt.Sprintf("Failed load config: %q", err))
return
}

services, err := cfg.DiscoverServices()
tree, err := cfg.DiscoverServices()
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to find services: %q", err))
return
Expand All @@ -28,29 +27,14 @@ 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())
}

rollbackEnvsCmd := addEnvironmentToCommand(rollbackCmd, cfg.GlobalEnvironments)
for _, rollbackEnvCmd := range rollbackEnvsCmd {
addServiceNameToCommand(rollbackEnvCmd, cfg, services, rollbackCmd.Name(), rollbackEnvCmd.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())
}
}

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

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,
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,
RunE: func(cmd *cobra.Command, args []string) error {
cckCommand, err := makeInstance(cfg, commandName, cmd.Name(), env)
cckCommand, err := makeInstance(cfg, commandName, cmd.Name(), env, filePath)
if err != nil {
return err
}
Expand All @@ -35,9 +34,16 @@ func addServiceNameToCommand(cmd *cobra.Command, cfg *utils.Config, services []s
return nil
},
}
cmd.AddCommand(serviceCmd)
serviceNameCommands[index] = serviceCmd

cmd.AddCommand(serviceCmd2)
}

return serviceNameCommands
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)
}
}
43 changes: 36 additions & 7 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package utils

import (
"io/ioutil"
"path/filepath"
"strings"

"path/filepath"

"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 @@ -95,6 +101,14 @@ 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 @@ -111,27 +125,42 @@ func LoadConfig() (*Config, error) {
return cfg, nil
}

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

files, err := ioutil.ReadDir(c.WorkPath)
return mainTree, nil
}

func runThroughDir(serviceTree *ServiceTree, path string) error {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
return 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)
services = append(services, baseNameWithoutExt)
serviceTree.Service[baseNameWithoutExt] = path + "/" + file.Name()
}
}

return services, nil
return nil
}

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

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

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

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

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[2] != "manifest" {
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)
}
}

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit e5ec534

Please sign in to comment.