From bb0b714f5c8da3b18974fe4eea04baeb92e84427 Mon Sep 17 00:00:00 2001 From: Gong Zhang Date: Mon, 29 Jul 2019 17:39:19 +0800 Subject: [PATCH] Report an error if no flag(s) set in service update For now if no flag(s) set, service update will still try to do an update, it should return an error instead. [issue 286](https://github.com/knative/client/issues/286) --- pkg/kn/commands/service/service_update.go | 7 ++++++- .../commands/service/service_update_test.go | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/kn/commands/service/service_update.go b/pkg/kn/commands/service/service_update.go index 88eeb27669..e10000ec65 100644 --- a/pkg/kn/commands/service/service_update.go +++ b/pkg/kn/commands/service/service_update.go @@ -16,6 +16,7 @@ package service import ( "errors" + "fmt" "github.com/knative/client/pkg/kn/commands" "github.com/spf13/cobra" @@ -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 @@ -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 diff --git a/pkg/kn/commands/service/service_update_test.go b/pkg/kn/commands/service/service_update_test.go index fc1cd87d5e..0f1bb72747 100644 --- a/pkg/kn/commands/service/service_update_test.go +++ b/pkg/kn/commands/service/service_update_test.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "reflect" + "strings" "testing" "github.com/knative/client/pkg/kn/commands" @@ -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()