github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/dependency_build.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package cmd
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/spf13/cobra"
    25  	"k8s.io/client-go/util/homedir"
    26  
    27  	"helm.sh/helm/v3/cmd/helm/require"
    28  	"helm.sh/helm/v3/pkg/action"
    29  	"helm.sh/helm/v3/pkg/downloader"
    30  	"helm.sh/helm/v3/pkg/getter"
    31  )
    32  
    33  const dependencyBuildDesc = `
    34  Build out the charts/ directory from the Chart.lock file.
    35  
    36  Build is used to reconstruct a chart's dependencies to the state specified in
    37  the lock file. This will not re-negotiate dependencies, as 'helm dependency update'
    38  does.
    39  
    40  If no lock file is found, 'helm dependency build' will mirror the behavior
    41  of 'helm dependency update'.
    42  `
    43  
    44  func newDependencyBuildCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    45  	client := action.NewDependency()
    46  
    47  	cmd := &cobra.Command{
    48  		Use:   "build CHART",
    49  		Short: "rebuild the charts/ directory based on the Chart.lock file",
    50  		Long:  dependencyBuildDesc,
    51  		Args:  require.MaximumNArgs(1),
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			chartpath := "."
    54  			if len(args) > 0 {
    55  				chartpath = filepath.Clean(args[0])
    56  			}
    57  			man := &downloader.Manager{
    58  				Out:              out,
    59  				ChartPath:        chartpath,
    60  				Keyring:          client.Keyring,
    61  				SkipUpdate:       client.SkipRefresh,
    62  				Getters:          getter.All(settings),
    63  				RegistryClient:   cfg.RegistryClient,
    64  				RepositoryConfig: settings.RepositoryConfig,
    65  				RepositoryCache:  settings.RepositoryCache,
    66  				Debug:            settings.Debug,
    67  			}
    68  			if client.Verify {
    69  				man.Verify = downloader.VerifyIfPossible
    70  			}
    71  			err := man.Build()
    72  			if e, ok := err.(downloader.ErrRepoNotFound); ok {
    73  				return fmt.Errorf("%s. Please add the missing repos via 'helm repo add'", e.Error())
    74  			}
    75  			return err
    76  		},
    77  	}
    78  
    79  	f := cmd.Flags()
    80  	f.BoolVar(&client.Verify, "verify", false, "verify the packages against signatures")
    81  	f.StringVar(&client.Keyring, "keyring", defaultKeyring(), "keyring containing public keys")
    82  	f.BoolVar(&client.SkipRefresh, "skip-refresh", false, "do not refresh the local repository cache")
    83  
    84  	return cmd
    85  }
    86  
    87  // defaultKeyring returns the expanded path to the default keyring.
    88  func defaultKeyring() string {
    89  	if v, ok := os.LookupEnv("GNUPGHOME"); ok {
    90  		return filepath.Join(v, "pubring.gpg")
    91  	}
    92  	return filepath.Join(homedir.HomeDir(), ".gnupg", "pubring.gpg")
    93  }