Skip to content

Commit 58dcfcd

Browse files
authored
Merge pull request docker#9643 from ulyssessouza/add-gocritic-linter
Add gocritic to linters
2 parents 923e01d + fc723ac commit 58dcfcd

27 files changed

+181
-163
lines changed

.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ linters:
77
- deadcode
88
- depguard
99
- errcheck
10+
- gocritic
1011
- gocyclo
1112
- gofmt
1213
- goimports
@@ -32,6 +33,17 @@ linters-settings:
3233
# The io/ioutil package has been deprecated.
3334
# https://go.dev/doc/go1.16#ioutil
3435
- io/ioutil
36+
gocritic:
37+
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
38+
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
39+
enabled-tags:
40+
- diagnostic
41+
- opinionated
42+
- style
43+
disabled-checks:
44+
- paramTypeCombine
45+
- unnamedResult
46+
- whyNoLint
3547
gocyclo:
3648
min-complexity: 16
3749
lll:

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
# limitations under the License.
1717

1818
ARG GO_VERSION=1.18.4-alpine
19-
ARG GOLANGCI_LINT_VERSION=v1.40.1-alpine
19+
ARG GOLANGCI_LINT_VERSION=v1.46.2-alpine
2020
ARG PROTOC_GEN_GO_VERSION=v1.4.3
2121

22+
FROM --platform=${BUILDPLATFORM} golangci/golangci-lint:${GOLANGCI_LINT_VERSION} AS local-golangci-lint
23+
2224
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS base
2325
WORKDIR /compose-cli
2426
RUN apk add --no-cache -vv \
@@ -34,7 +36,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
3436

3537
FROM base AS lint
3638
ENV CGO_ENABLED=0
37-
COPY --from=golangci/golangci-lint /usr/bin/golangci-lint /usr/bin/golangci-lint
39+
COPY --from=local-golangci-lint /usr/bin/golangci-lint /usr/bin/golangci-lint
3840
ARG BUILD_TAGS
3941
ARG GIT_TAG
4042
RUN --mount=target=. \

cmd/compose/compose.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
239239
verbose bool
240240
version bool
241241
)
242-
command := &cobra.Command{
242+
c := &cobra.Command{
243243
Short: "Docker Compose",
244244
Use: PluginName,
245245
TraverseChildren: true,
246-
// By default (no Run/RunE in parent command) for typos in subcommands, cobra displays the help of parent command but exit(0) !
246+
// By default (no Run/RunE in parent c) for typos in subcommands, cobra displays the help of parent c but exit(0) !
247247
RunE: func(cmd *cobra.Command, args []string) error {
248248
if len(args) == 0 {
249249
return cmd.Help()
@@ -300,7 +300,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
300300
},
301301
}
302302

303-
command.AddCommand(
303+
c.AddCommand(
304304
upCommand(&opts, backend),
305305
downCommand(&opts, backend),
306306
startCommand(&opts, backend),
@@ -327,16 +327,16 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
327327
createCommand(&opts, backend),
328328
copyCommand(&opts, backend),
329329
)
330-
command.Flags().SetInterspersed(false)
331-
opts.addProjectFlags(command.Flags())
332-
command.Flags().StringVar(&ansi, "ansi", "auto", `Control when to print ANSI control characters ("never"|"always"|"auto")`)
333-
command.Flags().BoolVarP(&version, "version", "v", false, "Show the Docker Compose version information")
334-
command.Flags().MarkHidden("version") //nolint:errcheck
335-
command.Flags().BoolVar(&noAnsi, "no-ansi", false, `Do not print ANSI control characters (DEPRECATED)`)
336-
command.Flags().MarkHidden("no-ansi") //nolint:errcheck
337-
command.Flags().BoolVar(&verbose, "verbose", false, "Show more output")
338-
command.Flags().MarkHidden("verbose") //nolint:errcheck
339-
return command
330+
c.Flags().SetInterspersed(false)
331+
opts.addProjectFlags(c.Flags())
332+
c.Flags().StringVar(&ansi, "ansi", "auto", `Control when to print ANSI control characters ("never"|"always"|"auto")`)
333+
c.Flags().BoolVarP(&version, "version", "v", false, "Show the Docker Compose version information")
334+
c.Flags().MarkHidden("version") //nolint:errcheck
335+
c.Flags().BoolVar(&noAnsi, "no-ansi", false, `Do not print ANSI control characters (DEPRECATED)`)
336+
c.Flags().MarkHidden("no-ansi") //nolint:errcheck
337+
c.Flags().BoolVar(&verbose, "verbose", false, "Show more output")
338+
c.Flags().MarkHidden("verbose") //nolint:errcheck
339+
return c
340340
}
341341

342342
func setEnvWithDotEnv(prjOpts *projectOptions) error {
@@ -355,7 +355,7 @@ func setEnvWithDotEnv(prjOpts *projectOptions) error {
355355
}
356356
for k, v := range envFromFile {
357357
if _, ok := os.LookupEnv(k); !ok {
358-
if err = os.Setenv(k, v); err != nil {
358+
if err := os.Setenv(k, v); err != nil {
359359
return err
360360
}
361361
}

cmd/compose/down.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
6969
flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
7070
flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
7171
flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
72-
switch name {
73-
case "volume":
72+
if name == "volume" {
7473
name = "volumes"
7574
logrus.Warn("--volume is deprecated, please use --volumes")
7675
}

cmd/compose/list.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ type lsOptions struct {
3939
}
4040

4141
func listCommand(backend api.Service) *cobra.Command {
42-
opts := lsOptions{Filter: opts.NewFilterOpt()}
42+
lsOpts := lsOptions{Filter: opts.NewFilterOpt()}
4343
lsCmd := &cobra.Command{
4444
Use: "ls",
4545
Short: "List running compose projects",
4646
RunE: Adapt(func(ctx context.Context, args []string) error {
47-
return runList(ctx, backend, opts)
47+
return runList(ctx, backend, lsOpts)
4848
}),
4949
ValidArgsFunction: noCompletion(),
5050
}
51-
lsCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
52-
lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs.")
53-
lsCmd.Flags().Var(&opts.Filter, "filter", "Filter output based on conditions provided.")
54-
lsCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped Compose projects")
51+
lsCmd.Flags().StringVar(&lsOpts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
52+
lsCmd.Flags().BoolVarP(&lsOpts.Quiet, "quiet", "q", false, "Only display IDs.")
53+
lsCmd.Flags().Var(&lsOpts.Filter, "filter", "Filter output based on conditions provided.")
54+
lsCmd.Flags().BoolVarP(&lsOpts.All, "all", "a", false, "Show all stopped Compose projects")
5555

5656
return lsCmd
5757
}
@@ -60,18 +60,18 @@ var acceptedListFilters = map[string]bool{
6060
"name": true,
6161
}
6262

63-
func runList(ctx context.Context, backend api.Service, opts lsOptions) error {
64-
filters := opts.Filter.Value()
63+
func runList(ctx context.Context, backend api.Service, lsOpts lsOptions) error {
64+
filters := lsOpts.Filter.Value()
6565
err := filters.Validate(acceptedListFilters)
6666
if err != nil {
6767
return err
6868
}
6969

70-
stackList, err := backend.List(ctx, api.ListOptions{All: opts.All})
70+
stackList, err := backend.List(ctx, api.ListOptions{All: lsOpts.All})
7171
if err != nil {
7272
return err
7373
}
74-
if opts.Quiet {
74+
if lsOpts.Quiet {
7575
for _, s := range stackList {
7676
fmt.Println(s.Name)
7777
}
@@ -90,7 +90,7 @@ func runList(ctx context.Context, backend api.Service, opts lsOptions) error {
9090
}
9191

9292
view := viewFromStackList(stackList)
93-
return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
93+
return formatter.Print(view, lsOpts.Format, os.Stdout, func(w io.Writer) {
9494
for _, stack := range view {
9595
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
9696
}

cmd/compose/up.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,20 @@ func runUp(ctx context.Context, backend api.Service, createOptions createOptions
219219

220220
func setServiceScale(project *types.Project, name string, replicas uint64) error {
221221
for i, s := range project.Services {
222-
if s.Name == name {
223-
service, err := project.GetService(name)
224-
if err != nil {
225-
return err
226-
}
227-
if service.Deploy == nil {
228-
service.Deploy = &types.DeployConfig{}
229-
}
230-
service.Deploy.Replicas = &replicas
231-
project.Services[i] = service
232-
return nil
222+
if s.Name != name {
223+
continue
224+
}
225+
226+
service, err := project.GetService(name)
227+
if err != nil {
228+
return err
229+
}
230+
if service.Deploy == nil {
231+
service.Deploy = &types.DeployConfig{}
233232
}
233+
service.Deploy.Replicas = &replicas
234+
project.Services[i] = service
235+
return nil
234236
}
235237
return fmt.Errorf("unknown service %q", name)
236238
}

cmd/formatter/logs.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ import (
2727
"github.com/docker/compose/v2/pkg/api"
2828
)
2929

30+
// LogConsumer consume logs from services and format them
31+
type logConsumer struct {
32+
ctx context.Context
33+
presenters sync.Map // map[string]*presenter
34+
width int
35+
writer io.Writer
36+
color bool
37+
prefix bool
38+
}
39+
3040
// NewLogConsumer creates a new LogConsumer
3141
func NewLogConsumer(ctx context.Context, w io.Writer, color bool, prefix bool) api.LogConsumer {
3242
return &logConsumer{
@@ -79,14 +89,14 @@ func (l *logConsumer) Log(container, service, message string) {
7989
}
8090
p := l.getPresenter(container)
8191
for _, line := range strings.Split(message, "\n") {
82-
fmt.Fprintf(l.writer, "%s%s\n", p.prefix, line) // nolint:errcheck
92+
fmt.Fprintf(l.writer, "%s%s\n", p.prefix, line) //nolint:errcheck
8393
}
8494
}
8595

8696
func (l *logConsumer) Status(container, msg string) {
8797
p := l.getPresenter(container)
8898
s := p.colors(fmt.Sprintf("%s %s\n", container, msg))
89-
l.writer.Write([]byte(s)) // nolint:errcheck
99+
l.writer.Write([]byte(s)) //nolint:errcheck
90100
}
91101

92102
func (l *logConsumer) computeWidth() {
@@ -101,16 +111,6 @@ func (l *logConsumer) computeWidth() {
101111
l.width = width + 1
102112
}
103113

104-
// LogConsumer consume logs from services and format them
105-
type logConsumer struct {
106-
ctx context.Context
107-
presenters sync.Map // map[string]*presenter
108-
width int
109-
writer io.Writer
110-
color bool
111-
prefix bool
112-
}
113-
114114
type presenter struct {
115115
colors colorFunc
116116
name string

pkg/api/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
const (
24-
//ExitCodeLoginRequired exit code when command cannot execute because it requires cloud login
24+
// ExitCodeLoginRequired exit code when command cannot execute because it requires cloud login
2525
// This will be used by VSCode to detect when creating context if the user needs to login first
2626
ExitCodeLoginRequired = 5
2727
)

pkg/compose/attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
127127
if stdout != nil {
128128
go func() {
129129
if tty {
130-
io.Copy(stdout, streamOut) // nolint:errcheck
130+
io.Copy(stdout, streamOut) //nolint:errcheck
131131
} else {
132-
stdcopy.StdCopy(stdout, stderr, streamOut) // nolint:errcheck
132+
stdcopy.StdCopy(stdout, stderr, streamOut) //nolint:errcheck
133133
}
134134
}()
135135
}

pkg/compose/build.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,30 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
6161
}
6262

6363
for _, service := range services {
64-
if service.Build != nil {
65-
imageName := getImageName(service, project.Name)
66-
imagesToBuild = append(imagesToBuild, imageName)
67-
buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs)
68-
if err != nil {
69-
return err
70-
}
71-
buildOptions.Pull = options.Pull
72-
buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, args)
73-
buildOptions.NoCache = options.NoCache
74-
buildOptions.CacheFrom, err = buildflags.ParseCacheEntry(service.Build.CacheFrom)
75-
if err != nil {
76-
return err
77-
}
64+
if service.Build == nil {
65+
continue
66+
}
67+
imageName := getImageName(service, project.Name)
68+
imagesToBuild = append(imagesToBuild, imageName)
69+
buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs)
70+
if err != nil {
71+
return err
72+
}
73+
buildOptions.Pull = options.Pull
74+
buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, args)
75+
buildOptions.NoCache = options.NoCache
76+
buildOptions.CacheFrom, err = buildflags.ParseCacheEntry(service.Build.CacheFrom)
77+
if err != nil {
78+
return err
79+
}
7880

79-
for _, image := range service.Build.CacheFrom {
80-
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
81-
Type: "registry",
82-
Attrs: map[string]string{"ref": image},
83-
})
84-
}
85-
opts[imageName] = buildOptions
81+
for _, image := range service.Build.CacheFrom {
82+
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
83+
Type: "registry",
84+
Attrs: map[string]string{"ref": image},
85+
})
8686
}
87+
opts[imageName] = buildOptions
8788
}
8889

8990
_, err = s.doBuild(ctx, project, opts, options.Progress)
@@ -312,11 +313,11 @@ func mergeArgs(m ...types.Mapping) types.Mapping {
312313
return merged
313314
}
314315

315-
func dockerFilePath(context string, dockerfile string) string {
316-
if urlutil.IsGitURL(context) || filepath.IsAbs(dockerfile) {
316+
func dockerFilePath(ctxName string, dockerfile string) string {
317+
if urlutil.IsGitURL(ctxName) || filepath.IsAbs(dockerfile) {
317318
return dockerfile
318319
}
319-
return filepath.Join(context, dockerfile)
320+
return filepath.Join(ctxName, dockerfile)
320321
}
321322

322323
func sshAgentProvider(sshKeys types.SSHConfig) (session.Attachable, error) {

0 commit comments

Comments
 (0)