github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/cmd/helm/get_values.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/chartutil"
    27  	"k8s.io/helm/pkg/helm"
    28  )
    29  
    30  var getValuesHelp = `
    31  This command downloads a values file for a given release.
    32  `
    33  
    34  type getValuesCmd struct {
    35  	release   string
    36  	allValues bool
    37  	out       io.Writer
    38  	client    helm.Interface
    39  	version   int32
    40  	output    string
    41  }
    42  
    43  func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
    44  	get := &getValuesCmd{
    45  		out:    out,
    46  		client: client,
    47  	}
    48  	cmd := &cobra.Command{
    49  		Use:     "values [flags] RELEASE_NAME",
    50  		Short:   "download the values file for a named release",
    51  		Long:    getValuesHelp,
    52  		PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() },
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			if len(args) == 0 {
    55  				return errReleaseRequired
    56  			}
    57  			get.release = args[0]
    58  			get.client = ensureHelmClient(get.client)
    59  			return get.run()
    60  		},
    61  	}
    62  
    63  	f := cmd.Flags()
    64  	settings.AddFlagsTLS(f)
    65  	f.Int32Var(&get.version, "revision", 0, "get the named release with revision")
    66  	f.BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values")
    67  	f.StringVar(&get.output, "output", "yaml", "output the specified format (json or yaml)")
    68  
    69  	// set defaults from environment
    70  	settings.InitTLS(f)
    71  
    72  	return cmd
    73  }
    74  
    75  // getValues implements 'helm get values'
    76  func (g *getValuesCmd) run() error {
    77  	res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.version))
    78  	if err != nil {
    79  		return prettyError(err)
    80  	}
    81  
    82  	values, err := chartutil.ReadValues([]byte(res.Release.Config.Raw))
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	// If the user wants all values, compute the values and return.
    88  	if g.allValues {
    89  		values, err = chartutil.CoalesceValues(res.Release.Chart, res.Release.Config)
    90  		if err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	result, err := formatValues(g.output, values)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	fmt.Fprintln(g.out, result)
   100  	return nil
   101  }
   102  
   103  func formatValues(format string, values chartutil.Values) (string, error) {
   104  	switch format {
   105  	case "", "yaml":
   106  		out, err := values.YAML()
   107  		if err != nil {
   108  			return "", err
   109  		}
   110  		return out, nil
   111  	case "json":
   112  		out, err := json.Marshal(values)
   113  		if err != nil {
   114  			return "", fmt.Errorf("Failed to Marshal JSON output: %s", err)
   115  		}
   116  		return string(out), nil
   117  	default:
   118  		return "", fmt.Errorf("Unknown output format %q", format)
   119  	}
   120  }