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](#286)
  • Loading branch information
Gong Zhang committed Jul 31, 2019
1 parent f797fba commit dd02b81
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
23 changes: 21 additions & 2 deletions 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 All @@ -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
Expand Down Expand Up @@ -91,10 +99,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 dd02b81

Please sign in to comment.