Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 use forbidigo linter to prevent print statements #3585

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 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 @@ -73,6 +74,9 @@ linters-settings:
exhaustive:
# https://golangci-lint.run/usage/linters/#exhaustive
default-signifies-exhaustive: true
forbidigo:
forbid:
- 'fmt\.Print.*(# 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 Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stdout, err) // can this just be stderr?

Check warning on line 117 in attestor/command/cli.go

View check run for this annotation

Codecov / codecov/patch

attestor/command/cli.go#L117

Added line #L117 was not covered by tests
raghavkaul marked this conversation as resolved.
Show resolved Hide resolved
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 @@
for checkName := range enabledChecks {
fmt.Fprintf(os.Stderr, "Finished [%s]\n", checkName)
}
fmt.Println("\nRESULTS\n-------")
fmt.Fprintln(os.Stderr, "\nRESULTS\n-------")

Check warning on line 154 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L154

Added line #L154 was not covered by tests
}

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 @@
if port == "" {
port = "8080"
}
fmt.Printf("Listening on localhost:%s\n", port)
logger.Info("Listening on localhost:" + port + "\n")

Check warning on line 98 in cmd/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/serve.go#L98

Added line #L98 was not covered by tests
//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 @@
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 @@
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)

Check warning on line 58 in options/options.go

View check run for this annotation

Codecov / codecov/patch

options/options.go#L58

Added line #L58 was not covered by tests
}
// Defaulting.
// TODO(options): Consider moving this to a separate function/method.
Expand Down Expand Up @@ -105,7 +106,7 @@

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
Loading