Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions setup/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ func setup(cmd *cobra.Command, _ []string) { // nolint:gocyclo
tokenRequestURI, err := auth.GetTokenRequestURI(cl)
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>`"
if err != nil {
term.Fatalf(err, errMsg)
term.Fatal(err, errMsg)
}
term.Fatalf(fmt.Errorf("a token can be requested from %s", tokenRequestURI), errMsg)
term.Fatal(fmt.Errorf("a token can be requested from %s", tokenRequestURI), errMsg)
}
}

Expand Down
6 changes: 6 additions & 0 deletions setup/metrics/gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/codeready-toolchain/toolchain-e2e/setup/metrics/queries"
"github.com/codeready-toolchain/toolchain-e2e/setup/terminal"
"github.com/stretchr/testify/require"

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

type noopTerminal struct{}

var _ terminal.Terminal = &noopTerminal{}

func (t *noopTerminal) InOrStdin() io.Reader { return nil }
func (t *noopTerminal) OutOrStdout() io.Writer { return io.Discard }
func (t *noopTerminal) Debugf(msg string, args ...interface{}) {}
func (t *noopTerminal) Info(msg string) {}
func (t *noopTerminal) Infof(msg string, args ...interface{}) {}
func (t *noopTerminal) Error(err error, msg string) {}
func (t *noopTerminal) Errorf(err error, msg string, args ...interface{}) {}
func (t *noopTerminal) Fatalf(err error, msg string, args ...interface{}) { panic("unexpected Fatalf") }
func (t *noopTerminal) Fatal(err error, msg string) { panic("unexpected Fatal") }
func (t *noopTerminal) PromptBoolf(msg string, args ...interface{}) bool { return false }
func (t *noopTerminal) AddPreFatalExitHook(func()) {}
2 changes: 1 addition & 1 deletion setup/results/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ func (r *Results) OutputResults() {
r.term.Fatalf(err, "failed to write results")
}

r.term.Infof("\nResults file: " + cfg.ResultsFilepath())
r.term.Info("\nResults file: " + cfg.ResultsFilepath())
}
26 changes: 26 additions & 0 deletions setup/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ type Terminal interface {
InOrStdin() io.Reader
OutOrStdout() io.Writer
Debugf(msg string, args ...interface{})
Info(msg string)
Infof(msg string, args ...interface{})
Error(err error, msg string)
Errorf(err error, msg string, args ...interface{})
Fatal(err error, msg string)
Fatalf(err error, msg string, args ...interface{})
PromptBoolf(msg string, args ...interface{}) bool
AddPreFatalExitHook(func())
Expand Down Expand Up @@ -65,6 +68,15 @@ func (t *DefaultTerminal) Debugf(msg string, args ...interface{}) {
fmt.Fprintln(t.OutOrStdout(), fmt.Sprintf(msg, args...))
}

// Info displays a message with the default color
func (t *DefaultTerminal) Info(msg string) {
if msg == "" {
fmt.Fprintln(t.OutOrStdout(), "")
return
}
fmt.Fprintln(t.OutOrStdout(), msg)
}

// Infof displays a message with the default color
func (t *DefaultTerminal) Infof(msg string, args ...interface{}) {
if msg == "" {
Expand All @@ -74,11 +86,25 @@ func (t *DefaultTerminal) Infof(msg string, args ...interface{}) {
fmt.Fprintln(t.OutOrStdout(), fmt.Sprintf(msg, args...))
}

// Error prints a message with the red color
func (t *DefaultTerminal) Error(err error, msg string) {
color.New(color.FgRed).Fprintln(t.OutOrStdout(), fmt.Sprintf("%s: %s", msg, err.Error())) // nolint:errcheck
}

// Errorf prints a message with the red color
func (t *DefaultTerminal) Errorf(err error, msg string, args ...interface{}) {
color.New(color.FgRed).Fprintln(t.OutOrStdout(), fmt.Sprintf("%s: %s", fmt.Sprintf(msg, args...), err.Error())) // nolint:errcheck
}

// Fatal prints a message with the red color and exits the program with a `1` return code
func (t *DefaultTerminal) Fatal(err error, msg string) {
defer os.Exit(1)
for _, hook := range t.fatalExitHooks {
hook()
}
t.Error(err, msg)
}

// Fatalf prints a message with the red color and exits the program with a `1` return code
func (t *DefaultTerminal) Fatalf(err error, msg string, args ...interface{}) {
defer os.Exit(1)
Expand Down
Loading