github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/helm/step_helm_list.go (about)

     1  package helm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     9  
    10  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    11  
    12  	"github.com/olli-ai/jx/v2/pkg/helm"
    13  	"github.com/pkg/errors"
    14  
    15  	"github.com/jenkins-x/jx-logging/pkg/log"
    16  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  // StepHelmListOptions contains the command line flags
    21  type StepHelmListOptions struct {
    22  	StepHelmOptions
    23  
    24  	Namespace string
    25  }
    26  
    27  var (
    28  	StepHelmListLong = templates.LongDesc(`
    29  		List the helm releases
    30  `)
    31  
    32  	StepHelmListExample = templates.Examples(`
    33  		# list all the helm releases in the current namespace
    34  		jx step helm list
    35  
    36  `)
    37  )
    38  
    39  func NewCmdStepHelmList(commonOpts *opts.CommonOptions) *cobra.Command {
    40  	options := StepHelmListOptions{
    41  		StepHelmOptions: StepHelmOptions{
    42  			StepOptions: step.StepOptions{
    43  				CommonOptions: commonOpts,
    44  			},
    45  		},
    46  	}
    47  	cmd := &cobra.Command{
    48  		Use:     "list",
    49  		Short:   "List the helm releases",
    50  		Aliases: []string{""},
    51  		Long:    StepHelmListLong,
    52  		Example: StepHelmListExample,
    53  		Run: func(cmd *cobra.Command, args []string) {
    54  			options.Cmd = cmd
    55  			options.Args = args
    56  			err := options.Run()
    57  			helper.CheckErr(err)
    58  		},
    59  	}
    60  	options.addStepHelmFlags(cmd)
    61  	cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "the namespace to look for the helm releases. Defaults to the current namespace")
    62  
    63  	return cmd
    64  }
    65  
    66  func (o *StepHelmListOptions) Run() error {
    67  	h := o.Helm()
    68  	if h == nil {
    69  		return fmt.Errorf("No Helmer created!")
    70  	}
    71  	releases, sortedKeys, err := h.ListReleases(o.Namespace)
    72  	if err != nil {
    73  		return errors.WithStack(err)
    74  	}
    75  	output, err := helm.RenderReleasesAsTable(releases, sortedKeys)
    76  	if err != nil {
    77  		return errors.WithStack(err)
    78  	}
    79  	log.Logger().Info(output)
    80  	return nil
    81  }