Skip to content

Commit 38051b4

Browse files
authored
lint: forbidigo (no print or printf in production, prefer fprint) (#4141)
1 parent a1aea2c commit 38051b4

File tree

13 files changed

+60
-40
lines changed

13 files changed

+60
-40
lines changed

.golangci.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ linters:
5858
- paralleltest # Detects missing usage of t.Parallel() method in your Go test
5959
- testpackage # linter that makes you use a separate _test package
6060
- exhaustruct # Checks if all structure fields are initialized
61-
- forbidigo # Forbids identifiers
6261
- gochecknoglobals # Check that no global variables exist.
6362
- goconst # Finds repeated strings that could be replaced by a constant
6463
- tagliatelle # Checks the struct tags.
@@ -200,6 +199,14 @@ linters:
200199
# Default: false
201200
default-case-required: true
202201

202+
forbidigo:
203+
forbid:
204+
# prefer fprint to print calls, keep the latter for debugging
205+
# so they're not comitted by mistake.
206+
- pattern: ^print(ln)?$
207+
- pattern: ^fmt\.Print.*$
208+
msg: use the Fprint equivalent in production code
209+
203210
gocritic:
204211
enable-all: true
205212
disabled-checks:
@@ -553,6 +560,12 @@ linters:
553560
- maintidx
554561
path: (.+)_test.go
555562

563+
# tolerate print in tests for now
564+
- linters:
565+
- forbidigo
566+
path: (.+)_test.go
567+
text: 'use the Fprint equivalent in production code'
568+
556569
# tolerate long functions in tests
557570
- linters:
558571
- revive

cmd/crowdsec-cli/cliallowlists/allowlists.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ func (*cliAllowLists) add(ctx context.Context, db *database.Client, name string,
505505
return fmt.Errorf("unable to apply allowlists to existing decisions: %w", err)
506506
}
507507
if deleted > 0 {
508-
fmt.Printf("%d decisions deleted by allowlists\n", deleted)
508+
fmt.Fprintf(os.Stdout, "%d decisions deleted by allowlists\n", deleted)
509509
}
510510

511511
return nil

cmd/crowdsec-cli/clibouncer/add.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"os"
89

910
"github.com/spf13/cobra"
1011

@@ -32,18 +33,18 @@ func (cli *cliBouncers) add(ctx context.Context, bouncerName string, key string)
3233

3334
switch cli.cfg().Cscli.Output {
3435
case "human":
35-
fmt.Printf("API key for '%s':\n\n", bouncerName)
36-
fmt.Printf(" %s\n\n", key)
37-
fmt.Print("Please keep this key since you will not be able to retrieve it!\n")
36+
fmt.Fprintf(os.Stdout, "API key for '%s':\n\n", bouncerName)
37+
fmt.Fprintf(os.Stdout, " %s\n\n", key)
38+
fmt.Fprintln(os.Stdout, "Please keep this key since you will not be able to retrieve it!")
3839
case "raw":
39-
fmt.Print(key)
40+
fmt.Fprint(os.Stdout, key)
4041
case "json":
4142
j, err := json.Marshal(key)
4243
if err != nil {
4344
return errors.New("unable to serialize api key")
4445
}
4546

46-
fmt.Print(string(j))
47+
fmt.Fprint(os.Stdout, string(j))
4748
}
4849

4950
return nil

cmd/crowdsec-cli/clibouncer/prune.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (cli *cliBouncers) prune(ctx context.Context, duration time.Duration, force
2424
"This may remove active bouncers. Continue?", false); err != nil {
2525
return err
2626
} else if !yes {
27-
fmt.Println("User aborted prune. No changes were made.")
27+
fmt.Fprintln(os.Stdout, "User aborted prune. No changes were made.")
2828
return nil
2929
}
3030
}
@@ -35,7 +35,7 @@ func (cli *cliBouncers) prune(ctx context.Context, duration time.Duration, force
3535
}
3636

3737
if len(bouncers) == 0 {
38-
fmt.Println("No bouncers to prune.")
38+
fmt.Fprintln(os.Stdout, "No bouncers to prune.")
3939
return nil
4040
}
4141

@@ -47,7 +47,7 @@ func (cli *cliBouncers) prune(ctx context.Context, duration time.Duration, force
4747
"These will NOT be recoverable. Continue?", false); err != nil {
4848
return err
4949
} else if !yes {
50-
fmt.Println("User aborted prune. No changes were made.")
50+
fmt.Fprintln(os.Stdout, "User aborted prune. No changes were made.")
5151
return nil
5252
}
5353
}

cmd/crowdsec-cli/cliconfig/feature_flags.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cliconfig
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67

78
"github.com/fatih/color"
@@ -29,17 +30,17 @@ func (cli *cliConfig) featureFlags(showRetired bool) error {
2930
status = green("✓")
3031
}
3132

32-
fmt.Printf("%s %s", status, nameDesc)
33+
fmt.Fprintf(os.Stdout, "%s %s", status, nameDesc)
3334

3435
if feat.State == fflag.DeprecatedState {
35-
fmt.Printf("\n %s %s", yellow("DEPRECATED"), feat.DeprecationMsg)
36+
fmt.Fprintf(os.Stdout, "\n %s %s", yellow("DEPRECATED"), feat.DeprecationMsg)
3637
}
3738

3839
if feat.State == fflag.RetiredState {
39-
fmt.Printf("\n %s %s", magenta("RETIRED"), feat.DeprecationMsg)
40+
fmt.Fprintf(os.Stdout, "\n %s %s", magenta("RETIRED"), feat.DeprecationMsg)
4041
}
4142

42-
fmt.Println()
43+
fmt.Fprintln(os.Stdout)
4344
}
4445

4546
feats := fflag.Crowdsec.GetAllFeatures()
@@ -63,53 +64,53 @@ func (cli *cliConfig) featureFlags(showRetired bool) error {
6364
}
6465

6566
if len(enabled) > 0 {
66-
fmt.Println(" --- Enabled features ---")
67-
fmt.Println()
67+
fmt.Fprintln(os.Stdout, " --- Enabled features ---")
68+
fmt.Fprintln(os.Stdout)
6869

6970
for _, feat := range enabled {
7071
printFeature(feat)
7172
}
7273

73-
fmt.Println()
74+
fmt.Fprintln(os.Stdout)
7475
}
7576

7677
if len(disabled) > 0 {
77-
fmt.Println(" --- Disabled features ---")
78-
fmt.Println()
78+
fmt.Fprintln(os.Stdout, " --- Disabled features ---")
79+
fmt.Fprintln(os.Stdout)
7980

8081
for _, feat := range disabled {
8182
printFeature(feat)
8283
}
8384

84-
fmt.Println()
85+
fmt.Fprintln(os.Stdout)
8586
}
8687

87-
fmt.Println("To enable a feature you can: ")
88-
fmt.Println(" - set the environment variable CROWDSEC_FEATURE_<uppercase_feature_name> to true")
88+
fmt.Fprintln(os.Stdout, "To enable a feature you can: ")
89+
fmt.Fprintln(os.Stdout, " - set the environment variable CROWDSEC_FEATURE_<uppercase_feature_name> to true")
8990

9091
featurePath, err := filepath.Abs(csconfig.GetFeatureFilePath(cli.cfg().FilePath))
9192
if err != nil {
9293
// we already read the file, shouldn't happen
9394
return err
9495
}
9596

96-
fmt.Printf(" - add the line '- <feature_name>' to the file %s\n", featurePath)
97-
fmt.Println()
97+
fmt.Fprintf(os.Stdout, " - add the line '- <feature_name>' to the file %s\n", featurePath)
98+
fmt.Fprintln(os.Stdout)
9899

99100
if len(enabled) == 0 && len(disabled) == 0 {
100-
fmt.Println("However, no feature flag is available in this release.")
101-
fmt.Println()
101+
fmt.Fprintln(os.Stdout, "However, no feature flag is available in this release.")
102+
fmt.Fprintln(os.Stdout)
102103
}
103104

104105
if showRetired && len(retired) > 0 {
105-
fmt.Println(" --- Retired features ---")
106-
fmt.Println()
106+
fmt.Fprintln(os.Stdout, " --- Retired features ---")
107+
fmt.Fprintln(os.Stdout)
107108

108109
for _, feat := range retired {
109110
printFeature(feat)
110111
}
111112

112-
fmt.Println()
113+
fmt.Fprintln(os.Stdout)
113114
}
114115

115116
return nil

cmd/crowdsec-cli/cliconfig/showyaml.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cliconfig
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/spf13/cobra"
78

@@ -15,7 +16,7 @@ func (*cliConfig) newShowYAMLCmd(mergedConfigGetter mergedConfigGetter) *cobra.C
1516
Args: args.NoArgs,
1617
DisableAutoGenTag: true,
1718
RunE: func(_ *cobra.Command, _ []string) error {
18-
fmt.Println(mergedConfigGetter())
19+
fmt.Fprintln(os.Stdout, mergedConfigGetter())
1920
return nil
2021
},
2122
}

cmd/crowdsec-cli/clihub/items.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"os"
89
"slices"
910
"strings"
1011

@@ -69,7 +70,7 @@ func ListItems(out io.Writer, wantColor string, itemTypes []string, items map[st
6970
}
7071

7172
if nothingToDisplay {
72-
fmt.Println("No items to display")
73+
fmt.Fprintln(os.Stdout, "No items to display")
7374
}
7475
case "json":
7576
type itemHubStatus struct {

cmd/crowdsec-cli/climachine/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (cli *cliMachines) add(ctx context.Context, args []string, machinePassword
116116

117117
fmt.Fprintf(os.Stderr, "API credentials written to '%s'.\n", dumpFile)
118118
} else {
119-
fmt.Print(string(apiConfigDump))
119+
fmt.Fprint(os.Stderr, string(apiConfigDump))
120120
}
121121

122122
return nil

cmd/crowdsec-cli/climachine/inspect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"os"
910
"sort"
1011

1112
"github.com/fatih/color"
@@ -45,7 +46,7 @@ func (cli *cliMachines) inspectHubHuman(out io.Writer, machine *ent.Machine) {
4546
state := machine.Hubstate
4647

4748
if len(state) == 0 {
48-
fmt.Println("No hub items found for this machine")
49+
fmt.Fprintln(os.Stdout, "No hub items found for this machine")
4950
return
5051
}
5152

cmd/crowdsec-cli/climachine/prune.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notVa
2525
"This can break installations if the machines are only temporarily disconnected. Continue?", false); err != nil {
2626
return err
2727
} else if !yes {
28-
fmt.Println("User aborted prune. No changes were made.")
28+
fmt.Fprintln(os.Stdout, "User aborted prune. No changes were made.")
2929
return nil
3030
}
3131
}
@@ -42,7 +42,7 @@ func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notVa
4242
}
4343

4444
if len(machines) == 0 {
45-
fmt.Println("No machines to prune.")
45+
fmt.Fprintln(os.Stdout, "No machines to prune.")
4646
return nil
4747
}
4848

@@ -54,7 +54,7 @@ func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notVa
5454
"These will NOT be recoverable. Continue?", false); err != nil {
5555
return err
5656
} else if !yes {
57-
fmt.Println("User aborted prune. No changes were made.")
57+
fmt.Fprintln(os.Stdout, "User aborted prune. No changes were made.")
5858
return nil
5959
}
6060
}

0 commit comments

Comments
 (0)