Commit 5e3044a6 authored by Adam Reese's avatar Adam Reese

ref(cmd): refactor get command

parent e339cc7e
...@@ -19,6 +19,7 @@ package main ...@@ -19,6 +19,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -53,48 +54,88 @@ were generated from this release's chart(s). If a chart is dependent on other ...@@ -53,48 +54,88 @@ were generated from this release's chart(s). If a chart is dependent on other
charts, those resources will also be included in the manifest. charts, those resources will also be included in the manifest.
` `
var allValues = false
var errReleaseRequired = errors.New("release name is required") var errReleaseRequired = errors.New("release name is required")
var getCommand = &cobra.Command{ type getCmd struct {
Use: "get [flags] RELEASE_NAME", release string
Short: "download a named release", out io.Writer
Long: getHelp, client helm.Interface
RunE: getCmd,
PersistentPreRunE: setupConnection,
} }
var getValuesCommand = &cobra.Command{ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
Use: "values [flags] RELEASE_NAME", get := &getCmd{
Short: "download the values file for a named release", out: out,
Long: getValuesHelp, client: client,
RunE: getValues, }
cmd := &cobra.Command{
Use: "get [flags] RELEASE_NAME",
Short: "download a named release",
Long: getHelp,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
}
get.release = args[0]
return get.run()
},
PersistentPreRunE: setupConnection,
}
cmd.AddCommand(newGetValuesCmd(client, out))
cmd.AddCommand(newGetManifestCmd(client, out))
return cmd
} }
var getManifestCommand = &cobra.Command{ type getValuesCmd struct {
Use: "manifest [flags] RELEASE_NAME", allValues bool
Short: "download the manifest for a named release", getCmd
Long: getManifestHelp,
RunE: getManifest,
} }
func init() { func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
// 'get values' flags. get := &getValuesCmd{}
getValuesCommand.PersistentFlags().BoolVarP(&allValues, "all", "a", false, "dump all (computed) values") get.out = out
get.client = client
cmd := &cobra.Command{
Use: "values [flags] RELEASE_NAME",
Short: "download the values file for a named release",
Long: getValuesHelp,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
}
get.release = args[0]
return get.run()
},
}
cmd.Flags().BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values")
return cmd
}
getCommand.AddCommand(getValuesCommand) type getManifestCmd struct {
getCommand.AddCommand(getManifestCommand) getCmd
RootCommand.AddCommand(getCommand)
} }
// getCmd is the command that implements 'helm get' func newGetManifestCmd(client helm.Interface, out io.Writer) *cobra.Command {
func getCmd(cmd *cobra.Command, args []string) error { get := &getManifestCmd{}
if len(args) == 0 { get.out = out
return errReleaseRequired get.client = client
cmd := &cobra.Command{
Use: "manifest [flags] RELEASE_NAME",
Short: "download the manifest for a named release",
Long: getManifestHelp,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
}
get.release = args[0]
return get.run()
},
} }
return cmd
}
res, err := helm.GetReleaseContent(args[0]) // getCmd is the command that implements 'helm get'
func (g *getCmd) run() error {
res, err := helm.GetReleaseContent(g.release)
if err != nil { if err != nil {
return prettyError(err) return prettyError(err)
} }
...@@ -108,30 +149,26 @@ func getCmd(cmd *cobra.Command, args []string) error { ...@@ -108,30 +149,26 @@ func getCmd(cmd *cobra.Command, args []string) error {
return err return err
} }
fmt.Printf("CHART: %s-%s\n", res.Release.Chart.Metadata.Name, res.Release.Chart.Metadata.Version) fmt.Fprintf(g.out, "CHART: %s-%s\n", res.Release.Chart.Metadata.Name, res.Release.Chart.Metadata.Version)
fmt.Printf("RELEASED: %s\n", timeconv.Format(res.Release.Info.LastDeployed, time.ANSIC)) fmt.Fprintf(g.out, "RELEASED: %s\n", timeconv.Format(res.Release.Info.LastDeployed, time.ANSIC))
fmt.Println("USER-SUPPLIED VALUES:") fmt.Fprintln(g.out, "USER-SUPPLIED VALUES:")
fmt.Println(res.Release.Config.Raw) fmt.Fprintln(g.out, res.Release.Config.Raw)
fmt.Println("COMPUTED VALUES:") fmt.Fprintln(g.out, "COMPUTED VALUES:")
fmt.Println(cfgStr) fmt.Fprintln(g.out, cfgStr)
fmt.Println("MANIFEST:") fmt.Fprintln(g.out, "MANIFEST:")
fmt.Println(res.Release.Manifest) fmt.Fprintln(g.out, res.Release.Manifest)
return nil return nil
} }
// getValues implements 'helm get values' // getValues implements 'helm get values'
func getValues(cmd *cobra.Command, args []string) error { func (g *getValuesCmd) run() error {
if len(args) == 0 { res, err := helm.GetReleaseContent(g.release)
return errReleaseRequired
}
res, err := helm.GetReleaseContent(args[0])
if err != nil { if err != nil {
return prettyError(err) return prettyError(err)
} }
// If the user wants all values, compute the values and return. // If the user wants all values, compute the values and return.
if allValues { if g.allValues {
cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config, nil) cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config, nil)
if err != nil { if err != nil {
return err return err
...@@ -140,24 +177,20 @@ func getValues(cmd *cobra.Command, args []string) error { ...@@ -140,24 +177,20 @@ func getValues(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(cfgStr) fmt.Fprintln(g.out, cfgStr)
return nil return nil
} }
fmt.Println(res.Release.Config.Raw) fmt.Fprintln(g.out, res.Release.Config.Raw)
return nil return nil
} }
// getManifest implements 'helm get manifest' // getManifest implements 'helm get manifest'
func getManifest(cmd *cobra.Command, args []string) error { func (g *getManifestCmd) run() error {
if len(args) == 0 { res, err := helm.GetReleaseContent(g.release)
return errReleaseRequired
}
res, err := helm.GetReleaseContent(args[0])
if err != nil { if err != nil {
return prettyError(err) return prettyError(err)
} }
fmt.Println(res.Release.Manifest) fmt.Fprintln(g.out, res.Release.Manifest)
return nil return nil
} }
...@@ -86,6 +86,7 @@ func newRootCmd(out io.Writer) *cobra.Command { ...@@ -86,6 +86,7 @@ func newRootCmd(out io.Writer) *cobra.Command {
p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output") p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output")
cmd.AddCommand(newListCmd(nil, out)) cmd.AddCommand(newListCmd(nil, out))
cmd.AddCommand(newGetCmd(nil, out))
return cmd return cmd
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment