github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/cmd/helm/get_values.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"fmt"
    21  	"io"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/helm/pkg/chartutil"
    26  	"k8s.io/helm/pkg/helm"
    27  )
    28  
    29  var getValuesHelp = `
    30  This command downloads a values file for a given release.
    31  `
    32  
    33  type getValuesCmd struct {
    34  	release   string
    35  	allValues bool
    36  	out       io.Writer
    37  	client    helm.Interface
    38  	version   int32
    39  }
    40  
    41  func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
    42  	get := &getValuesCmd{
    43  		out:    out,
    44  		client: client,
    45  	}
    46  	cmd := &cobra.Command{
    47  		Use:     "values [flags] RELEASE_NAME",
    48  		Short:   "download the values file for a named release",
    49  		Long:    getValuesHelp,
    50  		PreRunE: setupConnection,
    51  		RunE: func(cmd *cobra.Command, args []string) error {
    52  			if len(args) == 0 {
    53  				return errReleaseRequired
    54  			}
    55  			get.release = args[0]
    56  			get.client = ensureHelmClient(get.client)
    57  			return get.run()
    58  		},
    59  	}
    60  
    61  	cmd.Flags().Int32Var(&get.version, "revision", 0, "get the named release with revision")
    62  	cmd.Flags().BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values")
    63  	return cmd
    64  }
    65  
    66  // getValues implements 'helm get values'
    67  func (g *getValuesCmd) run() error {
    68  	res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.version))
    69  	if err != nil {
    70  		return prettyError(err)
    71  	}
    72  
    73  	// If the user wants all values, compute the values and return.
    74  	if g.allValues {
    75  		cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		cfgStr, err := cfg.YAML()
    80  		if err != nil {
    81  			return err
    82  		}
    83  		fmt.Fprintln(g.out, cfgStr)
    84  		return nil
    85  	}
    86  
    87  	fmt.Fprintln(g.out, res.Release.Config.Raw)
    88  	return nil
    89  }