Skip to content

Commit edeca76

Browse files
authored
Reject use of fmt.Printf under cmd/ (#3570)
## Changes Reject directly writing to global stdout/stderr. The "forbidigo" linter includes this rule by default. Also see https://golangci-lint.run/docs/linters/configuration/#forbidigo ## Why The CLI uses Cobra and should therefore use `cmd.OutOrStdout()` and friends. Saw this here: #3546 (comment)
1 parent 207b511 commit edeca76

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

.golangci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ linters:
1616
- unused
1717
- exhaustruct
1818
- copyloopvar
19+
- forbidigo
1920
settings:
2021
copyloopvar:
2122
check-alias: true
@@ -72,6 +73,9 @@ linters:
7273
- path: bundle/terranova/tnresources/all_test.go
7374
linters:
7475
- exhaustruct
76+
- path-except: ^cmd
77+
linters:
78+
- forbidigo
7579
issues:
7680
max-issues-per-linter: 1000
7781
max-same-issues: 1000

cmd/bundle/generate/dashboard.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"os"
1011
"path"
1112
"path/filepath"
@@ -53,6 +54,10 @@ type dashboard struct {
5354

5455
// Relative path from the resource directory to the dashboard directory.
5556
relativeDashboardDir string
57+
58+
// Output and error streams.
59+
out io.Writer
60+
err io.Writer
5661
}
5762

5863
func (d *dashboard) resolveID(ctx context.Context, b *bundle.Bundle) string {
@@ -185,7 +190,7 @@ func (d *dashboard) saveSerializedDashboard(_ context.Context, b *bundle.Bundle,
185190
}
186191
}
187192

188-
fmt.Printf("Writing dashboard to %q\n", rel)
193+
fmt.Fprintf(d.out, "Writing dashboard to %q\n", rel)
189194
return os.WriteFile(filename, data, 0o644)
190195
}
191196

@@ -229,7 +234,7 @@ func (d *dashboard) saveConfiguration(ctx context.Context, b *bundle.Bundle, das
229234
rel = resourcePath
230235
}
231236

232-
fmt.Printf("Writing configuration to %q\n", rel)
237+
fmt.Fprintf(d.out, "Writing configuration to %q\n", rel)
233238
err = saver.SaveAsYAML(result, resourcePath, d.force)
234239
if err != nil {
235240
return err
@@ -462,7 +467,10 @@ The --watch flag continuously polls for remote changes and updates your local
462467
bundle files automatically, useful during active dashboard development.`,
463468
}
464469

465-
d := &dashboard{}
470+
d := &dashboard{
471+
out: cmd.OutOrStdout(),
472+
err: cmd.ErrOrStderr(),
473+
}
466474

467475
// Lookup flags.
468476
cmd.Flags().StringVar(&d.existingPath, "existing-path", "", `workspace path of the dashboard to generate configuration for`)

0 commit comments

Comments
 (0)