Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for -A variant of --all-namespaces #356

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cmd/kn_revision_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_route_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_service_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions pkg/kn/commands/namespaced.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
Expand All @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions pkg/kn/commands/namespaced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func TestGetNamespaceAllNamespacesSet(t *testing.T) {
if actualNamespace != expectedNamespace {
t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace)
}

// Now do it again using -A instead
testCmd.SetArgs([]string{"--namespace", sampleNamespace, "-A"})
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
Expand All @@ -95,6 +107,18 @@ func TestGetNamespaceDefaultAllNamespacesUnset(t *testing.T) {
if actualNamespace != expectedNamespace {
t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace)
}

// Now do it again using -A instead
testCmd.SetArgs([]string{"-A"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make those tests in a loop like in

for _, arg := range []string { "--all-namespaces", "-A" } {
   ....

}

This would avoid copy & paste code. Same for the other test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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
Expand Down