Skip to content

Commit d42adf6

Browse files
committed
Add "opinionated" tag to gocritic
Signed-off-by: Ulysses Souza <[email protected]>
1 parent a81f23a commit d42adf6

File tree

10 files changed

+76
-69
lines changed

10 files changed

+76
-69
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ linters-settings:
3838
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
3939
enabled-tags:
4040
- diagnostic
41+
- opinionated
42+
disabled-checks:
43+
- paramTypeCombine
44+
- unnamedResult
4145
gocyclo:
4246
min-complexity: 16
4347
lll:

cmd/compose/compose.go

Lines changed: 13 additions & 13 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 {

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
}

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) {

pkg/compose/build_classic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
214214
if imageID == "" {
215215
return "", errors.Errorf("Server did not provide an image ID. Cannot write %s", options.ImageIDFile)
216216
}
217-
if err := os.WriteFile(options.ImageIDFile, []byte(imageID), 0666); err != nil {
217+
if err := os.WriteFile(options.ImageIDFile, []byte(imageID), 0o666); err != nil {
218218
return "", err
219219
}
220220
}

pkg/compose/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
893893
continue
894894
}
895895

896-
mount, err := buildMount(p, types.ServiceVolumeConfig{
896+
mnt, err := buildMount(p, types.ServiceVolumeConfig{
897897
Type: types.VolumeTypeBind,
898898
Source: definedSecret.File,
899899
Target: target,
@@ -902,7 +902,7 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
902902
if err != nil {
903903
return nil, err
904904
}
905-
mounts[target] = mount
905+
mounts[target] = mnt
906906
}
907907
values := make([]mount.Mount, 0, len(mounts))
908908
for _, v := range mounts {
@@ -911,8 +911,8 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
911911
return values, nil
912912
}
913913

914-
func isUnixAbs(path string) bool {
915-
return strings.HasPrefix(path, "/")
914+
func isUnixAbs(p string) bool {
915+
return strings.HasPrefix(p, "/")
916916
}
917917

918918
func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {

pkg/compose/secrets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func createTar(env string, config types.ServiceSecretConfig) (bytes.Buffer, erro
5757
value := []byte(env)
5858
b := bytes.Buffer{}
5959
tarWriter := tar.NewWriter(&b)
60-
mode := uint32(0400)
60+
mode := uint32(0o400)
6161
if config.Mode != nil {
6262
mode = *config.Mode
6363
}

pkg/e2e/framework.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func initializePlugins(t testing.TB, configDir string) {
127127
}
128128
})
129129

130-
require.NoError(t, os.MkdirAll(filepath.Join(configDir, "cli-plugins"), 0755),
130+
require.NoError(t, os.MkdirAll(filepath.Join(configDir, "cli-plugins"), 0o755),
131131
"Failed to create cli-plugins directory")
132132
composePlugin, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"})
133133
if os.IsNotExist(err) {
@@ -175,7 +175,7 @@ func CopyFile(t testing.TB, sourceFile string, destinationFile string) {
175175
// nolint: errcheck
176176
defer src.Close()
177177

178-
dst, err := os.OpenFile(destinationFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
178+
dst, err := os.OpenFile(destinationFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
179179
require.NoError(t, err, "Failed to open destination file: %s", destinationFile)
180180
// nolint: errcheck
181181
defer dst.Close()

pkg/e2e/scan_message_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func TestDisplayScanMessageAfterBuild(t *testing.T) {
7171
})
7272

7373
t.Run("do not display if scan already invoked", func(t *testing.T) {
74-
_ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0755)
74+
_ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0o755)
7575
scanConfigFile := filepath.Join(c.ConfigDir, "scan", "config.json")
76-
err := os.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0644)
76+
err := os.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0o644)
7777
assert.NilError(t, err)
7878

7979
res := c.RunDockerCmd(t, "build", "-t", "test-image-scan-msg", "fixtures/simple-build-test/nginx-build")

0 commit comments

Comments
 (0)