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,7 +68,10 @@ func TestGetNamespaceAllNamespacesSet(t *testing.T) {
testCmd := testCommandGenerator(true) testCmd := testCommandGenerator(true)
expectedNamespace := "" expectedNamespace := ""
sampleNamespace := "test1" sampleNamespace := "test1"
testCmd.SetArgs([]string{"--namespace", sampleNamespace, "--all-namespaces"})
// Test both variants of the "all namespaces" flag
for _, arg := range []string{"--all-namespaces", "-A"} {
testCmd.SetArgs([]string{"--namespace", sampleNamespace, arg})
testCmd.Execute() testCmd.Execute()
kp := &KnParams{fixedCurrentNamespace: FakeNamespace} kp := &KnParams{fixedCurrentNamespace: FakeNamespace}
actualNamespace, err := kp.GetNamespace(testCmd) actualNamespace, err := kp.GetNamespace(testCmd)
@ -80,12 +83,17 @@ func TestGetNamespaceAllNamespacesSet(t *testing.T) {
} }
} }
}
// test with all-namespace flag set without any namespace flag set // test with all-namespace flag set without any namespace flag set
// all-namespace flag takes precendence // all-namespace flag takes precendence
func TestGetNamespaceDefaultAllNamespacesUnset(t *testing.T) { func TestGetNamespaceDefaultAllNamespacesUnset(t *testing.T) {
testCmd := testCommandGenerator(true) testCmd := testCommandGenerator(true)
expectedNamespace := "" expectedNamespace := ""
testCmd.SetArgs([]string{"--all-namespaces"})
// Test both variants of the "all namespaces" flag
for _, arg := range []string{"--all-namespaces", "-A"} {
testCmd.SetArgs([]string{arg})
testCmd.Execute() testCmd.Execute()
kp := &KnParams{fixedCurrentNamespace: FakeNamespace} kp := &KnParams{fixedCurrentNamespace: FakeNamespace}
actualNamespace, err := kp.GetNamespace(testCmd) actualNamespace, err := kp.GetNamespace(testCmd)
@ -97,6 +105,8 @@ func TestGetNamespaceDefaultAllNamespacesUnset(t *testing.T) {
} }
} }
}
// test with all-namespaces flag not defined for command // test with all-namespaces flag not defined for command
func TestGetNamespaceAllNamespacesNotDefined(t *testing.T) { func TestGetNamespaceAllNamespacesNotDefined(t *testing.T) {
testCmd := testCommandGenerator(false) testCmd := testCommandGenerator(false)