Skip to content

Commit a2c9f3c

Browse files
committed
linting: address else/if/elseif statements found by gocritic
cli/command/formatter/tabwriter/tabwriter.go:579:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli/connhelper/connhelper.go:43:2: singleCaseSwitch: should rewrite switch statement to if statement (gocritic) switch scheme := u.Scheme; scheme { ^ cli/compose/loader/loader.go:666:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ opts/hosts_test.go:173:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli-plugins/manager/candidate_test.go:78:4: ifElseChain: rewrite if-else to switch statement (gocritic) if tc.err != "" { ^ cli/command/checkpoint/formatter.go:15:2: singleCaseSwitch: should rewrite switch statement to if statement (gocritic) switch source { ^ cli/command/image/formatter_history.go:25:2: singleCaseSwitch: should rewrite switch statement to if statement (gocritic) switch source { ^ cli/command/service/scale.go:107:2: ifElseChain: rewrite if-else to switch statement (gocritic) if serviceMode.Replicated != nil { ^ cli/command/service/update.go:804:9: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli/command/service/update.go:222:2: ifElseChain: rewrite if-else to switch statement (gocritic) if sendAuth { ^ cli/command/container/formatter_diff.go:17:2: singleCaseSwitch: should rewrite switch statement to if statement (gocritic) switch source { ^ cli/command/container/start.go:79:2: ifElseChain: rewrite if-else to switch statement (gocritic) if opts.Attach || opts.OpenStdin { ^ cli/command/container/utils.go:84:11: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli/command/container/exec_test.go:200:11: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli/command/container/logs_test.go:52:11: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli/command/container/opts_test.go:1014:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) } else { ^ cli/command/system/info.go:297:7: singleCaseSwitch: should rewrite switch statement to if statement (gocritic) switch o.Key { ^ cli/command/system/version.go:164:4: singleCaseSwitch: should rewrite switch statement to if statement (gocritic) switch component.Name { ^ cli/command/system/info_test.go:478:4: ifElseChain: rewrite if-else to switch statement (gocritic) if tc.expectedOut != "" { ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 888df09 commit a2c9f3c

18 files changed

Lines changed: 54 additions & 71 deletions

File tree

cli-plugins/manager/candidate_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ func TestValidateCandidate(t *testing.T) {
7575
} {
7676
t.Run(tc.name, func(t *testing.T) {
7777
p, err := newPlugin(tc.c, fakeroot.Commands())
78-
if tc.err != "" {
78+
switch {
79+
case tc.err != "":
7980
assert.ErrorContains(t, err, tc.err)
80-
} else if tc.invalid != "" {
81+
case tc.invalid != "":
8182
assert.NilError(t, err)
8283
assert.Assert(t, cmp.ErrorType(p.Err, reflect.TypeOf(&pluginError{})))
8384
assert.ErrorContains(t, p.Err, tc.invalid)
84-
} else {
85+
default:
8586
assert.NilError(t, err)
8687
assert.Equal(t, NamePrefix+p.Name, goodPluginName)
8788
assert.Equal(t, p.SchemaVersion, "0.1.0")

cli/command/checkpoint/formatter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const (
1212

1313
// NewFormat returns a format for use with a checkpoint Context
1414
func NewFormat(source string) formatter.Format {
15-
switch source {
16-
case formatter.TableFormatKey:
15+
if source == formatter.TableFormatKey {
1716
return defaultCheckpointFormat
1817
}
1918
return formatter.Format(source)

cli/command/container/exec_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ func TestRunExec(t *testing.T) {
197197
err := RunExec(context.Background(), cli, "thecontainer", testcase.options)
198198
if testcase.expectedError != "" {
199199
assert.ErrorContains(t, err, testcase.expectedError)
200-
} else {
201-
if !assert.Check(t, err) {
202-
return
203-
}
200+
} else if !assert.Check(t, err) {
201+
return
204202
}
205203
assert.Check(t, is.Equal(testcase.expectedOut, cli.OutBuffer().String()))
206204
assert.Check(t, is.Equal(testcase.expectedErr, cli.ErrBuffer().String()))

cli/command/container/formatter_diff.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ const (
1414

1515
// NewDiffFormat returns a format for use with a diff Context
1616
func NewDiffFormat(source string) formatter.Format {
17-
switch source {
18-
case formatter.TableFormatKey:
17+
if source == formatter.TableFormatKey {
1918
return defaultDiffTableFormat
2019
}
2120
return formatter.Format(source)

cli/command/container/logs_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ func TestRunLogs(t *testing.T) {
4949
err := runLogs(cli, testcase.options)
5050
if testcase.expectedError != "" {
5151
assert.ErrorContains(t, err, testcase.expectedError)
52-
} else {
53-
if !assert.Check(t, err) {
54-
return
55-
}
52+
} else if !assert.Check(t, err) {
53+
return
5654
}
5755
assert.Check(t, is.Equal(testcase.expectedOut, cli.OutBuffer().String()))
5856
assert.Check(t, is.Equal(testcase.expectedErr, cli.ErrBuffer().String()))

cli/command/container/opts_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,8 @@ func TestValidateDevice(t *testing.T) {
10111011
for path, expectedError := range invalid {
10121012
if _, err := validateDevice(path, runtime.GOOS); err == nil {
10131013
t.Fatalf("ValidateDevice(`%q`) should have failed validation", path)
1014-
} else {
1015-
if err.Error() != expectedError {
1016-
t.Fatalf("ValidateDevice(`%q`) error should contain %q, got %q", path, expectedError, err.Error())
1017-
}
1014+
} else if err.Error() != expectedError {
1015+
t.Fatalf("ValidateDevice(`%q`) error should contain %q, got %q", path, expectedError, err.Error())
10181016
}
10191017
}
10201018
}

cli/command/container/start.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error {
7676
ctx, cancelFun := context.WithCancel(context.Background())
7777
defer cancelFun()
7878

79-
if opts.Attach || opts.OpenStdin {
79+
switch {
80+
case opts.Attach || opts.OpenStdin:
8081
// We're going to attach to a container.
8182
// 1. Ensure we only have one container.
8283
if len(opts.Containers) > 1 {
@@ -180,7 +181,8 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error {
180181
if status := <-statusChan; status != 0 {
181182
return cli.StatusError{StatusCode: status}
182183
}
183-
} else if opts.Checkpoint != "" {
184+
return nil
185+
case opts.Checkpoint != "":
184186
if len(opts.Containers) > 1 {
185187
return errors.New("you cannot restore multiple containers at once")
186188
}
@@ -189,14 +191,11 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error {
189191
CheckpointID: opts.Checkpoint,
190192
CheckpointDir: opts.CheckpointDir,
191193
})
192-
193-
} else {
194+
default:
194195
// We're not going to attach to anything.
195196
// Start as many containers as we want.
196197
return startContainersWithoutAttachments(ctx, dockerCli, opts.Containers)
197198
}
198-
199-
return nil
200199
}
201200

202201
func startContainersWithoutAttachments(ctx context.Context, dockerCli command.Cli, containers []string) error {

cli/command/container/utils.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,16 @@ func legacyWaitExitOrRemoved(ctx context.Context, apiClient client.APIClient, co
8181
}
8282
if !waitRemove {
8383
stopProcessing = true
84-
} else {
84+
} else if versions.LessThan(apiClient.ClientVersion(), "1.25") {
8585
// If we are talking to an older daemon, `AutoRemove` is not supported.
8686
// We need to fall back to the old behavior, which is client-side removal
87-
if versions.LessThan(apiClient.ClientVersion(), "1.25") {
88-
go func() {
89-
removeErr = apiClient.ContainerRemove(ctx, containerID, container.RemoveOptions{RemoveVolumes: true})
90-
if removeErr != nil {
91-
logrus.Errorf("error removing container: %v", removeErr)
92-
cancel() // cancel the event Q
93-
}
94-
}()
95-
}
87+
go func() {
88+
removeErr = apiClient.ContainerRemove(ctx, containerID, container.RemoveOptions{RemoveVolumes: true})
89+
if removeErr != nil {
90+
logrus.Errorf("error removing container: %v", removeErr)
91+
cancel() // cancel the event Q
92+
}
93+
}()
9694
}
9795
case "detach":
9896
exitCode = 0

cli/command/formatter/tabwriter/tabwriter.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -576,18 +576,16 @@ func (b *Writer) Write(buf []byte) (n int, err error) {
576576
b.startEscape(ch)
577577
}
578578
}
579-
} else {
579+
} else if ch == b.endChar {
580580
// inside escape
581-
if ch == b.endChar {
582-
// end of tag/entity
583-
j := i + 1
584-
if ch == Escape && b.flags&StripEscape != 0 {
585-
j = i // strip Escape
586-
}
587-
b.append(buf[n:j])
588-
n = i + 1 // ch consumed
589-
b.endEscape()
581+
// end of tag/entity
582+
j := i + 1
583+
if ch == Escape && b.flags&StripEscape != 0 {
584+
j = i // strip Escape
590585
}
586+
b.append(buf[n:j])
587+
n = i + 1 // ch consumed
588+
b.endEscape()
591589
}
592590
}
593591

cli/command/image/formatter_history.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ const (
2222

2323
// NewHistoryFormat returns a format for rendering an HistoryContext
2424
func NewHistoryFormat(source string, quiet bool, human bool) formatter.Format {
25-
switch source {
26-
case formatter.TableFormatKey:
25+
if source == formatter.TableFormatKey {
2726
switch {
2827
case quiet:
2928
return formatter.DefaultQuietFormat

0 commit comments

Comments
 (0)