Skip to content

Commit

Permalink
Report an error if no flag(s) set in service update
Browse files Browse the repository at this point in the history
For now if no flag(s) set, service update will still try to
do an update, it should return an error instead.

[issue 286](knative#286)
  • Loading branch information
Gong Zhang committed Jul 30, 2019
1 parent 39d9947 commit bb0b714
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/kn/commands/service/service_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package service

import (
"errors"
"fmt"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
Expand All @@ -26,7 +27,7 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
var editFlags ConfigurationEditFlags

serviceUpdateCommand := &cobra.Command{
Use: "update NAME",
Use: "update NAME [flags]",
Short: "Update a service.",
Example: `
# Updates a service 'mysvc' with new environment variables
Expand All @@ -39,6 +40,10 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
return errors.New("requires the service name.")
}

if cmd.Flags().NFlag() == 0 {
return fmt.Errorf("Flag(s) not set\nUsage: %s", cmd.Use)
}

namespace, err := commands.GetNamespace(cmd)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions pkg/kn/commands/service/service_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"testing"

"github.com/knative/client/pkg/kn/commands"
Expand Down Expand Up @@ -63,6 +64,26 @@ func fakeServiceUpdate(original *v1alpha1.Service, args []string) (
return
}

func TestServcieUpdateNoFlags(t *testing.T) {
orig := newEmptyService()

action, _, _, err := fakeServiceUpdate(orig, []string{
"service", "update", "foo"})

if action != nil {
t.Errorf("Unexpected action if no flag(s) set")
}

if err != nil {
expectedErrMsg := "Flag(s) not set"
if !strings.Contains(err.Error(), expectedErrMsg) {
t.Errorf("Missing %s in %s", expectedErrMsg, err.Error())
}
} else {
t.Errorf("Error is expected if no flag(s) set")
}
}

func TestServiceUpdateImage(t *testing.T) {
orig := newEmptyService()

Expand Down

0 comments on commit bb0b714

Please sign in to comment.