Skip to content

Commit 4075ddd

Browse files
authored
Color errors and warnings (#360)
1 parent 8e69415 commit 4075ddd

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

internal/cmd/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
3333
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
3434

35-
"github.com/fatih/color"
3635
"github.com/spf13/cobra"
3736
"github.com/spf13/viper"
3837
)
@@ -96,7 +95,7 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command {
9695
}
9796

9897
func beautifyUsageTemplate(cmd *cobra.Command) {
99-
cobra.AddTemplateFunc("WhiteBold", color.New(color.FgHiWhite, color.Bold).SprintFunc())
98+
cobra.AddTemplateFunc("WhiteBold", print.WhiteBold)
10099
usageTemplate := cmd.UsageTemplate()
101100
usageTemplate = strings.NewReplacer(
102101
`Usage:`, `{{WhiteBold "USAGE"}}`,

internal/pkg/print/print.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os/exec"
1212
"strings"
1313

14+
"github.com/fatih/color"
1415
"github.com/lmittmann/tint"
1516
"github.com/mattn/go-colorable"
1617
"github.com/spf13/cobra"
@@ -33,7 +34,13 @@ const (
3334
YAMLOutputFormat = "yaml"
3435
)
3536

36-
var errAborted = errors.New("operation aborted")
37+
var (
38+
errAborted = errors.New("operation aborted")
39+
40+
WhiteBold = color.New(color.FgHiWhite, color.Bold).SprintFunc()
41+
RedBold = color.New(color.FgHiRed, color.Bold).SprintFunc()
42+
YellowBold = color.New(color.FgHiYellow, color.Bold).SprintFunc()
43+
)
3744

3845
type Printer struct {
3946
Cmd *cobra.Command
@@ -106,13 +113,13 @@ func (p *Printer) Warn(msg string, args ...any) {
106113
return
107114
}
108115
warning := fmt.Sprintf(msg, args...)
109-
p.Cmd.PrintErrf("Warning: %s", warning)
116+
p.Cmd.PrintErrf("%s %s", YellowBold("Warning:"), warning)
110117
}
111118

112119
// Print an Error level output to the defined Err output (falling back to Stderr if not set).
113120
func (p *Printer) Error(msg string, args ...any) {
114121
err := fmt.Sprintf(msg, args...)
115-
p.Cmd.PrintErrln(p.Cmd.ErrPrefix(), err)
122+
p.Cmd.PrintErrln(RedBold(p.Cmd.ErrPrefix()), err)
116123
}
117124

118125
// Prompts the user for confirmation.

0 commit comments

Comments
 (0)