github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/cmd/tools/helm/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  /*
    17  NOTICE: This file's 'package' and some functionality has been modified / removed to fit within Jackal's package structure.
    18  */
    19  
    20  // Package helm is a copy of the main package from helm to include a subset of the helm CLI in Jackal
    21  package helm
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  	"os"
    27  	"path/filepath"
    28  
    29  	"github.com/spf13/cobra"
    30  	"k8s.io/client-go/util/homedir"
    31  
    32  	"helm.sh/helm/v3/cmd/helm/require"
    33  	"helm.sh/helm/v3/pkg/action"
    34  	"helm.sh/helm/v3/pkg/downloader"
    35  	"helm.sh/helm/v3/pkg/getter"
    36  )
    37  
    38  const dependencyBuildDesc = `
    39  Build out the charts/ directory from the Chart.lock file.
    40  
    41  Build is used to reconstruct a chart's dependencies to the state specified in
    42  the lock file. This will not re-negotiate dependencies, as 'jackal tools helm dependency update'
    43  does.
    44  
    45  If no lock file is found, 'jackal tools helm dependency build' will mirror the behavior
    46  of 'jackal tools helm dependency update'.
    47  `
    48  
    49  func newDependencyBuildCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    50  	client := action.NewDependency()
    51  
    52  	cmd := &cobra.Command{
    53  		Use:   "build CHART",
    54  		Short: "rebuild the charts/ directory based on the Chart.lock file",
    55  		Long:  dependencyBuildDesc,
    56  		Args:  require.MaximumNArgs(1),
    57  		RunE: func(_ *cobra.Command, args []string) error {
    58  			chartpath := "."
    59  			if len(args) > 0 {
    60  				chartpath = filepath.Clean(args[0])
    61  			}
    62  			man := &downloader.Manager{
    63  				Out:              out,
    64  				ChartPath:        chartpath,
    65  				Keyring:          client.Keyring,
    66  				SkipUpdate:       client.SkipRefresh,
    67  				Getters:          getter.All(settings),
    68  				RegistryClient:   cfg.RegistryClient,
    69  				RepositoryConfig: settings.RepositoryConfig,
    70  				RepositoryCache:  settings.RepositoryCache,
    71  				Debug:            settings.Debug,
    72  			}
    73  			if client.Verify {
    74  				man.Verify = downloader.VerifyIfPossible
    75  			}
    76  			err := man.Build()
    77  			if e, ok := err.(downloader.ErrRepoNotFound); ok {
    78  				return fmt.Errorf("%s. Please add the missing repos via 'jackal tools helm repo add'", e.Error())
    79  			}
    80  			return err
    81  		},
    82  	}
    83  
    84  	f := cmd.Flags()
    85  	f.BoolVar(&client.Verify, "verify", false, "verify the packages against signatures")
    86  	f.StringVar(&client.Keyring, "keyring", defaultKeyring(), "keyring containing public keys")
    87  	f.BoolVar(&client.SkipRefresh, "skip-refresh", false, "do not refresh the local repository cache")
    88  
    89  	return cmd
    90  }
    91  
    92  // defaultKeyring returns the expanded path to the default keyring.
    93  func defaultKeyring() string {
    94  	if v, ok := os.LookupEnv("GNUPGHOME"); ok {
    95  		return filepath.Join(v, "pubring.gpg")
    96  	}
    97  	return filepath.Join(homedir.HomeDir(), ".gnupg", "pubring.gpg")
    98  }