From d5d741dfed59d82f2b350bdb0c97d05f83f4abd1 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Wed, 1 Jan 2020 15:57:51 -0500 Subject: [PATCH] feat(comp): Support completion for --revision flag Signed-off-by: Marc Khouzam --- cmd/helm/get_all.go | 8 ++++++++ cmd/helm/get_hooks.go | 10 +++++++++- cmd/helm/get_manifest.go | 10 +++++++++- cmd/helm/get_notes.go | 7 +++++++ cmd/helm/get_values.go | 7 +++++++ cmd/helm/history.go | 14 ++++++++++++++ cmd/helm/status.go | 9 +++++++++ 7 files changed, 63 insertions(+), 2 deletions(-) diff --git a/cmd/helm/get_all.go b/cmd/helm/get_all.go index 678b85f25..7d893d7e0 100644 --- a/cmd/helm/get_all.go +++ b/cmd/helm/get_all.go @@ -67,6 +67,14 @@ func newGetAllCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f := cmd.Flags() f.IntVar(&client.Version, "revision", 0, "get the named release with revision") + flag := f.Lookup("revision") + completion.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { + if len(args) == 1 { + return compListRevisions(cfg, args[0]) + } + return nil, completion.BashCompDirectiveNoFileComp + }) + f.StringVar(&template, "template", "", "go template for formatting the output, eg: {{.Release.Name}}") return cmd diff --git a/cmd/helm/get_hooks.go b/cmd/helm/get_hooks.go index 84ef0c1fc..c2087b1ba 100644 --- a/cmd/helm/get_hooks.go +++ b/cmd/helm/get_hooks.go @@ -61,7 +61,15 @@ func newGetHooksCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return compListReleases(toComplete, cfg) }) - cmd.Flags().IntVar(&client.Version, "revision", 0, "get the named release with revision") + f := cmd.Flags() + f.IntVar(&client.Version, "revision", 0, "get the named release with revision") + flag := f.Lookup("revision") + completion.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { + if len(args) == 1 { + return compListRevisions(cfg, args[0]) + } + return nil, completion.BashCompDirectiveNoFileComp + }) return cmd } diff --git a/cmd/helm/get_manifest.go b/cmd/helm/get_manifest.go index 1860025cd..f332befd9 100644 --- a/cmd/helm/get_manifest.go +++ b/cmd/helm/get_manifest.go @@ -61,7 +61,15 @@ func newGetManifestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command return compListReleases(toComplete, cfg) }) - cmd.Flags().IntVar(&client.Version, "revision", 0, "get the named release with revision") + f := cmd.Flags() + f.IntVar(&client.Version, "revision", 0, "get the named release with revision") + flag := f.Lookup("revision") + completion.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { + if len(args) == 1 { + return compListRevisions(cfg, args[0]) + } + return nil, completion.BashCompDirectiveNoFileComp + }) return cmd } diff --git a/cmd/helm/get_notes.go b/cmd/helm/get_notes.go index de3adf498..4491bd9ba 100644 --- a/cmd/helm/get_notes.go +++ b/cmd/helm/get_notes.go @@ -61,6 +61,13 @@ func newGetNotesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f := cmd.Flags() f.IntVar(&client.Version, "revision", 0, "get the named release with revision") + flag := f.Lookup("revision") + completion.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { + if len(args) == 1 { + return compListRevisions(cfg, args[0]) + } + return nil, completion.BashCompDirectiveNoFileComp + }) return cmd } diff --git a/cmd/helm/get_values.go b/cmd/helm/get_values.go index 898db8929..a8c5acc5e 100644 --- a/cmd/helm/get_values.go +++ b/cmd/helm/get_values.go @@ -65,6 +65,13 @@ func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f := cmd.Flags() f.IntVar(&client.Version, "revision", 0, "get the named release with revision") + flag := f.Lookup("revision") + completion.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { + if len(args) == 1 { + return compListRevisions(cfg, args[0]) + } + return nil, completion.BashCompDirectiveNoFileComp + }) f.BoolVarP(&client.AllValues, "all", "a", false, "dump all (computed) values") bindOutputFlag(cmd, &outfmt) diff --git a/cmd/helm/history.go b/cmd/helm/history.go index 739848c1c..3ef542e58 100644 --- a/cmd/helm/history.go +++ b/cmd/helm/history.go @@ -19,6 +19,7 @@ package main import ( "fmt" "io" + "strconv" "time" "github.com/gosuri/uitable" @@ -185,3 +186,16 @@ func min(x, y int) int { } return y } + +func compListRevisions(cfg *action.Configuration, releaseName string) ([]string, completion.BashCompDirective) { + client := action.NewHistory(cfg) + + var revisions []string + if hist, err := client.Run(releaseName); err == nil { + for _, release := range hist { + revisions = append(revisions, strconv.Itoa(release.Version)) + } + return revisions, completion.BashCompDirectiveDefault + } + return nil, completion.BashCompDirectiveError +} diff --git a/cmd/helm/status.go b/cmd/helm/status.go index 8f172f66a..34543c6cb 100644 --- a/cmd/helm/status.go +++ b/cmd/helm/status.go @@ -75,7 +75,16 @@ func newStatusCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { }) f := cmd.PersistentFlags() + f.IntVar(&client.Version, "revision", 0, "if set, display the status of the named release with revision") + flag := f.Lookup("revision") + completion.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { + if len(args) == 1 { + return compListRevisions(cfg, args[0]) + } + return nil, completion.BashCompDirectiveNoFileComp + }) + bindOutputFlag(cmd, &outfmt) return cmd