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

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cmd
    18  
    19  import (
    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  
    30  const getHooksHelp = `
    31  This command downloads hooks for a given release.
    32  
    33  Hooks are formatted in YAML and separated by the YAML '---\n' separator.
    34  `
    35  
    36  func 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, args, 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  }