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