github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/package.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  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/pkg/errors"
    26  	"github.com/spf13/cobra"
    27  
    28  	"helm.sh/helm/v3/pkg/action"
    29  	"helm.sh/helm/v3/pkg/cli/values"
    30  	"helm.sh/helm/v3/pkg/downloader"
    31  	"helm.sh/helm/v3/pkg/getter"
    32  )
    33  
    34  const packageDesc = `
    35  This command packages a chart into a versioned chart archive file. If a path
    36  is given, this will look at that path for a chart (which must contain a
    37  Chart.yaml file) and then package that directory.
    38  
    39  Versioned chart archives are used by Helm package repositories.
    40  
    41  To sign a chart, use the '--sign' flag. In most cases, you should also
    42  provide '--keyring path/to/secret/keys' and '--key keyname'.
    43  
    44    $ helm package --sign ./mychart --key mykey --keyring ~/.gnupg/secring.gpg
    45  
    46  If '--keyring' is not specified, Helm usually defaults to the public keyring
    47  unless your environment is otherwise configured.
    48  `
    49  
    50  func newPackageCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    51  	client := action.NewPackage()
    52  	valueOpts := &values.Options{}
    53  
    54  	cmd := &cobra.Command{
    55  		Use:   "package [CHART_PATH] [...]",
    56  		Short: "package a chart directory into a chart archive",
    57  		Long:  packageDesc,
    58  		RunE: func(cmd *cobra.Command, args []string) error {
    59  			if len(args) == 0 {
    60  				return errors.Errorf("need at least one argument, the path to the chart")
    61  			}
    62  			if client.Sign {
    63  				if client.Key == "" {
    64  					return errors.New("--key is required for signing a package")
    65  				}
    66  				if client.Keyring == "" {
    67  					return errors.New("--keyring is required for signing a package")
    68  				}
    69  			}
    70  			client.RepositoryConfig = settings.RepositoryConfig
    71  			client.RepositoryCache = settings.RepositoryCache
    72  			p := getter.All(settings)
    73  			vals, err := valueOpts.MergeValues(p)
    74  			if err != nil {
    75  				return err
    76  			}
    77  
    78  			for i := 0; i < len(args); i++ {
    79  				path, err := filepath.Abs(args[i])
    80  				if err != nil {
    81  					return err
    82  				}
    83  				if _, err := os.Stat(args[i]); err != nil {
    84  					return err
    85  				}
    86  
    87  				if client.DependencyUpdate {
    88  					downloadManager := &downloader.Manager{
    89  						Out:              io.Discard,
    90  						ChartPath:        path,
    91  						Keyring:          client.Keyring,
    92  						Getters:          p,
    93  						Debug:            settings.Debug,
    94  						RegistryClient:   cfg.RegistryClient,
    95  						RepositoryConfig: settings.RepositoryConfig,
    96  						RepositoryCache:  settings.RepositoryCache,
    97  					}
    98  
    99  					if err := downloadManager.Update(); err != nil {
   100  						return err
   101  					}
   102  				}
   103  				p, err := client.Run(path, vals)
   104  				if err != nil {
   105  					return err
   106  				}
   107  				fmt.Fprintf(out, "Successfully packaged chart and saved it to: %s\n", p)
   108  			}
   109  			return nil
   110  		},
   111  	}
   112  
   113  	f := cmd.Flags()
   114  	f.BoolVar(&client.Sign, "sign", false, "use a PGP private key to sign this package")
   115  	f.StringVar(&client.Key, "key", "", "name of the key to use when signing. Used if --sign is true")
   116  	f.StringVar(&client.Keyring, "keyring", defaultKeyring(), "location of a public keyring")
   117  	f.StringVar(&client.PassphraseFile, "passphrase-file", "", `location of a file which contains the passphrase for the signing key. Use "-" in order to read from stdin.`)
   118  	f.StringVar(&client.Version, "version", "", "set the version on the chart to this semver version")
   119  	f.StringVar(&client.AppVersion, "app-version", "", "set the appVersion on the chart to this version")
   120  	f.StringVarP(&client.Destination, "destination", "d", ".", "location to write the chart.")
   121  	f.BoolVarP(&client.DependencyUpdate, "dependency-update", "u", false, `update dependencies from "Chart.yaml" to dir "charts/" before packaging`)
   122  
   123  	return cmd
   124  }