2016-06-23 02:28:45 +08:00
/ *
2018-08-25 03:03:55 +08:00
Copyright The Helm Authors .
2016-06-23 02:28:45 +08:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2025-02-24 23:11:54 +08:00
package cmd
2016-04-15 07:08:05 +08:00
import (
2024-11-16 11:07:40 +08:00
"errors"
2016-04-19 11:39:31 +08:00
"fmt"
2016-07-28 06:23:27 +08:00
"io"
2020-01-23 19:46:18 +08:00
"os"
2016-04-15 07:08:05 +08:00
"path/filepath"
"github.com/spf13/cobra"
2016-07-28 06:23:27 +08:00
2024-12-27 05:33:51 +08:00
"helm.sh/helm/v4/pkg/action"
"helm.sh/helm/v4/pkg/cli/values"
"helm.sh/helm/v4/pkg/downloader"
"helm.sh/helm/v4/pkg/getter"
2016-04-15 07:08:05 +08:00
)
const packageDesc = `
This command packages a chart into a versioned chart archive file . If a path
is given , this will look at that path for a chart ( which must contain a
Chart . yaml file ) and then package that directory .
Versioned chart archives are used by Helm package repositories .
2019-12-07 00:14:39 +08:00
To sign a chart , use the ' -- sign ' flag . In most cases , you should also
provide ' -- keyring path / to / secret / keys ' and ' -- key keyname ' .
$ helm package -- sign . / mychart -- key mykey -- keyring ~ / . gnupg / secring . gpg
If ' -- keyring ' is not specified , Helm usually defaults to the public keyring
unless your environment is otherwise configured .
2016-04-15 07:08:05 +08:00
`
2024-10-23 00:56:58 +08:00
func newPackageCmd ( out io . Writer ) * cobra . Command {
2019-02-09 08:02:57 +08:00
client := action . NewPackage ( )
2019-08-02 05:40:52 +08:00
valueOpts := & values . Options { }
2016-10-15 11:05:04 +08:00
2016-07-28 06:23:27 +08:00
cmd := & cobra . Command {
2018-05-17 00:56:45 +08:00
Use : "package [CHART_PATH] [...]" ,
2016-07-28 06:23:27 +08:00
Short : "package a chart directory into a chart archive" ,
Long : packageDesc ,
2024-03-12 05:13:34 +08:00
RunE : func ( _ * cobra . Command , args [ ] string ) error {
2016-07-28 06:23:27 +08:00
if len ( args ) == 0 {
2024-11-16 11:07:40 +08:00
return fmt . Errorf ( "need at least one argument, the path to the chart" )
2016-07-28 06:23:27 +08:00
}
2019-02-09 08:02:57 +08:00
if client . Sign {
if client . Key == "" {
2016-07-28 06:23:27 +08:00
return errors . New ( "--key is required for signing a package" )
}
2019-02-09 08:02:57 +08:00
if client . Keyring == "" {
2016-07-28 06:23:27 +08:00
return errors . New ( "--keyring is required for signing a package" )
}
}
2019-08-23 14:31:50 +08:00
client . RepositoryConfig = settings . RepositoryConfig
client . RepositoryCache = settings . RepositoryCache
p := getter . All ( settings )
vals , err := valueOpts . MergeValues ( p )
2019-08-01 23:04:36 +08:00
if err != nil {
2019-02-09 08:02:57 +08:00
return err
}
2024-11-20 05:18:27 +08:00
registryClient , err := newRegistryClient ( client . CertFile , client . KeyFile , client . CaFile ,
2024-06-20 01:37:31 +08:00
client . InsecureSkipTLSverify , client . PlainHTTP , client . Username , client . Password )
if err != nil {
return fmt . Errorf ( "missing registry client: %w" , err )
}
2016-08-26 04:36:27 +08:00
for i := 0 ; i < len ( args ) ; i ++ {
2019-02-09 08:02:57 +08:00
path , err := filepath . Abs ( args [ i ] )
if err != nil {
return err
}
2020-01-23 19:46:18 +08:00
if _ , err := os . Stat ( args [ i ] ) ; err != nil {
return err
}
2019-02-09 08:02:57 +08:00
if client . DependencyUpdate {
downloadManager := & downloader . Manager {
2023-03-22 21:31:16 +08:00
Out : io . Discard ,
2019-08-23 14:31:50 +08:00
ChartPath : path ,
Keyring : client . Keyring ,
Getters : p ,
Debug : settings . Debug ,
2024-06-20 01:37:31 +08:00
RegistryClient : registryClient ,
2019-08-23 14:31:50 +08:00
RepositoryConfig : settings . RepositoryConfig ,
RepositoryCache : settings . RepositoryCache ,
2025-08-22 02:25:55 +08:00
ContentCache : settings . ContentCache ,
2019-02-09 08:02:57 +08:00
}
if err := downloadManager . Update ( ) ; err != nil {
return err
}
}
2019-08-01 23:04:36 +08:00
p , err := client . Run ( path , vals )
2019-02-09 08:02:57 +08:00
if err != nil {
2016-08-26 04:36:27 +08:00
return err
}
2019-02-09 08:02:57 +08:00
fmt . Fprintf ( out , "Successfully packaged chart and saved it to: %s\n" , p )
2016-08-26 04:36:27 +08:00
}
return nil
2016-07-28 06:23:27 +08:00
} ,
}
2016-04-15 07:08:05 +08:00
2019-03-13 23:58:35 +08:00
f := cmd . Flags ( )
f . BoolVar ( & client . Sign , "sign" , false , "use a PGP private key to sign this package" )
f . StringVar ( & client . Key , "key" , "" , "name of the key to use when signing. Used if --sign is true" )
f . StringVar ( & client . Keyring , "keyring" , defaultKeyring ( ) , "location of a public keyring" )
2020-09-19 06:23:40 +08:00
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. ` )
2019-03-13 23:58:35 +08:00
f . StringVar ( & client . Version , "version" , "" , "set the version on the chart to this semver version" )
f . StringVar ( & client . AppVersion , "app-version" , "" , "set the appVersion on the chart to this version" )
f . StringVarP ( & client . Destination , "destination" , "d" , "." , "location to write the chart." )
f . BoolVarP ( & client . DependencyUpdate , "dependency-update" , "u" , false , ` update dependencies from "Chart.yaml" to dir "charts/" before packaging ` )
2024-06-20 01:37:31 +08:00
f . StringVar ( & client . Username , "username" , "" , "chart repository username where to locate the requested chart" )
f . StringVar ( & client . Password , "password" , "" , "chart repository password where to locate the requested chart" )
f . StringVar ( & client . CertFile , "cert-file" , "" , "identify HTTPS client using this SSL certificate file" )
f . StringVar ( & client . KeyFile , "key-file" , "" , "identify HTTPS client using this SSL key file" )
f . BoolVar ( & client . InsecureSkipTLSverify , "insecure-skip-tls-verify" , false , "skip tls certificate checks for the chart download" )
f . BoolVar ( & client . PlainHTTP , "plain-http" , false , "use insecure HTTP connections for the chart download" )
2024-11-20 05:18:27 +08:00
f . StringVar ( & client . CaFile , "ca-file" , "" , "verify certificates of HTTPS-enabled servers using this CA bundle" )
2016-04-19 11:39:31 +08:00
2016-07-28 06:23:27 +08:00
return cmd
}