From dd02b819649b603f71da8fb300a2cbabeb172c3e 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 | 23 +++++++++++++++++-- .../commands/service/service_update_test.go | 20 ++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pkg/kn/commands/service/service_update.go b/pkg/kn/commands/service/service_update.go index 39260977ef..37ea77b112 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 @@ -44,12 +44,20 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command { if len(args) != 1 { return errors.New("requires the service name.") } - + namespace, err := p.GetNamespace(cmd) if err != nil { return err } + if cmd.Flags().NFlag() == 0 { + return errors.New(fmt.Sprintf("Flag(s) not set\n See usage: %s", cmd.Use)) + } + + if cmd.Flags().NFlag() == 0 { + return fmt.Errorf("Flag(s) not set\nUsage: %s", cmd.Use) + } + client, err := p.NewClient(namespace) if err != nil { return err @@ -91,6 +99,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 +109,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()