Skip to content

Commit

Permalink
Report an error if no flag(s) set in service update (#318)
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](#286)
  • Loading branch information
Gong Zhang authored and knative-prow-robot committed Jul 31, 2019
1 parent 9f203f2 commit 8fbb51f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/kn/commands/service/service_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,10 +91,21 @@ 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)
editFlags.AddUpdateFlags(serviceUpdateCommand)
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
}
20 changes: 20 additions & 0 deletions pkg/kn/commands/service/service_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 8fbb51f

Please sign in to comment.