github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/cmd/helm/get_hooks.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/helm/pkg/helm"
    26  )
    27  
    28  const getHooksHelp = `
    29  This command downloads hooks for a given release.
    30  
    31  Hooks are formatted in YAML and separated by the YAML '---\n' separator.
    32  `
    33  
    34  type getHooksCmd struct {
    35  	release string
    36  	out     io.Writer
    37  	client  helm.Interface
    38  	version int32
    39  }
    40  
    41  func newGetHooksCmd(client helm.Interface, out io.Writer) *cobra.Command {
    42  	ghc := &getHooksCmd{
    43  		out:    out,
    44  		client: client,
    45  	}
    46  	cmd := &cobra.Command{
    47  		Use:     "hooks [flags] RELEASE_NAME",
    48  		Short:   "download all hooks for a named release",
    49  		Long:    getHooksHelp,
    50  		PreRunE: setupConnection,
    51  		RunE: func(cmd *cobra.Command, args []string) error {
    52  			if len(args) == 0 {
    53  				return errReleaseRequired
    54  			}
    55  			ghc.release = args[0]
    56  			ghc.client = ensureHelmClient(ghc.client)
    57  			return ghc.run()
    58  		},
    59  	}
    60  	cmd.Flags().Int32Var(&ghc.version, "revision", 0, "get the named release with revision")
    61  	return cmd
    62  }
    63  
    64  func (g *getHooksCmd) run() error {
    65  	res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.version))
    66  	if err != nil {
    67  		fmt.Fprintln(g.out, g.release)
    68  		return prettyError(err)
    69  	}
    70  
    71  	for _, hook := range res.Release.Hooks {
    72  		fmt.Fprintf(g.out, "---\n# %s\n%s", hook.Name, hook.Manifest)
    73  	}
    74  	return nil
    75  }