From 854f3e0b51f86b005dc9e094e86ec70d9825d9e1 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Mon, 3 Oct 2016 11:34:49 -0700 Subject: [PATCH] ref(helm): refactor {home,lint,serve} commands --- cmd/helm/helm.go | 22 +++++++++++----------- cmd/helm/home.go | 26 +++++++++++++------------- cmd/helm/lint.go | 46 +++++++++++++++++++++++++++------------------- cmd/helm/serve.go | 36 ++++++++++++++++++++++-------------- 4 files changed, 73 insertions(+), 57 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 4c076b068..af51494e5 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -86,31 +86,31 @@ func newRootCmd(out io.Writer) *cobra.Command { cmd.AddCommand( newCreateCmd(out), newDeleteCmd(nil, out), + newDependencyCmd(out), + newFetchCmd(out), newGetCmd(nil, out), + newHomeCmd(out), newInitCmd(out), newInspectCmd(nil, out), newInstallCmd(nil, out), + newLintCmd(out), newListCmd(nil, out), + newPackageCmd(nil, out), + newRepoCmd(out), + newRollbackCmd(nil, out), + newSearchCmd(out), + newServeCmd(out), newStatusCmd(nil, out), + newUpdateCmd(out), newUpgradeCmd(nil, out), - newRollbackCmd(nil, out), - newPackageCmd(nil, out), - newFetchCmd(out), newVerifyCmd(out), - newUpdateCmd(out), newVersionCmd(nil, out), - newRepoCmd(out), - newDependencyCmd(out), - newSearchCmd(out), ) return cmd } -// RootCommand is the top-level command for Helm. -var RootCommand = newRootCmd(os.Stdout) - func main() { - cmd := RootCommand + cmd := newRootCmd(os.Stdout) if err := cmd.Execute(); err != nil { os.Exit(1) } diff --git a/cmd/helm/home.go b/cmd/helm/home.go index 93c130d85..feaf1f77b 100644 --- a/cmd/helm/home.go +++ b/cmd/helm/home.go @@ -17,6 +17,9 @@ limitations under the License. package main import ( + "fmt" + "io" + "github.com/spf13/cobra" ) @@ -25,17 +28,14 @@ This command displays the location of HELM_HOME. This is where any helm configuration files live. ` -var homeCommand = &cobra.Command{ - Use: "home", - Short: "displays the location of HELM_HOME", - Long: longHomeHelp, - Run: home, -} - -func init() { - RootCommand.AddCommand(homeCommand) -} - -func home(cmd *cobra.Command, args []string) { - cmd.Printf(homePath() + "\n") +func newHomeCmd(out io.Writer) *cobra.Command { + cmd := &cobra.Command{ + Use: "home", + Short: "displays the location of HELM_HOME", + Long: longHomeHelp, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintf(out, homePath()+"\n") + }, + } + return cmd } diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 75a0f3082..20f1995c4 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -19,6 +19,7 @@ package main import ( "errors" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -40,30 +41,37 @@ it will emit [ERROR] messages. If it encounters issues that break with conventio or recommendation, it will emit [WARNING] messages. ` -var lintCommand = &cobra.Command{ - Use: "lint [flags] PATH", - Short: "examines a chart for possible issues", - Long: longLintHelp, - RunE: lintCmd, +type lintCmd struct { + strict bool + paths []string + out io.Writer } -var flagStrict bool - -func init() { - lintCommand.Flags().BoolVarP(&flagStrict, "strict", "", false, "fail on lint warnings") - RootCommand.AddCommand(lintCommand) +func newLintCmd(out io.Writer) *cobra.Command { + l := &lintCmd{ + paths: []string{"."}, + out: out, + } + cmd := &cobra.Command{ + Use: "lint [flags] PATH", + Short: "examines a chart for possible issues", + Long: longLintHelp, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + l.paths = args + } + return l.run() + }, + } + cmd.Flags().BoolVar(&l.strict, "strict", false, "fail on lint warnings") + return cmd } var errLintNoChart = errors.New("No chart found for linting (missing Chart.yaml)") -func lintCmd(cmd *cobra.Command, args []string) error { - paths := []string{"."} - if len(args) > 0 { - paths = args - } - +func (l *lintCmd) run() error { var lowestTolerance int - if flagStrict { + if l.strict { lowestTolerance = support.WarningSev } else { lowestTolerance = support.ErrorSev @@ -71,7 +79,7 @@ func lintCmd(cmd *cobra.Command, args []string) error { var total int var failures int - for _, path := range paths { + for _, path := range l.paths { if linter, err := lintChart(path); err != nil { fmt.Println("==> Skipping", path) fmt.Println(err) @@ -99,7 +107,7 @@ func lintCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("%s, %d chart(s) failed", msg, failures) } - fmt.Printf("%s, no failures\n", msg) + fmt.Fprintf(l.out, "%s, no failures\n", msg) return nil } diff --git a/cmd/helm/serve.go b/cmd/helm/serve.go index 3fca0bd8a..ebb75e223 100644 --- a/cmd/helm/serve.go +++ b/cmd/helm/serve.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "io" "os" "path/filepath" @@ -25,24 +26,31 @@ import ( "k8s.io/helm/pkg/repo" ) -var serveDesc = `This command starts a local chart repository server that serves charts from a local directory.` -var repoPath string +const serveDesc = `This command starts a local chart repository server that serves charts from a local directory.` -func init() { - serveCmd.Flags().StringVar(&repoPath, "repo-path", localRepoDirectory(), "The local directory path from which to serve charts.") - RootCommand.AddCommand(serveCmd) +type serveCmd struct { + repoPath string + out io.Writer } -var serveCmd = &cobra.Command{ - Use: "serve", - Short: "start a local http web server", - Long: serveDesc, - RunE: serve, +func newServeCmd(out io.Writer) *cobra.Command { + s := &serveCmd{ + out: out, + } + cmd := &cobra.Command{ + Use: "serve", + Short: "start a local http web server", + Long: serveDesc, + RunE: func(cmd *cobra.Command, args []string) error { + return s.run() + }, + } + cmd.Flags().StringVar(&s.repoPath, "repo-path", localRepoDirectory(), "The local directory path from which to serve charts.") + return cmd } -func serve(cmd *cobra.Command, args []string) error { - - repoPath, err := filepath.Abs(repoPath) +func (s *serveCmd) run() error { + repoPath, err := filepath.Abs(s.repoPath) if err != nil { return err } @@ -50,6 +58,6 @@ func serve(cmd *cobra.Command, args []string) error { return err } - repo.StartLocalRepo(repoPath) + repo.StartLocalRepo(s.repoPath) return nil }