Skip to content

Commit b10b79e

Browse files
committed
cli-plugins: minor cleanups: use Println
- use Println to print newline instead of custom format - suppress some errors to make my IDE and linters happier - use res.Assert() with icmd.Expected{} where possible to make assertions not depend on newline / whitespace randomness - use apiClient instead of client for the API client to prevent shadowing imports. - use dockerCLI with Go's standard camelCase casing. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 4d7fe01 commit b10b79e

7 files changed

Lines changed: 41 additions & 37 deletions

File tree

cli-plugins/examples/helloworld/main.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ import (
1212
)
1313

1414
func main() {
15-
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
15+
plugin.Run(func(dockerCLI command.Cli) *cobra.Command {
1616
goodbye := &cobra.Command{
1717
Use: "goodbye",
1818
Short: "Say Goodbye instead of Hello",
1919
Run: func(cmd *cobra.Command, _ []string) {
20-
fmt.Fprintln(dockerCli.Out(), "Goodbye World!")
20+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Goodbye World!")
2121
},
2222
}
2323
apiversion := &cobra.Command{
2424
Use: "apiversion",
2525
Short: "Print the API version of the server",
2626
RunE: func(_ *cobra.Command, _ []string) error {
27-
cli := dockerCli.Client()
28-
ping, err := cli.Ping(context.Background())
27+
apiClient := dockerCLI.Client()
28+
ping, err := apiClient.Ping(context.Background())
2929
if err != nil {
3030
return err
3131
}
32-
fmt.Println(ping.APIVersion)
32+
_, _ = fmt.Println(ping.APIVersion)
3333
return nil
3434
},
3535
}
@@ -38,7 +38,7 @@ func main() {
3838
Use: "exitstatus2",
3939
Short: "Exit with status 2",
4040
RunE: func(_ *cobra.Command, _ []string) error {
41-
fmt.Fprintln(dockerCli.Err(), "Exiting with error status 2")
41+
_, _ = fmt.Fprintln(dockerCLI.Err(), "Exiting with error status 2")
4242
os.Exit(2)
4343
return nil
4444
},
@@ -56,33 +56,33 @@ func main() {
5656
return err
5757
}
5858
if preRun {
59-
fmt.Fprintf(dockerCli.Err(), "Plugin PersistentPreRunE called")
59+
_, _ = fmt.Fprintln(dockerCLI.Err(), "Plugin PersistentPreRunE called")
6060
}
6161
return nil
6262
},
6363
RunE: func(cmd *cobra.Command, args []string) error {
6464
if debug {
65-
fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
65+
_, _ = fmt.Fprintln(dockerCLI.Err(), "Plugin debug mode enabled")
6666
}
6767

6868
switch optContext {
6969
case "Christmas":
70-
fmt.Fprintf(dockerCli.Out(), "Merry Christmas!\n")
70+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Merry Christmas!")
7171
return nil
7272
case "":
7373
// nothing
7474
}
7575

7676
if who == "" {
77-
who, _ = dockerCli.ConfigFile().PluginConfig("helloworld", "who")
77+
who, _ = dockerCLI.ConfigFile().PluginConfig("helloworld", "who")
7878
}
7979
if who == "" {
8080
who = "World"
8181
}
8282

83-
fmt.Fprintf(dockerCli.Out(), "Hello %s!\n", who)
84-
dockerCli.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
85-
return dockerCli.ConfigFile().Save()
83+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Hello", who)
84+
dockerCLI.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
85+
return dockerCLI.ConfigFile().Save()
8686
},
8787
}
8888

cli-plugins/hooks/printer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func PrintNextSteps(out io.Writer, messages []string) {
1111
if len(messages) == 0 {
1212
return
1313
}
14-
fmt.Fprintln(out, aec.Bold.Apply("\nWhat's next:"))
14+
_, _ = fmt.Fprintln(out, aec.Bold.Apply("\nWhat's next:"))
1515
for _, n := range messages {
16-
_, _ = fmt.Fprintf(out, " %s\n", n)
16+
_, _ = fmt.Fprintln(out, " ", n)
1717
}
1818
}

e2e/cli-plugins/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestConfig(t *testing.T) {
2020
res := icmd.RunCmd(run("helloworld"))
2121
res.Assert(t, icmd.Expected{
2222
ExitCode: 0,
23-
Out: "Hello Cambridge!",
23+
Out: "Hello Cambridge",
2424
})
2525

2626
cfg2, err := config.Load(filepath.Dir(cfg.GetFilename()))

e2e/cli-plugins/dial_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"testing"
77

88
"github.com/docker/cli/cli-plugins/manager"
9-
"gotest.tools/v3/assert"
10-
is "gotest.tools/v3/assert/cmp"
119
"gotest.tools/v3/icmd"
1210
)
1311

@@ -24,7 +22,9 @@ func TestCLIPluginDialStdio(t *testing.T) {
2422
helloworld := filepath.Join(os.Getenv("DOCKER_CLI_E2E_PLUGINS_EXTRA_DIRS"), "docker-helloworld")
2523
cmd := icmd.Command(helloworld, "--config=blah", "--log-level", "debug", "helloworld", "--who=foo")
2624
res := icmd.RunCmd(cmd, icmd.WithEnv(manager.ReexecEnvvar+"=/bin/true"))
27-
res.Assert(t, icmd.Success)
28-
assert.Assert(t, is.Contains(res.Stderr(), `msg="commandconn: starting /bin/true with [--config=blah --log-level debug system dial-stdio]"`))
29-
assert.Assert(t, is.Equal(res.Stdout(), "Hello foo!\n"))
25+
res.Assert(t, icmd.Expected{
26+
ExitCode: 0,
27+
Err: `msg="commandconn: starting /bin/true with [--config=blah --log-level debug system dial-stdio]"`,
28+
Out: `Hello foo`,
29+
})
3030
}

e2e/cli-plugins/flags_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestRunGoodArgument(t *testing.T) {
1515
res := icmd.RunCmd(run("helloworld", "--who", "Cleveland"))
1616
res.Assert(t, icmd.Expected{
1717
ExitCode: 0,
18-
Out: "Hello Cleveland!",
18+
Out: "Hello Cleveland",
1919
})
2020
}
2121

@@ -33,25 +33,25 @@ func TestClashWithGlobalArgs(t *testing.T) {
3333
{
3434
name: "short-without-val",
3535
args: []string{"-D"},
36-
expectedOut: "Hello World!",
36+
expectedOut: "Hello World",
3737
expectedErr: "Plugin debug mode enabled",
3838
},
3939
{
4040
name: "long-without-val",
4141
args: []string{"--debug"},
42-
expectedOut: "Hello World!",
42+
expectedOut: "Hello World",
4343
expectedErr: "Plugin debug mode enabled",
4444
},
4545
{
4646
name: "short-with-val",
4747
args: []string{"-c", "Christmas"},
48-
expectedOut: "Merry Christmas!",
48+
expectedOut: "Merry Christmas",
4949
expectedErr: icmd.None,
5050
},
5151
{
5252
name: "short-with-val",
5353
args: []string{"--context", "Christmas"},
54-
expectedOut: "Merry Christmas!",
54+
expectedOut: "Merry Christmas",
5555
expectedErr: icmd.None,
5656
},
5757
} {
@@ -220,7 +220,7 @@ func TestCliPluginsVersion(t *testing.T) {
220220
name: "plugin-with-version",
221221
args: []string{"helloworld", "version"},
222222
expCode: 0,
223-
expOut: "Hello World!",
223+
expOut: "Hello World",
224224
expErr: icmd.None,
225225
},
226226
{

e2e/cli-plugins/plugins/presocket/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func RootCmd(dockerCli command.Cli) *cobra.Command {
113113
select {
114114
case <-done:
115115
case <-time.After(2 * time.Second):
116-
_, _ = fmt.Fprint(dockerCli.Err(), "timeout after 2 seconds")
116+
_, _ = fmt.Fprintln(dockerCli.Err(), "timeout after 2 seconds")
117117
}
118118
return nil
119119
},

e2e/cli-plugins/run_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"gotest.tools/v3/assert"
7-
is "gotest.tools/v3/assert/cmp"
87
"gotest.tools/v3/golden"
98
"gotest.tools/v3/icmd"
109
)
@@ -123,7 +122,7 @@ func TestRunGood(t *testing.T) {
123122
res := icmd.RunCmd(run("helloworld"))
124123
res.Assert(t, icmd.Expected{
125124
ExitCode: 0,
126-
Out: "Hello World!",
125+
Out: "Hello World",
127126
Err: icmd.None,
128127
})
129128
}
@@ -218,18 +217,23 @@ func TestCliInitialized(t *testing.T) {
218217
run, _, cleanup := prepare(t)
219218
defer cleanup()
220219

221-
var apiversion string
220+
var apiVersion string
222221
t.Run("withhook", func(t *testing.T) {
223222
res := icmd.RunCmd(run("helloworld", "--pre-run", "apiversion"))
224-
res.Assert(t, icmd.Success)
225-
assert.Assert(t, res.Stdout() != "")
226-
apiversion = res.Stdout()
227-
assert.Assert(t, is.Equal(res.Stderr(), "Plugin PersistentPreRunE called"))
223+
res.Assert(t, icmd.Expected{
224+
ExitCode: 0,
225+
Err: "Plugin PersistentPreRunE called",
226+
})
227+
apiVersion = res.Stdout()
228+
assert.Assert(t, apiVersion != "")
228229
})
229230
t.Run("withouthook", func(t *testing.T) {
230231
res := icmd.RunCmd(run("nopersistentprerun"))
231-
res.Assert(t, icmd.Success)
232-
assert.Assert(t, is.Equal(res.Stdout(), apiversion))
232+
res.Assert(t, icmd.Expected{
233+
ExitCode: 0,
234+
Err: icmd.None,
235+
Out: apiVersion,
236+
})
233237
})
234238
}
235239

0 commit comments

Comments
 (0)