From 4dd95addd24fbcf02442c3d4590050b8aef09440 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Thu, 14 Jul 2016 11:30:11 -0700 Subject: [PATCH] ref(cmd): refactor create cmd --- cmd/helm/create.go | 42 +++++++++++++++++++++++++++--------------- cmd/helm/helm.go | 8 +++++--- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/cmd/helm/create.go b/cmd/helm/create.go index 74fa3f6ed..9f50be5f7 100644 --- a/cmd/helm/create.go +++ b/cmd/helm/create.go @@ -18,9 +18,12 @@ package main import ( "errors" + "fmt" + "io" "path/filepath" "github.com/spf13/cobra" + "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/proto/hapi/chart" ) @@ -50,31 +53,40 @@ destination exists and there are files in that directory, conflicting files will be overwritten, but other files will be left alone. ` -func init() { - RootCommand.AddCommand(createCmd) +type createCmd struct { + name string + out io.Writer } -var createCmd = &cobra.Command{ - Use: "create NAME", - Short: "create a new chart with the given name", - Long: createDesc, - RunE: runCreate, +func newCreateCmd(out io.Writer) *cobra.Command { + cc := &createCmd{ + out: out, + } + cmd := &cobra.Command{ + Use: "create NAME", + Short: "create a new chart with the given name", + Long: createDesc, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("the name of the new chart is required") + } + cc.name = args[0] + return cc.run() + }, + } + return cmd } -func runCreate(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return errors.New("the name of the new chart is required") - } - cname := args[0] - cmd.Printf("Creating %s\n", cname) +func (c *createCmd) run() error { + fmt.Fprintf(c.out, "Creating %s\n", c.name) - chartname := filepath.Base(cname) + chartname := filepath.Base(c.name) cfile := &chart.Metadata{ Name: chartname, Description: "A Helm chart for Kubernetes", Version: "0.1.0", } - _, err := chartutil.Create(cfile, filepath.Dir(cname)) + _, err := chartutil.Create(cfile, filepath.Dir(c.name)) return err } diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 00cd3937e..bbc271456 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -85,9 +85,11 @@ func newRootCmd(out io.Writer) *cobra.Command { p.StringVarP(&tillerNamespace, "namespace", "", "", "kubernetes namespace") p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output") - cmd.AddCommand(newListCmd(nil, out)) - cmd.AddCommand(newGetCmd(nil, out)) - + cmd.AddCommand( + newCreateCmd(out), + newGetCmd(nil, out), + newListCmd(nil, out), + ) return cmd }