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
24	"github.com/spf13/cobra"
25
26	"helm.sh/helm/v3/cmd/helm/require"
27	"helm.sh/helm/v3/pkg/action"
28)
29
30const getHooksHelp = `
31This command downloads hooks for a given release.
32
33Hooks are formatted in YAML and separated by the YAML '---\n' separator.
34`
35
36func newGetHooksCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
37	client := action.NewGet(cfg)
38
39	cmd := &cobra.Command{
40		Use:   "hooks RELEASE_NAME",
41		Short: "download all hooks for a named release",
42		Long:  getHooksHelp,
43		Args:  require.ExactArgs(1),
44		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
45			if len(args) != 0 {
46				return nil, cobra.ShellCompDirectiveNoFileComp
47			}
48			return compListReleases(toComplete, cfg)
49		},
50		RunE: func(cmd *cobra.Command, args []string) error {
51			res, err := client.Run(args[0])
52			if err != nil {
53				return err
54			}
55			for _, hook := range res.Hooks {
56				fmt.Fprintf(out, "---\n# Source: %s\n%s\n", hook.Path, hook.Manifest)
57			}
58			return nil
59		},
60	}
61
62	cmd.Flags().IntVar(&client.Version, "revision", 0, "get the named release with revision")
63	err := cmd.RegisterFlagCompletionFunc("revision", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
64		if len(args) == 1 {
65			return compListRevisions(toComplete, cfg, args[0])
66		}
67		return nil, cobra.ShellCompDirectiveNoFileComp
68	})
69
70	if err != nil {
71		log.Fatal(err)
72	}
73
74	return cmd
75}
76