From 39ba728b6fec8fafa23ff27bca073aab363ab613 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Mon, 1 Aug 2016 13:00:02 -0700 Subject: [PATCH] ref(cmd): refactor init command --- cmd/helm/helm.go | 7 +++--- cmd/helm/init.go | 63 +++++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index c47a13e6a..ad1049267 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -84,12 +84,13 @@ func newRootCmd(out io.Writer) *cobra.Command { cmd.AddCommand( newCreateCmd(out), + newDeleteCmd(nil, out), newGetCmd(nil, out), + newInitCmd(out), + newInspectCmd(nil, out), + newInstallCmd(nil, out), newListCmd(nil, out), newStatusCmd(nil, out), - newInstallCmd(nil, out), - newDeleteCmd(nil, out), - newInspectCmd(nil, out), newUpgradeCmd(nil, out), ) return cmd diff --git a/cmd/helm/init.go b/cmd/helm/init.go index 565022a89..218527295 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -19,6 +19,7 @@ package main import ( "errors" "fmt" + "io" "os" "github.com/spf13/cobra" @@ -32,58 +33,54 @@ Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.he ` var ( - tillerImg string - clientOnly bool defaultRepository = "kubernetes-charts" defaultRepositoryURL = "http://storage.googleapis.com/kubernetes-charts" ) -func init() { - f := initCmd.Flags() - f.StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image") - f.BoolVarP(&clientOnly, "client-only", "c", false, "If set does not install tiller") - RootCommand.AddCommand(initCmd) +type initCmd struct { + image string + clientOnly bool + out io.Writer } -var initCmd = &cobra.Command{ - Use: "init", - Short: "initialize Helm on both client and server", - Long: initDesc, - RunE: runInit, +func newInitCmd(out io.Writer) *cobra.Command { + i := &initCmd{ + out: out, + } + cmd := &cobra.Command{ + Use: "init", + Short: "initialize Helm on both client and server", + Long: initDesc, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) != 0 { + return errors.New("This command does not accept arguments") + } + return i.run() + }, + } + cmd.Flags().StringVarP(&i.image, "tiller-image", "i", "", "override tiller image") + cmd.Flags().BoolVarP(&i.clientOnly, "client-only", "c", false, "If set does not install tiller") + return cmd } // runInit initializes local config and installs tiller to Kubernetes Cluster -func runInit(cmd *cobra.Command, args []string) error { - if len(args) != 0 { - return errors.New("This command does not accept arguments. \n") - } - +func (i *initCmd) run() error { if err := ensureHome(); err != nil { return err } - if !clientOnly { - if err := installTiller(); err != nil { - return err + if !i.clientOnly { + if err := client.Install(tillerNamespace, i.image, flagDebug); err != nil { + return fmt.Errorf("error installing: %s", err) } + fmt.Fprintln(i.out, "\nTiller (the helm server side component) has been installed into your Kubernetes Cluster.") } else { - fmt.Println("Not installing tiller due to 'client-only' flag having been set") + fmt.Fprintln(i.out, "Not installing tiller due to 'client-only' flag having been set") } - - fmt.Println("Happy Helming!") - return nil -} - -func installTiller() error { - if err := client.Install(tillerNamespace, tillerImg, flagDebug); err != nil { - return fmt.Errorf("error installing: %s", err) - } - fmt.Println("\nTiller (the helm server side component) has been installed into your Kubernetes Cluster.") - + fmt.Fprintln(i.out, "Happy Helming!") return nil } -// requireHome checks to see if $HELM_HOME exists, and returns an error if it does not. func requireHome() error { dirs := []string{homePath(), repositoryDirectory(), cacheDirectory(), localRepoDirectory()} for _, d := range dirs {