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 <dug@us.ibm.com>

* tweak tests

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2019-08-09 10:01:05 -04:00 committed by Knative Prow Robot
parent 8c362f0363
commit 74b3e10f76
5 changed files with 34 additions and 23 deletions

View File

@ -30,7 +30,7 @@ kn revision list [name] [flags]
### Options ### 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) --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 -h, --help help for list
-n, --namespace string List the requested object(s) in given namespace. -n, --namespace string List the requested object(s) in given namespace.

View File

@ -27,7 +27,7 @@ kn route list NAME [flags]
### Options ### 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) --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 -h, --help help for list
-n, --namespace string List the requested object(s) in given namespace. -n, --namespace string List the requested object(s) in given namespace.

View File

@ -27,7 +27,7 @@ kn service list [name] [flags]
### Options ### 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) --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 -h, --help help for list
-n, --namespace string List the requested object(s) in given namespace. -n, --namespace string List the requested object(s) in given namespace.

View File

@ -32,8 +32,9 @@ func AddNamespaceFlags(flags *pflag.FlagSet, allowAll bool) {
) )
if allowAll { if allowAll {
flags.Bool( flags.BoolP(
"all-namespaces", "all-namespaces",
"A",
false, false,
"If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.", "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 // GetNamespace returns namespace from command specified by flag
func (params *KnParams) GetNamespace(cmd *cobra.Command) (string, error) { func (params *KnParams) GetNamespace(cmd *cobra.Command) (string, error) {
namespace := cmd.Flag("namespace").Value.String() 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 { if cmd.Flags().Lookup("all-namespaces") != nil {
all, err := cmd.Flags().GetBool("all-namespaces") all, err := cmd.Flags().GetBool("all-namespaces")
if err != nil { if err != nil {

View File

@ -68,16 +68,21 @@ func TestGetNamespaceAllNamespacesSet(t *testing.T) {
testCmd := testCommandGenerator(true) testCmd := testCommandGenerator(true)
expectedNamespace := "" expectedNamespace := ""
sampleNamespace := "test1" sampleNamespace := "test1"
testCmd.SetArgs([]string{"--namespace", sampleNamespace, "--all-namespaces"})
testCmd.Execute() // Test both variants of the "all namespaces" flag
kp := &KnParams{fixedCurrentNamespace: FakeNamespace} for _, arg := range []string{"--all-namespaces", "-A"} {
actualNamespace, err := kp.GetNamespace(testCmd) testCmd.SetArgs([]string{"--namespace", sampleNamespace, arg})
if err != nil { testCmd.Execute()
t.Fatal(err) kp := &KnParams{fixedCurrentNamespace: FakeNamespace}
} actualNamespace, err := kp.GetNamespace(testCmd)
if actualNamespace != expectedNamespace { if err != nil {
t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace) 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 // 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) { func TestGetNamespaceDefaultAllNamespacesUnset(t *testing.T) {
testCmd := testCommandGenerator(true) testCmd := testCommandGenerator(true)
expectedNamespace := "" expectedNamespace := ""
testCmd.SetArgs([]string{"--all-namespaces"})
testCmd.Execute() // Test both variants of the "all namespaces" flag
kp := &KnParams{fixedCurrentNamespace: FakeNamespace} for _, arg := range []string{"--all-namespaces", "-A"} {
actualNamespace, err := kp.GetNamespace(testCmd) testCmd.SetArgs([]string{arg})
if err != nil { testCmd.Execute()
t.Fatal(err) kp := &KnParams{fixedCurrentNamespace: FakeNamespace}
} actualNamespace, err := kp.GetNamespace(testCmd)
if actualNamespace != expectedNamespace { if err != nil {
t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace) 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 // test with all-namespaces flag not defined for command