mirror of
https://github.com/helm/helm.git
synced 2026-05-28 04:35:48 -04:00
fix(cmd): Add --output option to get values
Signed-off-by: Dean Coakley <dean.s.coakley@gmail.com>
This commit is contained in:
parent
7ffc879f13
commit
66268d9eee
4 changed files with 40 additions and 20 deletions
|
|
@ -17,20 +17,26 @@ limitations under the License.
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/gosuri/uitable"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"helm.sh/helm/v3/cmd/helm/require"
|
||||
"helm.sh/helm/v3/pkg/action"
|
||||
"helm.sh/helm/v3/pkg/cli/output"
|
||||
)
|
||||
|
||||
var getValuesHelp = `
|
||||
This command downloads a values file for a given release.
|
||||
`
|
||||
|
||||
type valuesWriter struct {
|
||||
vals map[string]interface{}
|
||||
}
|
||||
|
||||
func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
|
||||
var outfmt output.Format
|
||||
client := action.NewGetValues(cfg)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
|
|
@ -39,17 +45,35 @@ func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
|
|||
Long: getValuesHelp,
|
||||
Args: require.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
res, err := client.Run(args[0])
|
||||
vals, err := client.Run(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(out, res)
|
||||
return nil
|
||||
return outfmt.Write(out, &valuesWriter{vals})
|
||||
},
|
||||
}
|
||||
|
||||
f := cmd.Flags()
|
||||
f.IntVar(&client.Version, "revision", 0, "get the named release with revision")
|
||||
f.BoolVarP(&client.AllValues, "all", "a", false, "dump all (computed) values")
|
||||
bindOutputFlag(cmd, &outfmt)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (v valuesWriter) WriteTable(out io.Writer) error {
|
||||
table := uitable.New()
|
||||
table.AddRow("USER-SUPPLIED VALUES:")
|
||||
for k, v := range v.vals {
|
||||
table.AddRow(k, v)
|
||||
}
|
||||
return output.EncodeTable(out, table)
|
||||
}
|
||||
|
||||
func (v valuesWriter) WriteJSON(out io.Writer) error {
|
||||
return output.EncodeJSON(out, v)
|
||||
}
|
||||
|
||||
func (v valuesWriter) WriteYAML(out io.Writer) error {
|
||||
return output.EncodeYAML(out, v)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,13 @@ func TestGetValuesCmd(t *testing.T) {
|
|||
name: "get values requires release name arg",
|
||||
cmd: "get values",
|
||||
golden: "output/get-values-args.txt",
|
||||
rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "thomas-guide"})},
|
||||
wantError: true,
|
||||
}, {
|
||||
name: "get values to json",
|
||||
cmd: "get values thomas-guide --output json",
|
||||
golden: "output/values.json",
|
||||
rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "thomas-guide"})},
|
||||
}}
|
||||
runTestCmd(t, tests)
|
||||
}
|
||||
|
|
|
|||
1
cmd/helm/testdata/output/values.json
vendored
Normal file
1
cmd/helm/testdata/output/values.json
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"name": "value"}
|
||||
|
|
@ -17,8 +17,6 @@ limitations under the License.
|
|||
package action
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"helm.sh/helm/v3/pkg/chartutil"
|
||||
)
|
||||
|
||||
|
|
@ -40,29 +38,20 @@ func NewGetValues(cfg *Configuration) *GetValues {
|
|||
}
|
||||
|
||||
// Run executes 'helm get values' against the given release.
|
||||
func (g *GetValues) Run(name string) (string, error) {
|
||||
func (g *GetValues) Run(name string) (map[string]interface{}, error) {
|
||||
res, err := g.cfg.releaseContent(name, g.Version)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If the user wants all values, compute the values and return.
|
||||
if g.AllValues {
|
||||
cfg, err := chartutil.CoalesceValues(res.Chart, res.Config)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
cfgStr, err := cfg.YAML()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cfgStr, nil
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
resConfig, err := yaml.Marshal(res.Config)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(resConfig), nil
|
||||
return res.Chart.Values, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue