Skip to content

Commit

Permalink
Make warning messages more visible (#7097)
Browse files Browse the repository at this point in the history
* Wrap warning messages to make them more visible

For example, instead of displaying:
```
⚠  You are using "default" project, odo may not work as expected in the default project.
```

We are now displaying:
```
========================================================================================
⚠  You are using "default" project, odo may not work as expected in the default project.
========================================================================================
```

* Display warning message about default project/namespace in a single block

* Add unit test

* Fix sample outputs for doc automation tests

* Revert "Fix sample outputs for doc automation tests"

This reverts commit 98a6554.

* Ignore '===' warning header and footer for doc automation tests
  • Loading branch information
rm3l authored Sep 26, 2023
1 parent 134a086 commit d34fbae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
38 changes: 24 additions & 14 deletions pkg/log/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,15 @@ func Warning(a ...interface{}) {
// ⚠ <message>
func Fwarning(out io.Writer, a ...interface{}) {
if !IsJSON() {
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(out, "%s%s%s%s", prefixSpacing, yellow(getWarningString()), suffixSpacing, fmt.Sprintln(a...))
Fwarningf(out, "%s", a...)
}
}

// Warningf will output in an appropriate "warning" manner
//
// ⚠ <message>
func Warningf(format string, a ...interface{}) {
if !IsJSON() {
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(GetStderr(), " %s%s%s\n", yellow(getWarningString()), suffixSpacing, fmt.Sprintf(format, a...))
}
Fwarningf(GetStderr(), format, a...)
}

// Fwarningf will output in an appropriate "warning" manner
Expand All @@ -313,8 +309,26 @@ func Warningf(format string, a ...interface{}) {
func Fwarningf(w io.Writer, format string, a ...interface{}) {
if !IsJSON() {
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(w, " %s%s%s\n", yellow(getWarningString()), suffixSpacing, fmt.Sprintf(format, a...))
fullMessage := fmt.Sprintf("%s%s%s", getWarningString(), suffixSpacing, fmt.Sprintf(format, a...))
fmt.Fprintln(w, yellow(wrapWarningMessage(fullMessage)))
}
}

func wrapWarningMessage(fullMessage string) string {
if fullMessage == "" || strings.TrimSpace(fullMessage) == "" {
return fullMessage
}
split := strings.Split(fullMessage, "\n")
max := 0
for _, s := range split {
if len(s) > max {
max = len(s)
}
}
h := strings.Repeat("=", max)
return fmt.Sprintf(`%[1]s
%[2]s
%[1]s`, h, fullMessage, h)
}

// Fsuccess will output in an appropriate "progress" manner in out writer
Expand All @@ -330,13 +344,9 @@ func Fsuccess(out io.Writer, a ...interface{}) {
// DisplayExperimentalWarning displays the experimental mode warning message.
func DisplayExperimentalWarning() {
if !IsJSON() {
yellow := color.New(color.FgYellow).SprintFunc()
h := "============================================================================"
fmt.Fprintln(GetStdout(), yellow(fmt.Sprintf(`%[1]s
%s Experimental mode enabled. Use at your own risk.
More details on https://odo.dev/docs/user-guides/advanced/experimental-mode
%[1]s
`, h, getWarningString())))
msg := `Experimental mode enabled. Use at your own risk.
More details on https://odo.dev/docs/user-guides/advanced/experimental-mode`
Fwarningf(GetStdout(), msg)
}
}

Expand Down
64 changes: 64 additions & 0 deletions pkg/log/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package log

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func Test_wrapWarningMessage(t *testing.T) {
type args struct {
fullMessage string
}
tests := []struct {
name string
args args
want string
}{
{
name: "empty message",
args: args{
fullMessage: "",
},
want: "",
},
{
name: "single-line message",
args: args{
fullMessage: "Lorem Ipsum Dolor Sit Amet",
},
want: `==========================
Lorem Ipsum Dolor Sit Amet
==========================`,
},
{
name: "multi-line message",
args: args{
fullMessage: `
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean vel faucibus ex.
Nulla in magna sem.
Vivamus condimentum ultricies erat, in ullamcorper risus tempor nec.
Nam sed risus blandit,
`,
},
want: `====================================================================
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean vel faucibus ex.
Nulla in magna sem.
Vivamus condimentum ultricies erat, in ullamcorper risus tempor nec.
Nam sed risus blandit,
====================================================================`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapWarningMessage(tt.args.fullMessage)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("wrapWarningMessage() mismatch (-want +got):\n%s", diff)
}
})
}
}
8 changes: 5 additions & 3 deletions pkg/odo/genericclioptions/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package genericclioptions

import (
"fmt"

v1 "k8s.io/api/core/v1"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/log"
pkgUtil "github.com/redhat-developer/odo/pkg/util"
v1 "k8s.io/api/core/v1"

dfutil "github.com/devfile/library/v2/pkg/util"
)
Expand Down Expand Up @@ -47,7 +49,7 @@ func WarnIfDefaultNamespace(namespace string, kubeClient kclient.ClientInterface
noun = "project"
}
fmt.Println()
log.Warningf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", noun)
log.Warningf("You may set a new %[1]s by running `odo create %[1]s <name>`, or set an existing one by running `odo set %[1]s <name>`", noun)
log.Warningf(`You are using "default" %[1]s, odo may not work as expected in the default %[1]s.
You may set a new %[1]s by running `+"`odo create %[1]s <name>`, or set an existing one by running `odo set %[1]s <name>`", noun)
}
}
3 changes: 3 additions & 0 deletions tests/helper/helper_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func StripSpinner(docString string) (returnString string) {
if strings.HasPrefix(line, "⚠") && !strings.Contains(line, "Pod is Pending") {
continue
}
if strings.HasPrefix(line, "===") {
continue
}

// for some reason, splitting the docString by \n does not split the spinner frames,
// so we perform a side operation to remove the extra spinner frames that are not present in the final output
Expand Down

0 comments on commit d34fbae

Please sign in to comment.