Skip to content

Commit

Permalink
Fixes issue knative#111 by responding with an error for unknown subco…
Browse files Browse the repository at this point in the history
…mmands

```bash
➜  client git:(issue111) ✗ ./kn service unknown
Service command group

Usage:
  kn service [flags]
  kn service [command]

Available Commands:
  create      Create a service.
  delete      Delete a service.
  describe    Describe available services.
  list        List available services.
  update      Update a service.

Flags:
  -h, --help   help for service

Global Flags:
      --kubeconfig string   kubectl config file (default is $HOME/.kube/config)

Use "kn service [command] --help" for more information about a command.
unknown command "unknown" for "kn service"

➜  client git:(issue111) ✗ ./kn revision lol
Revision command group

Usage:
  kn revision [flags]
  kn revision [command]

Available Commands:
  describe    Describe revisions.
  list        List available revisions.

Flags:
  -h, --help   help for revision

Global Flags:
      --kubeconfig string   kubectl config file (default is $HOME/.kube/config)

Use "kn revision [command] --help" for more information about a command.
unknown command "lol" for "kn revision"
```

This works for both the `service` and `revision` groups whe called with empty and unknown sub-command.
  • Loading branch information
maximilien committed Jun 20, 2019
1 parent 8a1d412 commit f27ed5c
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/cmd/kn_revision.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Revision command group

Revision command group

```
kn revision [flags]
```

### Options

```
Expand Down
4 changes: 4 additions & 0 deletions docs/cmd/kn_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Service command group

Service command group

```
kn service [flags]
```

### Options

```
Expand Down
11 changes: 11 additions & 0 deletions pkg/kn/commands/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package revision

import (
"errors"
"fmt"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
)
Expand All @@ -23,6 +26,14 @@ func NewRevisionCommand(p *commands.KnParams) *cobra.Command {
revisionCmd := &cobra.Command{
Use: "revision",
Short: "Revision command group",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
if len(args) == 0 {
return errors.New("please provide a valid sub-command for \"kn revision\"")
} else {
return errors.New(fmt.Sprintf("unknown command \"%s\" for \"kn revision\"", args[0]))
}
},
}
revisionCmd.AddCommand(NewRevisionListCommand(p))
revisionCmd.AddCommand(NewRevisionDescribeCommand(p))
Expand Down
6 changes: 3 additions & 3 deletions pkg/kn/commands/revision/revision_describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"sigs.k8s.io/yaml"
)

func fakeRevision(args []string, response *v1alpha1.Revision) (action client_testing.Action, output string, err error) {
func fakeRevisionDescribe(args []string, response *v1alpha1.Revision) (action client_testing.Action, output string, err error) {
knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewRevisionCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
Expand All @@ -46,7 +46,7 @@ func fakeRevision(args []string, response *v1alpha1.Revision) (action client_tes
}

func TestDescribeRevisionWithNoName(t *testing.T) {
_, _, err := fakeRevision([]string{"revision", "describe"}, &v1alpha1.Revision{})
_, _, err := fakeRevisionDescribe([]string{"revision", "describe"}, &v1alpha1.Revision{})
expectedError := "requires the revision name."
if err == nil || err.Error() != expectedError {
t.Fatal("expect to fail with missing revision name")
Expand All @@ -70,7 +70,7 @@ func TestDescribeRevisionYaml(t *testing.T) {
},
}

action, data, err := fakeRevision([]string{"revision", "describe", "test-rev"}, &expectedRevision)
action, data, err := fakeRevisionDescribe([]string{"revision", "describe", "test-rev"}, &expectedRevision)
if err != nil {
t.Fatal(err)
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/kn/commands/revision/revision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package revision

import (
"strings"
"testing"

"github.com/knative/client/pkg/kn/commands"
v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing"
)

func fakeRevision(args []string, response *v1alpha1.ServiceList) (action client_testing.Action, output []string, err error) {
knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewRevisionCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) {
action = a
return true, response, nil
})
cmd.SetArgs(args)
err = cmd.Execute()
if err != nil {
return
}
output = strings.Split(buf.String(), "\n")
return
}

func TestUnknownSubcommand(t *testing.T) {
_, _, err := fakeRevision([]string{"revision", "unknown"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "unknown command \"unknown\" for \"kn revision\"" {
t.Errorf("Bad error message '%s'", err.Error())
}
}

func TestEmptySubcommand(t *testing.T) {
_, _, err := fakeRevision([]string{"revision"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "please provide a valid sub-command for \"kn revision\"" {
t.Errorf("Bad error message '%s'", err.Error())
}
}
12 changes: 12 additions & 0 deletions pkg/kn/commands/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package service

import (
"errors"
"fmt"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
)
Expand All @@ -23,6 +26,15 @@ func NewServiceCommand(p *commands.KnParams) *cobra.Command {
serviceCmd := &cobra.Command{
Use: "service",
Short: "Service command group",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
if len(args) == 0 {
return errors.New("please provide a valid sub-command for \"kn service\"")
} else {
return errors.New(fmt.Sprintf("unknown command \"%s\" for \"kn service\"", args[0]))
}
return nil
},
}
serviceCmd.AddCommand(NewServiceListCommand(p))
serviceCmd.AddCommand(NewServiceDescribeCommand(p))
Expand Down
66 changes: 66 additions & 0 deletions pkg/kn/commands/service/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package service

import (
"strings"
"testing"

"github.com/knative/client/pkg/kn/commands"
v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing"
)

func fakeService(args []string, response *v1alpha1.ServiceList) (action client_testing.Action, output []string, err error) {
knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewServiceCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) {
action = a
return true, response, nil
})
cmd.SetArgs(args)
err = cmd.Execute()
if err != nil {
return
}
output = strings.Split(buf.String(), "\n")
return
}

func TestUnknownSubcommand(t *testing.T) {
_, _, err := fakeService([]string{"service", "unknown"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "unknown command \"unknown\" for \"kn service\"" {
t.Errorf("Bad error message '%s'", err.Error())
}
}

func TestEmptySubcommand(t *testing.T) {
_, _, err := fakeService([]string{"service"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "please provide a valid sub-command for \"kn service\"" {
t.Errorf("Bad error message '%s'", err.Error())
}
}

0 comments on commit f27ed5c

Please sign in to comment.