github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/pull.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  )
    29  
    30  const pullDesc = `
    31  Retrieve a package from a package repository, and download it locally.
    32  
    33  This is useful for fetching packages to inspect, modify, or repackage. It can
    34  also be used to perform cryptographic verification of a chart without installing
    35  the chart.
    36  
    37  There are options for unpacking the chart after download. This will create a
    38  directory for the chart and uncompress into that directory.
    39  
    40  If the --verify flag is specified, the requested chart MUST have a provenance
    41  file, and MUST pass the verification process. Failure in any part of this will
    42  result in an error, and the chart will not be saved locally.
    43  `
    44  
    45  func newPullCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    46  	client := action.NewPullWithOpts(action.WithConfig(cfg))
    47  
    48  	cmd := &cobra.Command{
    49  		Use:     "pull [chart URL | repo/chartname] [...]",
    50  		Short:   "download a chart from a repository and (optionally) unpack it in local directory",
    51  		Aliases: []string{"fetch"},
    52  		Long:    pullDesc,
    53  		Args:    require.MinimumNArgs(1),
    54  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    55  			if len(args) != 0 {
    56  				return nil, cobra.ShellCompDirectiveNoFileComp
    57  			}
    58  			return compListCharts(toComplete, false)
    59  		},
    60  		RunE: func(cmd *cobra.Command, args []string) error {
    61  			client.Settings = settings
    62  			if client.Version == "" && client.Devel {
    63  				debug("setting version to >0.0.0-0")
    64  				client.Version = ">0.0.0-0"
    65  			}
    66  
    67  			registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify)
    68  			if err != nil {
    69  				return fmt.Errorf("missing registry client: %w", err)
    70  			}
    71  			client.SetRegistryClient(registryClient)
    72  
    73  			for i := 0; i < len(args); i++ {
    74  				output, err := client.Run(args[i])
    75  				if err != nil {
    76  					return err
    77  				}
    78  				fmt.Fprint(out, output)
    79  			}
    80  			return nil
    81  		},
    82  	}
    83  
    84  	f := cmd.Flags()
    85  	f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")
    86  	f.BoolVar(&client.Untar, "untar", false, "if set to true, will untar the chart after downloading it")
    87  	f.BoolVar(&client.VerifyLater, "prov", false, "fetch the provenance file, but don't perform verification")
    88  	f.StringVar(&client.UntarDir, "untardir", ".", "if untar is specified, this flag specifies the name of the directory into which the chart is expanded")
    89  	f.StringVarP(&client.DestDir, "destination", "d", ".", "location to write the chart. If this and untardir are specified, untardir is appended to this")
    90  	addChartPathOptionsFlags(f, &client.ChartPathOptions)
    91  
    92  	err := cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    93  		if len(args) != 1 {
    94  			return nil, cobra.ShellCompDirectiveNoFileComp
    95  		}
    96  		return compVersionFlag(args[0], toComplete)
    97  	})
    98  
    99  	if err != nil {
   100  		log.Fatal(err)
   101  	}
   102  
   103  	return cmd
   104  }