github.com/appscode/helm@v3.0.0-alpha.1+incompatible/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  	"path/filepath"
    24  
    25  	"helm.sh/helm/pkg/action"
    26  
    27  	"github.com/pkg/errors"
    28  	"github.com/spf13/cobra"
    29  
    30  	"helm.sh/helm/pkg/downloader"
    31  	"helm.sh/helm/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  If no path is given, this will look in the present working directory for a
    40  Chart.yaml file, and (if found) build the current directory into a chart.
    41  
    42  Versioned chart archives are used by Helm package repositories.
    43  `
    44  
    45  func newPackageCmd(out io.Writer) *cobra.Command {
    46  	client := action.NewPackage()
    47  
    48  	cmd := &cobra.Command{
    49  		Use:   "package [CHART_PATH] [...]",
    50  		Short: "package a chart directory into a chart archive",
    51  		Long:  packageDesc,
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			if len(args) == 0 {
    54  				return errors.Errorf("need at least one argument, the path to the chart")
    55  			}
    56  			if client.Sign {
    57  				if client.Key == "" {
    58  					return errors.New("--key is required for signing a package")
    59  				}
    60  				if client.Keyring == "" {
    61  					return errors.New("--keyring is required for signing a package")
    62  				}
    63  			}
    64  			if err := client.ValueOptions.MergeValues(settings); err != nil {
    65  				return err
    66  			}
    67  
    68  			for i := 0; i < len(args); i++ {
    69  				path, err := filepath.Abs(args[i])
    70  				if err != nil {
    71  					return err
    72  				}
    73  
    74  				if client.DependencyUpdate {
    75  					downloadManager := &downloader.Manager{
    76  						Out:       ioutil.Discard,
    77  						ChartPath: path,
    78  						HelmHome:  settings.Home,
    79  						Keyring:   client.Keyring,
    80  						Getters:   getter.All(settings),
    81  						Debug:     settings.Debug,
    82  					}
    83  
    84  					if err := downloadManager.Update(); err != nil {
    85  						return err
    86  					}
    87  				}
    88  				p, err := client.Run(path)
    89  				if err != nil {
    90  					return err
    91  				}
    92  				fmt.Fprintf(out, "Successfully packaged chart and saved it to: %s\n", p)
    93  			}
    94  			return nil
    95  		},
    96  	}
    97  
    98  	f := cmd.Flags()
    99  	f.BoolVar(&client.Sign, "sign", false, "use a PGP private key to sign this package")
   100  	f.StringVar(&client.Key, "key", "", "name of the key to use when signing. Used if --sign is true")
   101  	f.StringVar(&client.Keyring, "keyring", defaultKeyring(), "location of a public keyring")
   102  	f.StringVar(&client.Version, "version", "", "set the version on the chart to this semver version")
   103  	f.StringVar(&client.AppVersion, "app-version", "", "set the appVersion on the chart to this version")
   104  	f.StringVarP(&client.Destination, "destination", "d", ".", "location to write the chart.")
   105  	f.BoolVarP(&client.DependencyUpdate, "dependency-update", "u", false, `update dependencies from "Chart.yaml" to dir "charts/" before packaging`)
   106  	addValueOptionsFlags(f, &client.ValueOptions)
   107  
   108  	return cmd
   109  }