From 51fbf3da9ee34a8143df5baeda6032c1747446d2 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 25 Apr 2022 15:15:52 +0200 Subject: enable gocritic linter The linter ensures a common code style. - use switch/case instead of else if - use if instead of switch/case for single case statement - add space between comment and text - detect the use of defer with os.Exit() - use short form var += "..." instead of var = var + "..." - detect problems with append() ``` newSlice := append(orgSlice, val) ``` This could lead to nasty bugs because the orgSlice will be changed in place if it has enough capacity too hold the new elements. Thus we newSlice might not be a copy. Of course most of the changes are just cosmetic and do not cause any logic errors but I think it is a good idea to enforce a common style. This should help maintainability. Signed-off-by: Paul Holzinger --- cmd/podman/common/completion.go | 9 +++++++-- cmd/podman/common/create.go | 2 +- cmd/podman/completion/completion.go | 2 +- cmd/podman/images/scp.go | 9 ++------- cmd/podman/pods/create.go | 7 ++++--- cmd/podman/secrets/create.go | 7 ++++--- cmd/podman/system/migrate.go | 4 ++++ cmd/podman/system/renumber.go | 3 +++ cmd/podman/system/reset.go | 3 +++ cmd/podman/utils/alias.go | 3 +-- 10 files changed, 30 insertions(+), 19 deletions(-) (limited to 'cmd') diff --git a/cmd/podman/common/completion.go b/cmd/podman/common/completion.go index abb943942..c7d5d6d60 100644 --- a/cmd/podman/common/completion.go +++ b/cmd/podman/common/completion.go @@ -327,7 +327,7 @@ func suffixCompSlice(suf string, slice []string) []string { if len(split) > 1 { slice[i] = split[0] + suf + "\t" + split[1] } else { - slice[i] = slice[i] + suf + slice[i] += suf } } return slice @@ -647,7 +647,10 @@ func AutocompleteInspect(cmd *cobra.Command, args []string, toComplete string) ( pods, _ := getPods(cmd, toComplete, completeDefault) networks, _ := getNetworks(cmd, toComplete, completeDefault) volumes, _ := getVolumes(cmd, toComplete) - suggestions := append(containers, images...) + + suggestions := make([]string, 0, len(containers)+len(images)+len(pods)+len(networks)+len(volumes)) + suggestions = append(suggestions, containers...) + suggestions = append(suggestions, images...) suggestions = append(suggestions, pods...) suggestions = append(suggestions, networks...) suggestions = append(suggestions, volumes...) @@ -961,6 +964,8 @@ func AutocompleteFormat(o interface{}) func(cmd *cobra.Command, args []string, t // this function provides shell completion for go templates return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // autocomplete json when nothing or json is typed + // gocritic complains about the argument order but this is correct in this case + //nolint:gocritic if strings.HasPrefix("json", toComplete) { return []string{"json"}, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/podman/common/create.go b/cmd/podman/common/create.go index 62c9f4c9a..1c1a7c3e3 100644 --- a/cmd/podman/common/create.go +++ b/cmd/podman/common/create.go @@ -896,7 +896,7 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions, ) _ = cmd.RegisterFlagCompletionFunc(memorySwappinessFlagName, completion.AutocompleteNone) } - //anyone can use these + // anyone can use these cpusFlagName := "cpus" createFlags.Float64Var( &cf.CPUS, diff --git a/cmd/podman/completion/completion.go b/cmd/podman/completion/completion.go index b02a0d772..6f185cde8 100644 --- a/cmd/podman/completion/completion.go +++ b/cmd/podman/completion/completion.go @@ -31,7 +31,7 @@ var ( Example: `podman completion bash podman completion zsh -f _podman podman completion fish --no-desc`, - //don't show this command to users + // don't show this command to users Hidden: true, } ) diff --git a/cmd/podman/images/scp.go b/cmd/podman/images/scp.go index 51a9d1c4e..3dbc9c331 100644 --- a/cmd/podman/images/scp.go +++ b/cmd/podman/images/scp.go @@ -121,13 +121,8 @@ func scp(cmd *cobra.Command, args []string) (finalErr error) { return err } if flipConnections { // the order of cliConnections matters, we need to flip both arrays since the args are parsed separately sometimes. - connect := cliConnections[0] - cliConnections[0] = cliConnections[1] - cliConnections[1] = connect - - loc := locations[0] - locations[0] = locations[1] - locations[1] = loc + cliConnections[0], cliConnections[1] = cliConnections[1], cliConnections[0] + locations[0], locations[1] = locations[1], locations[0] } dest = *locations[1] case len(locations) == 1: diff --git a/cmd/podman/pods/create.go b/cmd/podman/pods/create.go index b45ed0d39..891ff2e3c 100644 --- a/cmd/podman/pods/create.go +++ b/cmd/podman/pods/create.go @@ -224,7 +224,8 @@ func create(cmd *cobra.Command, args []string) error { } sort.Ints(vals) for ind, core := range vals { - if core > int(cpuSet) { + switch { + case core > int(cpuSet): if copy == "" { copy = "0-" + strconv.Itoa(int(cpuSet)) infraOptions.CPUSetCPUs = copy @@ -233,9 +234,9 @@ func create(cmd *cobra.Command, args []string) error { infraOptions.CPUSetCPUs = copy break } - } else if ind != 0 { + case ind != 0: copy += "," + strconv.Itoa(core) - } else { + default: copy = "" + strconv.Itoa(core) } } diff --git a/cmd/podman/secrets/create.go b/cmd/podman/secrets/create.go index 069e2a1dc..01ee3d256 100644 --- a/cmd/podman/secrets/create.go +++ b/cmd/podman/secrets/create.go @@ -62,13 +62,14 @@ func create(cmd *cobra.Command, args []string) error { path := args[1] var reader io.Reader - if env { + switch { + case env: envValue := os.Getenv(path) if envValue == "" { return errors.Errorf("cannot create store secret data: environment variable %s is not set", path) } reader = strings.NewReader(envValue) - } else if path == "-" || path == "/dev/stdin" { + case path == "-" || path == "/dev/stdin": stat, err := os.Stdin.Stat() if err != nil { return err @@ -77,7 +78,7 @@ func create(cmd *cobra.Command, args []string) error { return errors.New("if `-` is used, data must be passed into stdin") } reader = os.Stdin - } else { + default: file, err := os.Open(path) if err != nil { return err diff --git a/cmd/podman/system/migrate.go b/cmd/podman/system/migrate.go index 5d7b31314..b2bca9be0 100644 --- a/cmd/podman/system/migrate.go +++ b/cmd/podman/system/migrate.go @@ -69,6 +69,10 @@ func migrate(cmd *cobra.Command, args []string) { err = engine.Migrate(registry.Context(), cmd.Flags(), registry.PodmanConfig(), migrateOptions) if err != nil { fmt.Println(err) + + // FIXME change this to return the error like other commands + // defer will never run on os.Exit() + //nolint:gocritic os.Exit(define.ExecErrorCodeGeneric) } os.Exit(0) diff --git a/cmd/podman/system/renumber.go b/cmd/podman/system/renumber.go index f24488822..e5a241a4f 100644 --- a/cmd/podman/system/renumber.go +++ b/cmd/podman/system/renumber.go @@ -56,6 +56,9 @@ func renumber(cmd *cobra.Command, args []string) { err = engine.Renumber(registry.Context(), cmd.Flags(), registry.PodmanConfig()) if err != nil { fmt.Println(err) + // FIXME change this to return the error like other commands + // defer will never run on os.Exit() + //nolint:gocritic os.Exit(define.ExecErrorCodeGeneric) } os.Exit(0) diff --git a/cmd/podman/system/reset.go b/cmd/podman/system/reset.go index e8cf127b7..03783170f 100644 --- a/cmd/podman/system/reset.go +++ b/cmd/podman/system/reset.go @@ -95,6 +95,9 @@ func reset(cmd *cobra.Command, args []string) { if err := engine.Reset(registry.Context()); err != nil { logrus.Error(err) + // FIXME change this to return the error like other commands + // defer will never run on os.Exit() + //nolint:gocritic os.Exit(define.ExecErrorCodeGeneric) } os.Exit(0) diff --git a/cmd/podman/utils/alias.go b/cmd/podman/utils/alias.go index 4d5b625d0..b37d0f714 100644 --- a/cmd/podman/utils/alias.go +++ b/cmd/podman/utils/alias.go @@ -37,8 +37,7 @@ func AliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName { // TimeoutAliasFlags is a function to handle backwards compatibility with old timeout flags func TimeoutAliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName { - switch name { - case "timeout": + if name == "timeout" { name = "time" } return pflag.NormalizedName(name) -- cgit v1.2.3-54-g00ecf