Merge pull request #9243 from VilledeMontreal/feat/compDescriptions

Add descriptions to custom completions (part 1)
pull/9411/head
Marc Khouzam 5 years ago committed by GitHub
commit ea5d5b64b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,9 +29,14 @@ import (
func checkFileCompletion(t *testing.T, cmdName string, shouldBePerformed bool) { func checkFileCompletion(t *testing.T, cmdName string, shouldBePerformed bool) {
storage := storageFixture() storage := storageFixture()
storage.Create(&release.Release{ storage.Create(&release.Release{
Name: "myrelease", Name: "myrelease",
Info: &release.Info{Status: release.StatusDeployed}, Info: &release.Info{Status: release.StatusDeployed},
Chart: &chart.Chart{}, Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: "Myrelease-Chart",
Version: "1.2.3",
},
},
Version: 1, Version: 1,
}) })

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"log" "log"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -66,11 +67,14 @@ func bindOutputFlag(cmd *cobra.Command, varRef *output.Format) {
err := cmd.RegisterFlagCompletionFunc(outputFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { err := cmd.RegisterFlagCompletionFunc(outputFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var formatNames []string var formatNames []string
for _, format := range output.Formats() { for format, desc := range output.FormatsWithDesc() {
if strings.HasPrefix(format, toComplete) { if strings.HasPrefix(format, toComplete) {
formatNames = append(formatNames, format) formatNames = append(formatNames, fmt.Sprintf("%s\t%s", format, desc))
} }
} }
// Sort the results to get a deterministic order for the tests
sort.Strings(formatNames)
return formatNames, cobra.ShellCompDirectiveNoFileComp return formatNames, cobra.ShellCompDirectiveNoFileComp
}) })
@ -150,7 +154,21 @@ func compVersionFlag(chartRef string, toComplete string) ([]string, cobra.ShellC
for _, details := range indexFile.Entries[chartName] { for _, details := range indexFile.Entries[chartName] {
version := details.Metadata.Version version := details.Metadata.Version
if strings.HasPrefix(version, toComplete) { if strings.HasPrefix(version, toComplete) {
versions = append(versions, version) appVersion := details.Metadata.AppVersion
appVersionDesc := ""
if appVersion != "" {
appVersionDesc = fmt.Sprintf("App: %s, ", appVersion)
}
created := details.Created.Format("January 2, 2006")
createdDesc := ""
if created != "" {
createdDesc = fmt.Sprintf("Created: %s ", created)
}
deprecated := ""
if details.Metadata.Deprecated {
deprecated = "(deprecated)"
}
versions = append(versions, fmt.Sprintf("%s\t%s%s%s", version, appVersionDesc, createdDesc, deprecated))
} }
} }
} }

@ -193,7 +193,9 @@ func compListRevisions(toComplete string, cfg *action.Configuration, releaseName
for _, release := range hist { for _, release := range hist {
version := strconv.Itoa(release.Version) version := strconv.Itoa(release.Version)
if strings.HasPrefix(version, toComplete) { if strings.HasPrefix(version, toComplete) {
revisions = append(revisions, version) appVersion := fmt.Sprintf("App: %s", release.Chart.Metadata.AppVersion)
chartDesc := fmt.Sprintf("Chart: %s-%s", release.Chart.Metadata.Name, release.Chart.Metadata.Version)
revisions = append(revisions, fmt.Sprintf("%s\t%s, %s", version, appVersion, chartDesc))
} }
} }
return revisions, cobra.ShellCompDirectiveNoFileComp return revisions, cobra.ShellCompDirectiveNoFileComp

@ -203,14 +203,15 @@ func compListReleases(toComplete string, cfg *action.Configuration) ([]string, c
client.Filter = fmt.Sprintf("^%s", toComplete) client.Filter = fmt.Sprintf("^%s", toComplete)
client.SetStateMask() client.SetStateMask()
results, err := client.Run() releases, err := client.Run()
if err != nil { if err != nil {
return nil, cobra.ShellCompDirectiveDefault return nil, cobra.ShellCompDirectiveDefault
} }
var choices []string var choices []string
for _, res := range results { for _, rel := range releases {
choices = append(choices, res.Name) choices = append(choices,
fmt.Sprintf("%s\t%s-%s -> %s", rel.Name, rel.Chart.Metadata.Name, rel.Chart.Metadata.Version, rel.Info.Status.String()))
} }
return choices, cobra.ShellCompDirectiveNoFileComp return choices, cobra.ShellCompDirectiveNoFileComp

@ -83,7 +83,7 @@ func compListPlugins(toComplete string, ignoredPluginNames []string) []string {
filteredPlugins := filterPlugins(plugins, ignoredPluginNames) filteredPlugins := filterPlugins(plugins, ignoredPluginNames)
for _, p := range filteredPlugins { for _, p := range filteredPlugins {
if strings.HasPrefix(p.Metadata.Name, toComplete) { if strings.HasPrefix(p.Metadata.Name, toComplete) {
pNames = append(pNames, p.Metadata.Name) pNames = append(pNames, fmt.Sprintf("%s\t%s", p.Metadata.Name, p.Metadata.Usage))
} }
} }
} }

@ -131,13 +131,13 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
if config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( if config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules, loadingRules,
&clientcmd.ConfigOverrides{}).RawConfig(); err == nil { &clientcmd.ConfigOverrides{}).RawConfig(); err == nil {
ctxs := []string{} comps := []string{}
for name := range config.Contexts { for name, context := range config.Contexts {
if strings.HasPrefix(name, toComplete) { if strings.HasPrefix(name, toComplete) {
ctxs = append(ctxs, name) comps = append(comps, fmt.Sprintf("%s\t%s", name, context.Cluster))
} }
} }
return ctxs, cobra.ShellCompDirectiveNoFileComp return comps, cobra.ShellCompDirectiveNoFileComp
} }
return nil, cobra.ShellCompDirectiveNoFileComp return nil, cobra.ShellCompDirectiveNoFileComp
}) })

@ -118,56 +118,72 @@ func mustParseTime(t string) helmtime.Time {
} }
func TestStatusCompletion(t *testing.T) { func TestStatusCompletion(t *testing.T) {
releasesMockWithStatus := func(info *release.Info, hooks ...*release.Hook) []*release.Release { rels := []*release.Release{
info.LastDeployed = helmtime.Unix(1452902400, 0).UTC() {
return []*release.Release{{
Name: "athos", Name: "athos",
Namespace: "default", Namespace: "default",
Info: info, Info: &release.Info{
Chart: &chart.Chart{}, Status: release.StatusDeployed,
Hooks: hooks, },
Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: "Athos-chart",
Version: "1.2.3",
},
},
}, { }, {
Name: "porthos", Name: "porthos",
Namespace: "default", Namespace: "default",
Info: info, Info: &release.Info{
Chart: &chart.Chart{}, Status: release.StatusFailed,
Hooks: hooks, },
Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: "Porthos-chart",
Version: "111.222.333",
},
},
}, { }, {
Name: "aramis", Name: "aramis",
Namespace: "default", Namespace: "default",
Info: info, Info: &release.Info{
Chart: &chart.Chart{}, Status: release.StatusUninstalled,
Hooks: hooks, },
Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: "Aramis-chart",
Version: "0.0.0",
},
},
}, { }, {
Name: "dartagnan", Name: "dartagnan",
Namespace: "gascony", Namespace: "gascony",
Info: info, Info: &release.Info{
Chart: &chart.Chart{}, Status: release.StatusUnknown,
Hooks: hooks, },
Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: "Dartagnan-chart",
Version: "1.2.3-prerelease",
},
},
}} }}
}
tests := []cmdTestCase{{ tests := []cmdTestCase{{
name: "completion for status", name: "completion for status",
cmd: "__complete status a", cmd: "__complete status a",
golden: "output/status-comp.txt", golden: "output/status-comp.txt",
rels: releasesMockWithStatus(&release.Info{ rels: rels,
Status: release.StatusDeployed,
}),
}, { }, {
name: "completion for status with too many arguments", name: "completion for status with too many arguments",
cmd: "__complete status dartagnan ''", cmd: "__complete status dartagnan ''",
golden: "output/status-wrong-args-comp.txt", golden: "output/status-wrong-args-comp.txt",
rels: releasesMockWithStatus(&release.Info{ rels: rels,
Status: release.StatusDeployed,
}),
}, { }, {
name: "completion for status with too many arguments", name: "completion for status with global flag",
cmd: "__complete status --debug a", cmd: "__complete status --debug a",
golden: "output/status-comp.txt", golden: "output/status-comp.txt",
rels: releasesMockWithStatus(&release.Info{ rels: rels,
Status: release.StatusDeployed,
}),
}} }}
runTestCmd(t, tests) runTestCmd(t, tests)
} }

@ -4,6 +4,8 @@ entries:
- name: alpine - name: alpine
url: https://charts.helm.sh/stable/alpine-0.1.0.tgz url: https://charts.helm.sh/stable/alpine-0.1.0.tgz
checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d
created: "2018-06-27T10:00:18.230700509Z"
deprecated: true
home: https://helm.sh/helm home: https://helm.sh/helm
sources: sources:
- https://github.com/helm/helm - https://github.com/helm/helm
@ -17,6 +19,7 @@ entries:
- name: alpine - name: alpine
url: https://charts.helm.sh/stable/alpine-0.2.0.tgz url: https://charts.helm.sh/stable/alpine-0.2.0.tgz
checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d
created: "2018-07-09T11:34:37.797864902Z"
home: https://helm.sh/helm home: https://helm.sh/helm
sources: sources:
- https://github.com/helm/helm - https://github.com/helm/helm
@ -30,6 +33,7 @@ entries:
- name: alpine - name: alpine
url: https://charts.helm.sh/stable/alpine-0.3.0-rc.1.tgz url: https://charts.helm.sh/stable/alpine-0.3.0-rc.1.tgz
checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d
created: "2020-11-12T08:44:58.872726222Z"
home: https://helm.sh/helm home: https://helm.sh/helm
sources: sources:
- https://github.com/helm/helm - https://github.com/helm/helm
@ -44,6 +48,7 @@ entries:
- name: mariadb - name: mariadb
url: https://charts.helm.sh/stable/mariadb-0.3.0.tgz url: https://charts.helm.sh/stable/mariadb-0.3.0.tgz
checksum: 65229f6de44a2be9f215d11dbff311673fc8ba56 checksum: 65229f6de44a2be9f215d11dbff311673fc8ba56
created: "2018-04-23T08:20:27.160959131Z"
home: https://mariadb.org home: https://mariadb.org
sources: sources:
- https://github.com/bitnami/bitnami-docker-mariadb - https://github.com/bitnami/bitnami-docker-mariadb

@ -1,5 +1,5 @@
table json Output result in JSON format
json table Output result in human-readable format
yaml yaml Output result in YAML format
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,7 +1,7 @@
args args echo args
echo echo echo stuff
env env env stuff
exitwith exitwith exitwith code
fullenv fullenv show env vars
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,6 +1,6 @@
echo echo echo stuff
env env env stuff
exitwith exitwith exitwith code
fullenv fullenv show env vars
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,6 +1,6 @@
8 8 App: 1.0, Chart: foo-0.1.0-beta.1
9 9 App: 1.0, Chart: foo-0.1.0-beta.1
10 10 App: 1.0, Chart: foo-0.1.0-beta.1
11 11 App: 1.0, Chart: foo-0.1.0-beta.1
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,4 +1,4 @@
carabins carabins foo-0.1.0-beta.1 -> superseded
musketeers musketeers foo-0.1.0-beta.1 -> deployed
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,4 +1,4 @@
aramis aramis Aramis-chart-0.0.0 -> uninstalled
athos athos Athos-chart-1.2.3 -> deployed
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -1,5 +1,5 @@
0.3.0-rc.1 0.3.0-rc.1 App: 3.0.0, Created: November 12, 2020
0.2.0 0.2.0 App: 2.3.4, Created: July 9, 2018
0.1.0 0.1.0 App: 1.2.3, Created: June 27, 2018 (deprecated)
:4 :4
Completion ended with directive: ShellCompDirectiveNoFileComp Completion ended with directive: ShellCompDirectiveNoFileComp

@ -40,6 +40,16 @@ func Formats() []string {
return []string{Table.String(), JSON.String(), YAML.String()} return []string{Table.String(), JSON.String(), YAML.String()}
} }
// FormatsWithDesc returns a list of the string representation of the supported formats
// including a description
func FormatsWithDesc() map[string]string {
return map[string]string{
Table.String(): "Output result in human-readable format",
JSON.String(): "Output result in JSON format",
YAML.String(): "Output result in YAML format",
}
}
// ErrInvalidFormatType is returned when an unsupported format type is used // ErrInvalidFormatType is returned when an unsupported format type is used
var ErrInvalidFormatType = fmt.Errorf("invalid format type") var ErrInvalidFormatType = fmt.Errorf("invalid format type")

Loading…
Cancel
Save