From 8fbb51f92aaf379cf29c4406ba8bca8631850ae6 Mon Sep 17 00:00:00 2001 From: Gong Zhang Date: Thu, 1 Aug 2019 04:49:44 +0800 Subject: [PATCH] Report an error if no flag(s) set in service update (#318) 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 | 13 +++++++++++- .../commands/service/service_update_test.go | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/kn/commands/service/service_update.go b/pkg/kn/commands/service/service_update.go index 39260977ef..c0aac2bda8 100644 --- a/pkg/kn/commands/service/service_update.go +++ b/pkg/kn/commands/service/service_update.go @@ -29,7 +29,7 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command { var waitFlags commands.WaitFlags serviceUpdateCommand := &cobra.Command{ - Use: "update NAME", + Use: "update NAME [flags]", Short: "Update a service.", Example: ` # Updates a service 'mysvc' with new environment variables @@ -91,6 +91,9 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command { return nil } }, + PreRunE: func(cmd *cobra.Command, args []string) error { + return preCheck(cmd, args) + }, } commands.AddNamespaceFlags(serviceUpdateCommand.Flags(), false) @@ -98,3 +101,11 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command { waitFlags.AddConditionWaitFlags(serviceUpdateCommand, 60, "Update", "service") return serviceUpdateCommand } + +func preCheck(cmd *cobra.Command, args []string) error { + if cmd.Flags().NFlag() == 0 { + return errors.New(fmt.Sprintf("flag(s) not set\nUsage: %s", cmd.Use)) + } + + return nil +} diff --git a/pkg/kn/commands/service/service_update_test.go b/pkg/kn/commands/service/service_update_test.go index 76aed4bae2..3a832447d6 100644 --- a/pkg/kn/commands/service/service_update_test.go +++ b/pkg/kn/commands/service/service_update_test.go @@ -88,6 +88,26 @@ func fakeServiceUpdate(original *v1alpha1.Service, args []string, sync bool) ( return } +func TestServcieUpdateNoFlags(t *testing.T) { + orig := newEmptyService() + + action, _, _, err := fakeServiceUpdate(orig, []string{ + "service", "update", "foo"}, false) + + if action != nil { + t.Errorf("Unexpected action if no flag(s) set") + } + + if err == nil { + t.Fatal(err) + } + + expectedErrMsg := "flag(s) not set" + if !strings.Contains(err.Error(), expectedErrMsg) { + t.Fatalf("Missing %s in %s", expectedErrMsg, err.Error()) + } +} + func TestServiceUpdateImageSync(t *testing.T) { orig := newEmptyService()