Skip to content

Commit

Permalink
🌱 use forbidigo linter to prevent print statements (#3585)
Browse files Browse the repository at this point in the history
* enable forbidigo for print statements.

include reasoning as message exposed to developer.

Signed-off-by: Spencer Schrock <[email protected]>

* remove or grant exceptions for existing print statements

Signed-off-by: Spencer Schrock <[email protected]>

* swap stdout to stderr

Signed-off-by: Spencer Schrock <[email protected]>

* separate msg from regex for better readability.

Signed-off-by: Spencer Schrock <[email protected]>

---------

Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock committed Oct 23, 2023
1 parent d0cefa5 commit 2d93196
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ linters:
- errorlint
- exhaustive
- exportloopref
- forbidigo
- gci
- gochecknoinits
- gocognit
Expand Down Expand Up @@ -75,6 +76,10 @@ linters-settings:
exhaustive:
# https://golangci-lint.run/usage/linters/#exhaustive
default-signifies-exhaustive: true
forbidigo:
forbid:
- p: "^fmt\\.Print.*$"
msg: "Do not commit print statements. Output to stdout interferes with users who redirect JSON results to files."
govet:
enable:
- fieldalignment
Expand Down
2 changes: 1 addition & 1 deletion attestor/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func init() {

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
5 changes: 2 additions & 3 deletions attestor/policy/attestation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package policy
import (
"encoding/json"
"errors"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -533,8 +532,8 @@ func TestAttestationPolicyRead(t *testing.T) {
// Compare outputs only if the error is nil.
// TODO: compare objects.
if p.ToJSON() != tt.result.ToJSON() {
fmt.Printf("p.ToJSON(): %v\n", p.ToJSON())
fmt.Printf("tt.result.ToJSON(): %v\n", tt.result.ToJSON())
t.Logf("p.ToJSON(): %v\n", p.ToJSON())
t.Logf("tt.result.ToJSON(): %v\n", tt.result.ToJSON())
t.Fatalf("%s: invalid result", tt.name)
}
})
Expand Down
7 changes: 3 additions & 4 deletions clients/gitlabrepo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package gitlabrepo

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -107,9 +106,9 @@ func TestRepoURL_IsValid(t *testing.T) {
t.Errorf("repoURL.IsValid() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && !cmp.Equal(tt.expected, r, cmpopts.IgnoreUnexported(repoURL{})) {
fmt.Println("expected: " + tt.expected.host + " GOT: " + r.host)
fmt.Println("expected: " + tt.expected.owner + " GOT: " + r.owner)
fmt.Println("expected: " + tt.expected.project + " GOT: " + r.project)
t.Logf("expected: %s GOT: %s", tt.expected.host, r.host)
t.Logf("expected: %s GOT: %s", tt.expected.owner, r.owner)
t.Logf("expected: %s GOT: %s", tt.expected.project, r.project)
t.Errorf("Got diff: %s", cmp.Diff(tt.expected, r))
}
if !cmp.Equal(r.Host(), tt.expected.host) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func rootCmd(o *options.Options) error {
for checkName := range enabledChecks {
fmt.Fprintf(os.Stderr, "Finished [%s]\n", checkName)
}
fmt.Println("\nRESULTS\n-------")
fmt.Fprintln(os.Stderr, "\nRESULTS\n-------")
}

resultsErr := pkg.FormatResults(
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func serveCmd(o *options.Options) *cobra.Command {
if port == "" {
port = "8080"
}
fmt.Printf("Listening on localhost:%s\n", port)
logger.Info("Listening on localhost:" + port + "\n")
//nolint: gosec // unsused.
err = http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), nil)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions cron/internal/cii/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"fmt"
"io"
"log"
"net/http"
"strings"

Expand All @@ -46,7 +47,7 @@ func writeToCIIDataBucket(ctx context.Context, pageResp []ciiPageResp, bucketURL
if err != nil {
return fmt.Errorf("error during AsJSON: %w", err)
}
fmt.Printf("Writing result for: %s\n", projectURL)
log.Printf("Writing result for: %s\n", projectURL)
if err := data.WriteToBlobStore(ctx, bucketURL,
fmt.Sprintf("%s/result.json", projectURL), jsonData); err != nil {
return fmt.Errorf("error during data.WriteToBlobStore: %w", err)
Expand Down Expand Up @@ -82,7 +83,7 @@ func getPage(ctx context.Context, pageNum int) ([]ciiPageResp, error) {

func main() {
ctx := context.Background()
fmt.Println("Starting...")
log.Println("Starting...")

flag.Parse()
if err := config.ReadConfig(); err != nil {
Expand All @@ -107,5 +108,5 @@ func main() {
panic(err)
}

fmt.Println("Job completed")
log.Println("Job completed")
}
2 changes: 1 addition & 1 deletion cron/internal/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func scriptHandler(w http.ResponseWriter, r *http.Request) {

func main() {
http.HandleFunc("/", scriptHandler)
fmt.Printf("Starting HTTP server on port 8080 ...\n")
log.Printf("Starting HTTP server on port 8080 ...\n")
// nolint:gosec // internal server.
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
Expand Down
7 changes: 4 additions & 3 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package options
import (
"errors"
"fmt"
"log"
"os"
"strings"

"github.com/caarlos0/env/v6"

"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/log"
sclog "github.com/ossf/scorecard/v4/log"
)

// Options define common options for configuring scorecard.
Expand Down Expand Up @@ -54,7 +55,7 @@ type Options struct {
func New() *Options {
opts := &Options{}
if err := env.Parse(opts); err != nil {
fmt.Printf("could not parse env vars, using default options: %v", err)
log.Printf("could not parse env vars, using default options: %v", err)
}
// Defaulting.
// TODO(options): Consider moving this to a separate function/method.
Expand Down Expand Up @@ -105,7 +106,7 @@ const (

var (
// DefaultLogLevel retrieves the default log level.
DefaultLogLevel = log.DefaultLevel.String()
DefaultLogLevel = sclog.DefaultLevel.String()

errCommitIsEmpty = errors.New("commit should be non-empty")
errFormatNotSupported = errors.New("unsupported format")
Expand Down

0 comments on commit 2d93196

Please sign in to comment.