github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"log"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"helm.sh/helm/v3/cmd/helm/require"
    27  	"helm.sh/helm/v3/pkg/action"
    28  	"helm.sh/helm/v3/pkg/cli/output"
    29  )
    30  
    31  var getValuesHelp = `
    32  This command downloads a values file for a given release.
    33  `
    34  
    35  type valuesWriter struct {
    36  	vals      map[string]interface{}
    37  	allValues bool
    38  }
    39  
    40  func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    41  	var outfmt output.Format
    42  	client := action.NewGetValues(cfg)
    43  
    44  	cmd := &cobra.Command{
    45  		Use:   "values RELEASE_NAME",
    46  		Short: "download the values file for a named release",
    47  		Long:  getValuesHelp,
    48  		Args:  require.ExactArgs(1),
    49  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    50  			if len(args) != 0 {
    51  				return nil, cobra.ShellCompDirectiveNoFileComp
    52  			}
    53  			return compListReleases(toComplete, args, cfg)
    54  		},
    55  		RunE: func(cmd *cobra.Command, args []string) error {
    56  			vals, err := client.Run(args[0])
    57  			if err != nil {
    58  				return err
    59  			}
    60  			return outfmt.Write(out, &valuesWriter{vals, client.AllValues})
    61  		},
    62  	}
    63  
    64  	f := cmd.Flags()
    65  	f.IntVar(&client.Version, "revision", 0, "get the named release with revision")
    66  	err := cmd.RegisterFlagCompletionFunc("revision", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    67  		if len(args) == 1 {
    68  			return compListRevisions(toComplete, cfg, args[0])
    69  		}
    70  		return nil, cobra.ShellCompDirectiveNoFileComp
    71  	})
    72  
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  
    77  	f.BoolVarP(&client.AllValues, "all", "a", false, "dump all (computed) values")
    78  	bindOutputFlag(cmd, &outfmt)
    79  
    80  	return cmd
    81  }
    82  
    83  func (v valuesWriter) WriteTable(out io.Writer) error {
    84  	if v.allValues {
    85  		fmt.Fprintln(out, "COMPUTED VALUES:")
    86  	} else {
    87  		fmt.Fprintln(out, "USER-SUPPLIED VALUES:")
    88  	}
    89  	return output.EncodeYAML(out, v.vals)
    90  }
    91  
    92  func (v valuesWriter) WriteJSON(out io.Writer) error {
    93  	return output.EncodeJSON(out, v.vals)
    94  }
    95  
    96  func (v valuesWriter) WriteYAML(out io.Writer) error {
    97  	return output.EncodeYAML(out, v.vals)
    98  }