From e62f5cac1e2a620f8bfb252bfa32f43d25e6c1cf Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Thu, 13 Jan 2022 15:19:02 -0500 Subject: [PATCH] feat(comp): Shell completion for OCI Signed-off-by: Marc Khouzam --- cmd/helm/push.go | 18 ++++++++++++++++++ cmd/helm/push_test.go | 27 +++++++++++++++++++++++++++ cmd/helm/registry_login.go | 9 +++++---- cmd/helm/registry_login_test.go | 25 +++++++++++++++++++++++++ cmd/helm/registry_logout.go | 9 +++++---- cmd/helm/registry_logout_test.go | 25 +++++++++++++++++++++++++ cmd/helm/search_repo.go | 2 +- 7 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 cmd/helm/push_test.go create mode 100644 cmd/helm/registry_login_test.go create mode 100644 cmd/helm/registry_logout_test.go diff --git a/cmd/helm/push.go b/cmd/helm/push.go index ed5d1699a..d2cf2693e 100644 --- a/cmd/helm/push.go +++ b/cmd/helm/push.go @@ -24,6 +24,7 @@ import ( "helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/pusher" ) const pushDesc = ` @@ -41,6 +42,23 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Short: "push a chart to remote", Long: pushDesc, Args: require.MinimumNArgs(2), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + // Do file completion for the chart file to push + return nil, cobra.ShellCompDirectiveDefault + } + if len(args) == 1 { + providers := []pusher.Provider(pusher.All(settings)) + var comps []string + for _, p := range providers { + for _, scheme := range p.Schemes { + comps = append(comps, fmt.Sprintf("%s://", scheme)) + } + } + return comps, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace + } + return nil, cobra.ShellCompDirectiveNoFileComp + }, RunE: func(cmd *cobra.Command, args []string) error { chartRef := args[0] remote := args[1] diff --git a/cmd/helm/push_test.go b/cmd/helm/push_test.go new file mode 100644 index 000000000..8e56d99dc --- /dev/null +++ b/cmd/helm/push_test.go @@ -0,0 +1,27 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "testing" +) + +func TestPushFileCompletion(t *testing.T) { + checkFileCompletion(t, "push", true) + checkFileCompletion(t, "push package.tgz", false) + checkFileCompletion(t, "push package.tgz oci://localhost:5000", false) +} diff --git a/cmd/helm/registry_login.go b/cmd/helm/registry_login.go index 84f9f27b5..8abf91e77 100644 --- a/cmd/helm/registry_login.go +++ b/cmd/helm/registry_login.go @@ -41,10 +41,11 @@ func newRegistryLoginCmd(cfg *action.Configuration, out io.Writer) *cobra.Comman var passwordFromStdinOpt, insecureOpt bool cmd := &cobra.Command{ - Use: "login [host]", - Short: "login to a registry", - Long: registryLoginDesc, - Args: require.MinimumNArgs(1), + Use: "login [host]", + Short: "login to a registry", + Long: registryLoginDesc, + Args: require.MinimumNArgs(1), + ValidArgsFunction: noCompletions, RunE: func(cmd *cobra.Command, args []string) error { hostname := args[0] diff --git a/cmd/helm/registry_login_test.go b/cmd/helm/registry_login_test.go new file mode 100644 index 000000000..517fe08e1 --- /dev/null +++ b/cmd/helm/registry_login_test.go @@ -0,0 +1,25 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "testing" +) + +func TestRegistryLoginFileCompletion(t *testing.T) { + checkFileCompletion(t, "registry login", false) +} diff --git a/cmd/helm/registry_logout.go b/cmd/helm/registry_logout.go index 8d6a54d97..0084f8c09 100644 --- a/cmd/helm/registry_logout.go +++ b/cmd/helm/registry_logout.go @@ -31,10 +31,11 @@ Remove credentials stored for a remote registry. func newRegistryLogoutCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return &cobra.Command{ - Use: "logout [host]", - Short: "logout from a registry", - Long: registryLogoutDesc, - Args: require.MinimumNArgs(1), + Use: "logout [host]", + Short: "logout from a registry", + Long: registryLogoutDesc, + Args: require.MinimumNArgs(1), + ValidArgsFunction: noCompletions, RunE: func(cmd *cobra.Command, args []string) error { hostname := args[0] return action.NewRegistryLogout(cfg).Run(out, hostname) diff --git a/cmd/helm/registry_logout_test.go b/cmd/helm/registry_logout_test.go new file mode 100644 index 000000000..31f716725 --- /dev/null +++ b/cmd/helm/registry_logout_test.go @@ -0,0 +1,25 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "testing" +) + +func TestRegistryLogoutFileCompletion(t *testing.T) { + checkFileCompletion(t, "registry logout", false) +} diff --git a/cmd/helm/search_repo.go b/cmd/helm/search_repo.go index 34232fcfa..f794f6bca 100644 --- a/cmd/helm/search_repo.go +++ b/cmd/helm/search_repo.go @@ -326,7 +326,7 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell cobra.CompDebugln(fmt.Sprintf("Completions after repos: %v", completions), settings.Debug) // Now handle completions for url prefixes - for _, url := range []string{"https://\tChart URL prefix", "http://\tChart URL prefix", "file://\tChart local URL prefix"} { + for _, url := range []string{"oci://\tChart OCI prefix", "https://\tChart URL prefix", "http://\tChart URL prefix", "file://\tChart local URL prefix"} { if strings.HasPrefix(toComplete, url) { // The user already put in the full url prefix; we don't have // anything to add, but make sure the shell does not default