Skip to content

Commit 83cd15c

Browse files
xcoulonrajivnathan
andauthored
fix: avoid non-constant format string in calls (#1314)
add method that do not need extra variadic args to format the message (causing build failures in Go 1.26) --------- Signed-off-by: Xavier Coulon <xcoulon@redhat.com> Co-authored-by: Rajiv Senthilnathan <rajivnathan@gmail.com>
1 parent 45fbf1c commit 83cd15c

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

setup/cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ func setup(cmd *cobra.Command, _ []string) { // nolint:gocyclo
155155
tokenRequestURI, err := auth.GetTokenRequestURI(cl)
156156
errMsg := "a token is required to capture metrics, use oc login with token to log into the cluster. eg. `oc login --token=<token> --server=<server>`"
157157
if err != nil {
158-
term.Fatalf(err, errMsg)
158+
term.Fatal(err, errMsg)
159159
}
160-
term.Fatalf(fmt.Errorf("a token can be requested from %s", tokenRequestURI), errMsg)
160+
term.Fatal(fmt.Errorf("a token can be requested from %s", tokenRequestURI), errMsg)
161161
}
162162
}
163163

setup/metrics/gather_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/codeready-toolchain/toolchain-e2e/setup/metrics/queries"
10+
"github.com/codeready-toolchain/toolchain-e2e/setup/terminal"
1011
"github.com/stretchr/testify/require"
1112

1213
"github.com/codeready-toolchain/toolchain-common/pkg/test"
@@ -237,11 +238,16 @@ func TestComputeResultsZeroSamples(t *testing.T) {
237238

238239
type noopTerminal struct{}
239240

241+
var _ terminal.Terminal = &noopTerminal{}
242+
240243
func (t *noopTerminal) InOrStdin() io.Reader { return nil }
241244
func (t *noopTerminal) OutOrStdout() io.Writer { return io.Discard }
242245
func (t *noopTerminal) Debugf(msg string, args ...interface{}) {}
246+
func (t *noopTerminal) Info(msg string) {}
243247
func (t *noopTerminal) Infof(msg string, args ...interface{}) {}
248+
func (t *noopTerminal) Error(err error, msg string) {}
244249
func (t *noopTerminal) Errorf(err error, msg string, args ...interface{}) {}
245250
func (t *noopTerminal) Fatalf(err error, msg string, args ...interface{}) { panic("unexpected Fatalf") }
251+
func (t *noopTerminal) Fatal(err error, msg string) { panic("unexpected Fatal") }
246252
func (t *noopTerminal) PromptBoolf(msg string, args ...interface{}) bool { return false }
247253
func (t *noopTerminal) AddPreFatalExitHook(func()) {}

setup/results/results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@ func (r *Results) OutputResults() {
8282
r.term.Fatalf(err, "failed to write results")
8383
}
8484

85-
r.term.Infof("\nResults file: " + cfg.ResultsFilepath())
85+
r.term.Info("\nResults file: " + cfg.ResultsFilepath())
8686
}

setup/terminal/terminal.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ type Terminal interface {
1717
InOrStdin() io.Reader
1818
OutOrStdout() io.Writer
1919
Debugf(msg string, args ...interface{})
20+
Info(msg string)
2021
Infof(msg string, args ...interface{})
22+
Error(err error, msg string)
2123
Errorf(err error, msg string, args ...interface{})
24+
Fatal(err error, msg string)
2225
Fatalf(err error, msg string, args ...interface{})
2326
PromptBoolf(msg string, args ...interface{}) bool
2427
AddPreFatalExitHook(func())
@@ -65,6 +68,15 @@ func (t *DefaultTerminal) Debugf(msg string, args ...interface{}) {
6568
fmt.Fprintln(t.OutOrStdout(), fmt.Sprintf(msg, args...))
6669
}
6770

71+
// Info displays a message with the default color
72+
func (t *DefaultTerminal) Info(msg string) {
73+
if msg == "" {
74+
fmt.Fprintln(t.OutOrStdout(), "")
75+
return
76+
}
77+
fmt.Fprintln(t.OutOrStdout(), msg)
78+
}
79+
6880
// Infof displays a message with the default color
6981
func (t *DefaultTerminal) Infof(msg string, args ...interface{}) {
7082
if msg == "" {
@@ -74,11 +86,25 @@ func (t *DefaultTerminal) Infof(msg string, args ...interface{}) {
7486
fmt.Fprintln(t.OutOrStdout(), fmt.Sprintf(msg, args...))
7587
}
7688

89+
// Error prints a message with the red color
90+
func (t *DefaultTerminal) Error(err error, msg string) {
91+
color.New(color.FgRed).Fprintln(t.OutOrStdout(), fmt.Sprintf("%s: %s", msg, err.Error())) // nolint:errcheck
92+
}
93+
7794
// Errorf prints a message with the red color
7895
func (t *DefaultTerminal) Errorf(err error, msg string, args ...interface{}) {
7996
color.New(color.FgRed).Fprintln(t.OutOrStdout(), fmt.Sprintf("%s: %s", fmt.Sprintf(msg, args...), err.Error())) // nolint:errcheck
8097
}
8198

99+
// Fatal prints a message with the red color and exits the program with a `1` return code
100+
func (t *DefaultTerminal) Fatal(err error, msg string) {
101+
defer os.Exit(1)
102+
for _, hook := range t.fatalExitHooks {
103+
hook()
104+
}
105+
t.Error(err, msg)
106+
}
107+
82108
// Fatalf prints a message with the red color and exits the program with a `1` return code
83109
func (t *DefaultTerminal) Fatalf(err error, msg string, args ...interface{}) {
84110
defer os.Exit(1)

0 commit comments

Comments
 (0)