Skip to content

Commit 1756355

Browse files
committed
fix: add shell completion for namespace commands
Signed-off-by: Ruihua Wen <[email protected]>
1 parent d775a8c commit 1756355

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

cmd/nerdctl/namespace/namespace_inspect.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package namespace
1919
import (
2020
"github.com/spf13/cobra"
2121

22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2223
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2324
"github.com/containerd/nerdctl/v2/pkg/api/types"
2425
"github.com/containerd/nerdctl/v2/pkg/clientutil"
@@ -27,12 +28,13 @@ import (
2728

2829
func inspectCommand() *cobra.Command {
2930
cmd := &cobra.Command{
30-
Use: "inspect NAMESPACE",
31-
Short: "Display detailed information on one or more namespaces.",
32-
RunE: inspectAction,
33-
Args: cobra.MinimumNArgs(1),
34-
SilenceUsage: true,
35-
SilenceErrors: true,
31+
Use: "inspect NAMESPACE",
32+
Short: "Display detailed information on one or more namespaces.",
33+
RunE: inspectAction,
34+
ValidArgsFunction: namespaceInspectShellComplete,
35+
Args: cobra.MinimumNArgs(1),
36+
SilenceUsage: true,
37+
SilenceErrors: true,
3638
}
3739
cmd.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
3840
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
@@ -71,3 +73,8 @@ func inspectAction(cmd *cobra.Command, args []string) error {
7173

7274
return namespace.Inspect(ctx, client, args, options)
7375
}
76+
77+
func namespaceInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
78+
// show namespace names
79+
return completion.NamespaceNames(cmd, args, toComplete)
80+
}

cmd/nerdctl/namespace/namespace_remove.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package namespace
1919
import (
2020
"github.com/spf13/cobra"
2121

22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2223
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2324
"github.com/containerd/nerdctl/v2/pkg/api/types"
2425
"github.com/containerd/nerdctl/v2/pkg/clientutil"
@@ -27,13 +28,14 @@ import (
2728

2829
func removeCommand() *cobra.Command {
2930
cmd := &cobra.Command{
30-
Use: "remove [flags] NAMESPACE [NAMESPACE...]",
31-
Aliases: []string{"rm"},
32-
Args: cobra.MinimumNArgs(1),
33-
Short: "Remove one or more namespaces",
34-
RunE: removeAction,
35-
SilenceUsage: true,
36-
SilenceErrors: true,
31+
Use: "remove [flags] NAMESPACE [NAMESPACE...]",
32+
Aliases: []string{"rm"},
33+
Args: cobra.MinimumNArgs(1),
34+
Short: "Remove one or more namespaces",
35+
RunE: removeAction,
36+
ValidArgsFunction: namespaceRemoveShellComplete,
37+
SilenceUsage: true,
38+
SilenceErrors: true,
3739
}
3840
cmd.Flags().BoolP("cgroup", "c", false, "delete the namespace's cgroup")
3941
return cmd
@@ -69,3 +71,8 @@ func removeAction(cmd *cobra.Command, args []string) error {
6971

7072
return namespace.Remove(ctx, client, args, options)
7173
}
74+
75+
func namespaceRemoveShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
76+
// show namespace names
77+
return completion.NamespaceNames(cmd, args, toComplete)
78+
}

cmd/nerdctl/namespace/namespace_update.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package namespace
1919
import (
2020
"github.com/spf13/cobra"
2121

22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2223
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2324
"github.com/containerd/nerdctl/v2/pkg/api/types"
2425
"github.com/containerd/nerdctl/v2/pkg/clientutil"
@@ -27,12 +28,13 @@ import (
2728

2829
func updateCommand() *cobra.Command {
2930
cmd := &cobra.Command{
30-
Use: "update [flags] NAMESPACE",
31-
Short: "Update labels for a namespace",
32-
RunE: updateAction,
33-
Args: cobra.MinimumNArgs(1),
34-
SilenceUsage: true,
35-
SilenceErrors: true,
31+
Use: "update [flags] NAMESPACE",
32+
Short: "Update labels for a namespace",
33+
RunE: updateAction,
34+
ValidArgsFunction: namespaceUpdateShellComplete,
35+
Args: cobra.MinimumNArgs(1),
36+
SilenceUsage: true,
37+
SilenceErrors: true,
3638
}
3739
cmd.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace")
3840
return cmd
@@ -67,3 +69,8 @@ func updateAction(cmd *cobra.Command, args []string) error {
6769

6870
return namespace.Update(ctx, client, args[0], options)
6971
}
72+
73+
func namespaceUpdateShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
74+
// show namespace names
75+
return completion.NamespaceNames(cmd, args, toComplete)
76+
}

0 commit comments

Comments
 (0)