ref(cmd): refactor get command

pull/921/head
Adam Reese 8 years ago
parent e339cc7e0c
commit 5e3044a65f

@ -19,6 +19,7 @@ package main
import (
"errors"
"fmt"
"io"
"time"
"github.com/spf13/cobra"
@ -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.
`
var allValues = false
var errReleaseRequired = errors.New("release name is required")
var getCommand = &cobra.Command{
Use: "get [flags] RELEASE_NAME",
Short: "download a named release",
Long: getHelp,
RunE: getCmd,
PersistentPreRunE: setupConnection,
type getCmd struct {
release string
out io.Writer
client helm.Interface
}
var getValuesCommand = &cobra.Command{
Use: "values [flags] RELEASE_NAME",
Short: "download the values file for a named release",
Long: getValuesHelp,
RunE: getValues,
func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
get := &getCmd{
out: out,
client: client,
}
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{
Use: "manifest [flags] RELEASE_NAME",
Short: "download the manifest for a named release",
Long: getManifestHelp,
RunE: getManifest,
type getValuesCmd struct {
allValues bool
getCmd
}
func init() {
// 'get values' flags.
getValuesCommand.PersistentFlags().BoolVarP(&allValues, "all", "a", false, "dump all (computed) values")
func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
get := &getValuesCmd{}
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)
getCommand.AddCommand(getManifestCommand)
RootCommand.AddCommand(getCommand)
type getManifestCmd struct {
getCmd
}
// getCmd is the command that implements 'helm get'
func getCmd(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
func newGetManifestCmd(client helm.Interface, out io.Writer) *cobra.Command {
get := &getManifestCmd{}
get.out = out
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 {
return prettyError(err)
}
@ -108,30 +149,26 @@ func getCmd(cmd *cobra.Command, args []string) error {
return err
}
fmt.Printf("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.Println("USER-SUPPLIED VALUES:")
fmt.Println(res.Release.Config.Raw)
fmt.Println("COMPUTED VALUES:")
fmt.Println(cfgStr)
fmt.Println("MANIFEST:")
fmt.Println(res.Release.Manifest)
fmt.Fprintf(g.out, "CHART: %s-%s\n", res.Release.Chart.Metadata.Name, res.Release.Chart.Metadata.Version)
fmt.Fprintf(g.out, "RELEASED: %s\n", timeconv.Format(res.Release.Info.LastDeployed, time.ANSIC))
fmt.Fprintln(g.out, "USER-SUPPLIED VALUES:")
fmt.Fprintln(g.out, res.Release.Config.Raw)
fmt.Fprintln(g.out, "COMPUTED VALUES:")
fmt.Fprintln(g.out, cfgStr)
fmt.Fprintln(g.out, "MANIFEST:")
fmt.Fprintln(g.out, res.Release.Manifest)
return nil
}
// getValues implements 'helm get values'
func getValues(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
}
res, err := helm.GetReleaseContent(args[0])
func (g *getValuesCmd) run() error {
res, err := helm.GetReleaseContent(g.release)
if err != nil {
return prettyError(err)
}
// 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)
if err != nil {
return err
@ -140,24 +177,20 @@ func getValues(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Println(cfgStr)
fmt.Fprintln(g.out, cfgStr)
return nil
}
fmt.Println(res.Release.Config.Raw)
fmt.Fprintln(g.out, res.Release.Config.Raw)
return nil
}
// getManifest implements 'helm get manifest'
func getManifest(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
}
res, err := helm.GetReleaseContent(args[0])
func (g *getManifestCmd) run() error {
res, err := helm.GetReleaseContent(g.release)
if err != nil {
return prettyError(err)
}
fmt.Println(res.Release.Manifest)
fmt.Fprintln(g.out, res.Release.Manifest)
return nil
}

@ -86,6 +86,7 @@ func newRootCmd(out io.Writer) *cobra.Command {
p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output")
cmd.AddCommand(newListCmd(nil, out))
cmd.AddCommand(newGetCmd(nil, out))
return cmd
}

Loading…
Cancel
Save