Browse code

Show usage when `docker swarm update` has no flags

This fix tries to address the issue raised in 24352. Previously,
when `docker swarm update` has no flags, the output is
```
Swarm updated.
```
even though nothing was updated. This could be misleading for
users.

This fix tries to address the issue by adding a `PreRunE` function
in the command so that in case no flag is provided (`cmd.Flags().NFlag() == 0`),
the usage will be outputed instead.

An integration has been added to cover the changes.

This fix fixes 24352.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/12/01 06:23:18
Showing 2 changed files
... ...
@@ -23,6 +23,12 @@ func newUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
23 23
 		RunE: func(cmd *cobra.Command, args []string) error {
24 24
 			return runUpdate(dockerCli, cmd.Flags(), opts)
25 25
 		},
26
+		PreRunE: func(cmd *cobra.Command, args []string) error {
27
+			if cmd.Flags().NFlag() == 0 {
28
+				return pflag.ErrHelp
29
+			}
30
+			return nil
31
+		},
26 32
 	}
27 33
 
28 34
 	cmd.Flags().BoolVar(&opts.autolock, flagAutolock, false, "Change manager autolocking setting (true|false)")
... ...
@@ -1358,3 +1358,21 @@ func (s *DockerSwarmSuite) TestSwarmNetworkIPAMOptions(c *check.C) {
1358 1358
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1359 1359
 	c.Assert(strings.TrimSpace(out), checker.Equals, "map[foo:bar]")
1360 1360
 }
1361
+
1362
+// TODO: migrate to a unit test
1363
+// This test could be migrated to unit test and save costly integration test,
1364
+// once PR #29143 is merged.
1365
+func (s *DockerSwarmSuite) TestSwarmUpdateWithoutArgs(c *check.C) {
1366
+	d := s.AddDaemon(c, true, true)
1367
+
1368
+	expectedOutput := `
1369
+Usage:	docker swarm update [OPTIONS]
1370
+
1371
+Update the swarm
1372
+
1373
+Options:`
1374
+
1375
+	out, err := d.Cmd("swarm", "update")
1376
+	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
1377
+	c.Assert(out, checker.Contains, expectedOutput, check.Commentf(out))
1378
+}