From 542baa7ecffeecf290f462f75c99d52dd2009ccc Mon Sep 17 00:00:00 2001 From: Milos Pejanovic Date: Tue, 31 Mar 2020 01:02:14 +0200 Subject: [PATCH] Add init command and apply small fixes --- cmd/actions/build.go | 2 +- cmd/actions/init.go | 28 +++++++++++++++++++ cmd/actions/install.go | 7 +---- cmd/actions/root.go | 12 +++++++- cmd/actions/uninstall.go | 2 +- configurd.go | 26 +++++++++++++++-- example/configurd/charts/example/values.yaml | 5 ++-- .../namespaces/local/hello-group.yml | 18 ++++++++++++ 8 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 cmd/actions/init.go diff --git a/cmd/actions/build.go b/cmd/actions/build.go index c970417..27bc04a 100644 --- a/cmd/actions/build.go +++ b/cmd/actions/build.go @@ -25,7 +25,7 @@ func Build(service, tag, dir string, flagVerbose bool) (string, error) { } output, err := svc.RunBuild(log, dir, tag, flagVerbose) if err != nil { - return "", fmt.Errorf("could not build service: %v output:\n%v", svc.Name, output) + return "", outputErrorf(output, err, "could not build service: %v", svc.Name) } return output, nil } diff --git a/cmd/actions/init.go b/cmd/actions/init.go new file mode 100644 index 0000000..bc334a4 --- /dev/null +++ b/cmd/actions/init.go @@ -0,0 +1,28 @@ +package actions + +import ( + "path" + + "github.com/foomo/configurd" + "github.com/spf13/cobra" +) + +var initCmd = &cobra.Command{ + Use: "init [NAME]", + Short: "Initialize an example application with configurd", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + _, err := Init(args[0], flagDir, flagVerbose) + if err != nil { + log.Fatal(err) + } + }, +} + +func Init(name, dir string, flagVerbose bool) (string, error) { + output, err := configurd.Init(log, path.Join(dir, name), flagVerbose) + if err != nil { + return "", outputErrorf(output, err, "could not initialize an example configuraiton") + } + return output, nil +} diff --git a/cmd/actions/install.go b/cmd/actions/install.go index 7395da7..13dfc06 100644 --- a/cmd/actions/install.go +++ b/cmd/actions/install.go @@ -48,12 +48,7 @@ func install(group, namespace, tag, workDir, outputDir string, build, verbose bo } output, err := cnf.Install(log, sis, workDir, outputDir, tag, verbose) if err != nil { - // return "", fmt.Errorf("could not install group: %v output:\n%v \nerror: \n%v", group, output, err) - return "", errorf(output, err, "could not install group: %v", group) + return "", outputErrorf(output, err, "could not install group: %v", group) } return output, nil } - -func errorf(output string, err error, format string, args ...interface{}) error { - return fmt.Errorf("%v, error: %v", fmt.Sprintf(format, args...), err) -} diff --git a/cmd/actions/root.go b/cmd/actions/root.go index eb81637..5bdd1cb 100644 --- a/cmd/actions/root.go +++ b/cmd/actions/root.go @@ -1,6 +1,7 @@ package actions import ( + "fmt" "os" "path" @@ -21,6 +22,11 @@ var ( } if flagDir != "" { flagDir = path.Join(wdir, flagDir) + } else { + flagDir = wdir + } + if cmd.Name() == "help" || cmd.Name() == "init" { + return } cnf, err = configurd.New(log, flagDir) if err != nil { @@ -39,9 +45,13 @@ func init() { rootCmd.PersistentFlags().StringVarP(&flagTag, "tag", "t", "latest", "Specifies the image tag") rootCmd.PersistentFlags().StringVarP(&flagDir, "dir", "d", "", "Specifies working directory") rootCmd.PersistentFlags().BoolVarP(&flagVerbose, "verbose", "v", false, "Specifies should command output be displayed") - rootCmd.AddCommand(buildCmd, installCmd, uninstallCmd) + rootCmd.AddCommand(buildCmd, installCmd, uninstallCmd, initCmd) } func Execute() { rootCmd.Execute() } + +func outputErrorf(output string, err error, format string, args ...interface{}) error { + return fmt.Errorf("%v, error: %v", fmt.Sprintf(format, args...), err) +} diff --git a/cmd/actions/uninstall.go b/cmd/actions/uninstall.go index 3d5da05..36f08cf 100644 --- a/cmd/actions/uninstall.go +++ b/cmd/actions/uninstall.go @@ -32,7 +32,7 @@ func uninstall(group, namespace string, flagVerbose bool) (string, error) { } output, err := cnf.Uninstall(log, sis, flagVerbose) if err != nil { - return "", fmt.Errorf("could not uninstall service group: %v output:\n%v", group, output) + return "", outputErrorf(output, err, "could not uninstall service group: %v", group) } return output, nil } diff --git a/configurd.go b/configurd.go index 37c8aea..0659623 100644 --- a/configurd.go +++ b/configurd.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "os/exec" "path" "path/filepath" "strings" @@ -19,6 +20,7 @@ const ( defaultChartDir = "configurd/charts" defaultOutputDir = "configurd/.workdir" defaultOverridesFile = "overrides.yaml" + defaultInitUrl = "https://github.com/foomo/configurd.git/branches/feature/helm-charts-deployments/example" ) var ( @@ -49,8 +51,8 @@ type Logger interface { Fatalf(format string, args ...interface{}) } -func relativePath(absoltePath, basePath string) string { - return strings.Replace(absoltePath, basePath+"/", "", -1) +func relativePath(path, basePath string) string { + return strings.Replace(path, basePath+"/", "", -1) } func New(log Logger, basePath string) (Configurd, error) { @@ -185,7 +187,7 @@ func (c Configurd) Service(name string) (Service, error) { func (c Configurd) GetServiceItems(namespace, group string) []ServiceItem { var sis []ServiceItem for _, si := range c.serviceItems() { - if si.namespace == namespace && (si.group == group || group == "") { + if si.namespace == namespace && si.group == group { sis = append(sis, si) } } @@ -220,3 +222,21 @@ func logOutput(log Logger, verbose bool, format string, args ...interface{}) { log.Printf(format, args...) } } + +func Init(log Logger, dir string, flagVerbose bool) (string, error) { + // log.Printf("Creating dir: %q", dir) + + // if err := os.MkdirAll(dir, 0744); err != nil { + // return "", fmt.Errorf("could not create a directory: %w", err) + // } + + log.Printf("Downloading example configuration into dir: %q", dir) + cmd := exec.Command("svn", "export", defaultInitUrl, dir) + + out, err := cmd.CombinedOutput() + output := strings.Replace(string(out), "\n", "\n\t", -1) + if err != nil { + return "", fmt.Errorf("could not download a configurd example, output: \n%v", output) + } + return output, nil +} diff --git a/example/configurd/charts/example/values.yaml b/example/configurd/charts/example/values.yaml index 7eed295..762bae2 100644 --- a/example/configurd/charts/example/values.yaml +++ b/example/configurd/charts/example/values.yaml @@ -20,11 +20,12 @@ service: env: [] ingress: - enabled: true + enabled: false annotations: {} # kubernetes.io/ingress.class: nginx # kubernetes.io/tls-acme: "true" - paths: [] + paths: + - "/" hosts: - chart-example.local tls: [] diff --git a/example/configurd/namespaces/local/hello-group.yml b/example/configurd/namespaces/local/hello-group.yml index 4fb0e0c..e84a901 100644 --- a/example/configurd/namespaces/local/hello-group.yml +++ b/example/configurd/namespaces/local/hello-group.yml @@ -4,8 +4,26 @@ group: overrides: args: - "--greeting=hehe" + service: + targetPort: 80 + port: 80 + ingress: + enabled: true + paths: + - /services/hello + hosts: + - localhost hi-service: overrides: args: - "--greeting=hai" + service: + targetPort: 80 + port: 80 + ingress: + enabled: true + paths: + - /services/hi + hosts: + - localhost jobs: \ No newline at end of file