From 74b3e10f761a414bb3285d66a35787677c5acac9 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Fri, 9 Aug 2019 10:01:05 -0400 Subject: [PATCH] Add support for -A variant of --all-namespaces (#356) * Add support for -A variant of --all-namespaces -A is my newest favorite kubectl flag, it's so much easier/faster to type than --all-namespaces. kn users should benefit from it too! Signed-off-by: Doug Davis * tweak tests Signed-off-by: Doug Davis --- docs/cmd/kn_revision_list.md | 2 +- docs/cmd/kn_route_list.md | 2 +- docs/cmd/kn_service_list.md | 2 +- pkg/kn/commands/namespaced.go | 5 ++-- pkg/kn/commands/namespaced_test.go | 46 ++++++++++++++++++------------ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/docs/cmd/kn_revision_list.md b/docs/cmd/kn_revision_list.md index fbf90f2da..3e658c857 100644 --- a/docs/cmd/kn_revision_list.md +++ b/docs/cmd/kn_revision_list.md @@ -30,7 +30,7 @@ kn revision list [name] [flags] ### Options ``` - --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. + -A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for list -n, --namespace string List the requested object(s) in given namespace. diff --git a/docs/cmd/kn_route_list.md b/docs/cmd/kn_route_list.md index 36d4ef59c..4391699a2 100644 --- a/docs/cmd/kn_route_list.md +++ b/docs/cmd/kn_route_list.md @@ -27,7 +27,7 @@ kn route list NAME [flags] ### Options ``` - --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. + -A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for list -n, --namespace string List the requested object(s) in given namespace. diff --git a/docs/cmd/kn_service_list.md b/docs/cmd/kn_service_list.md index 5f66edb79..3d230321e 100644 --- a/docs/cmd/kn_service_list.md +++ b/docs/cmd/kn_service_list.md @@ -27,7 +27,7 @@ kn service list [name] [flags] ### Options ``` - --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. + -A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for list -n, --namespace string List the requested object(s) in given namespace. diff --git a/pkg/kn/commands/namespaced.go b/pkg/kn/commands/namespaced.go index 62b2a6303..4e37c3a6f 100644 --- a/pkg/kn/commands/namespaced.go +++ b/pkg/kn/commands/namespaced.go @@ -32,8 +32,9 @@ func AddNamespaceFlags(flags *pflag.FlagSet, allowAll bool) { ) if allowAll { - flags.Bool( + flags.BoolP( "all-namespaces", + "A", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.", ) @@ -43,7 +44,7 @@ func AddNamespaceFlags(flags *pflag.FlagSet, allowAll bool) { // GetNamespace returns namespace from command specified by flag func (params *KnParams) GetNamespace(cmd *cobra.Command) (string, error) { namespace := cmd.Flag("namespace").Value.String() - // check value of all-namepace only if its defined + // check value of all-namepaces only if its defined if cmd.Flags().Lookup("all-namespaces") != nil { all, err := cmd.Flags().GetBool("all-namespaces") if err != nil { diff --git a/pkg/kn/commands/namespaced_test.go b/pkg/kn/commands/namespaced_test.go index c847f0535..04ee331d1 100644 --- a/pkg/kn/commands/namespaced_test.go +++ b/pkg/kn/commands/namespaced_test.go @@ -68,16 +68,21 @@ func TestGetNamespaceAllNamespacesSet(t *testing.T) { testCmd := testCommandGenerator(true) expectedNamespace := "" sampleNamespace := "test1" - testCmd.SetArgs([]string{"--namespace", sampleNamespace, "--all-namespaces"}) - testCmd.Execute() - kp := &KnParams{fixedCurrentNamespace: FakeNamespace} - actualNamespace, err := kp.GetNamespace(testCmd) - if err != nil { - t.Fatal(err) - } - if actualNamespace != expectedNamespace { - t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace) + + // Test both variants of the "all namespaces" flag + for _, arg := range []string{"--all-namespaces", "-A"} { + testCmd.SetArgs([]string{"--namespace", sampleNamespace, arg}) + testCmd.Execute() + kp := &KnParams{fixedCurrentNamespace: FakeNamespace} + actualNamespace, err := kp.GetNamespace(testCmd) + if err != nil { + t.Fatal(err) + } + if actualNamespace != expectedNamespace { + t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace) + } } + } // test with all-namespace flag set without any namespace flag set @@ -85,16 +90,21 @@ func TestGetNamespaceAllNamespacesSet(t *testing.T) { func TestGetNamespaceDefaultAllNamespacesUnset(t *testing.T) { testCmd := testCommandGenerator(true) expectedNamespace := "" - testCmd.SetArgs([]string{"--all-namespaces"}) - testCmd.Execute() - kp := &KnParams{fixedCurrentNamespace: FakeNamespace} - actualNamespace, err := kp.GetNamespace(testCmd) - if err != nil { - t.Fatal(err) - } - if actualNamespace != expectedNamespace { - t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace) + + // Test both variants of the "all namespaces" flag + for _, arg := range []string{"--all-namespaces", "-A"} { + testCmd.SetArgs([]string{arg}) + testCmd.Execute() + kp := &KnParams{fixedCurrentNamespace: FakeNamespace} + actualNamespace, err := kp.GetNamespace(testCmd) + if err != nil { + t.Fatal(err) + } + if actualNamespace != expectedNamespace { + t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace) + } } + } // test with all-namespaces flag not defined for command