Make warning messages more visible (#7097)

* 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 98a6554c34.

* Ignore '===' warning header and footer for doc automation tests
This commit is contained in:
Armel Soro
2023-09-26 08:53:45 +02:00
committed by GitHub
parent 134a0867b2
commit d34fbaed1a
4 changed files with 96 additions and 17 deletions

View File

@@ -292,8 +292,7 @@ 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...)
}
}
@@ -301,10 +300,7 @@ func Fwarning(out io.Writer, a ...interface{}) {
//
// ⚠ <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
@@ -313,10 +309,28 @@ 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
//
// ✓ <message>
@@ -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)
}
}

64
pkg/log/status_test.go Normal file
View File

@@ -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)
}
})
}
}

View File

@@ -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"
)
@@ -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)
}
}