1/*
2Copyright The Helm Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package main
18
19import (
20	"fmt"
21	"io"
22	"log"
23	"strings"
24
25	"github.com/spf13/cobra"
26
27	"helm.sh/helm/v3/cmd/helm/require"
28	"helm.sh/helm/v3/pkg/action"
29)
30
31const pullDesc = `
32Retrieve a package from a package repository, and download it locally.
33
34This is useful for fetching packages to inspect, modify, or repackage. It can
35also be used to perform cryptographic verification of a chart without installing
36the chart.
37
38There are options for unpacking the chart after download. This will create a
39directory for the chart and uncompress into that directory.
40
41If the --verify flag is specified, the requested chart MUST have a provenance
42file, and MUST pass the verification process. Failure in any part of this will
43result in an error, and the chart will not be saved locally.
44`
45
46func newPullCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
47	client := action.NewPullWithOpts(action.WithConfig(cfg))
48
49	cmd := &cobra.Command{
50		Use:     "pull [chart URL | repo/chartname] [...]",
51		Short:   "download a chart from a repository and (optionally) unpack it in local directory",
52		Aliases: []string{"fetch"},
53		Long:    pullDesc,
54		Args:    require.MinimumNArgs(1),
55		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
56			if len(args) != 0 {
57				return nil, cobra.ShellCompDirectiveNoFileComp
58			}
59			return compListCharts(toComplete, false)
60		},
61		RunE: func(cmd *cobra.Command, args []string) error {
62			client.Settings = settings
63			if client.Version == "" && client.Devel {
64				debug("setting version to >0.0.0-0")
65				client.Version = ">0.0.0-0"
66			}
67
68			if strings.HasPrefix(args[0], "oci://") {
69				if !FeatureGateOCI.IsEnabled() {
70					return FeatureGateOCI.Error()
71				}
72			}
73
74			for i := 0; i < len(args); i++ {
75				output, err := client.Run(args[i])
76				if err != nil {
77					return err
78				}
79				fmt.Fprint(out, output)
80			}
81			return nil
82		},
83	}
84
85	f := cmd.Flags()
86	f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")
87	f.BoolVar(&client.Untar, "untar", false, "if set to true, will untar the chart after downloading it")
88	f.BoolVar(&client.VerifyLater, "prov", false, "fetch the provenance file, but don't perform verification")
89	f.StringVar(&client.UntarDir, "untardir", ".", "if untar is specified, this flag specifies the name of the directory into which the chart is expanded")
90	f.StringVarP(&client.DestDir, "destination", "d", ".", "location to write the chart. If this and tardir are specified, tardir is appended to this")
91	addChartPathOptionsFlags(f, &client.ChartPathOptions)
92
93	err := cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
94		if len(args) != 1 {
95			return nil, cobra.ShellCompDirectiveNoFileComp
96		}
97		return compVersionFlag(args[0], toComplete)
98	})
99
100	if err != nil {
101		log.Fatal(err)
102	}
103
104	return cmd
105}
106