Merge pull request #8793 from priyawadhwa/warning

Implement Warning type for JSON output
pull/8744/head
priyawadhwa 2020-07-21 18:17:54 -04:00 committed by GitHub
commit 6df5d19d70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 0 deletions

View File

@ -150,6 +150,10 @@ func FatalT(format string, a ...V) {
// WarningT is a shortcut for writing a templated warning message to stderr
func WarningT(format string, a ...V) {
if JSON {
register.PrintWarning(ApplyTemplateFormatting(Warning, useColor, format, a...))
return
}
ErrT(Warning, format, a...)
}

View File

@ -39,3 +39,9 @@ func PrintDownloadProgress(artifact, progress string) {
s := NewDownloadProgress(artifact, progress)
printAsCloudEvent(s, s.data)
}
// PrintWarning prints a Warning type in JSON format
func PrintWarning(warning string) {
w := NewWarning(warning)
printAsCloudEvent(w, w.data)
}

View File

@ -63,3 +63,23 @@ func TestPrintInfo(t *testing.T) {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
}
func TestWarning(t *testing.T) {
expected := `{"data":{"message":"warning"},"datacontenttype":"application/json","id":"random-id","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.warning"}`
expected += "\n"
buf := bytes.NewBuffer([]byte{})
outputFile = buf
defer func() { outputFile = os.Stdout }()
getUUID = func() string {
return "random-id"
}
PrintWarning("warning")
actual := buf.String()
if actual != expected {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
}

View File

@ -83,8 +83,19 @@ func NewDownloadProgress(artifact, progress string) *DownloadProgress {
// Warning will be used to notify the user of warnings
type Warning struct {
data map[string]string
}
// NewWarning returns a new warning type
func NewWarning(warning string) *Warning {
return &Warning{
map[string]string{
"message": warning,
},
}
}
// Type returns the cloud events compatible type of this struct
func (s *Warning) Type() string {
return "io.k8s.sigs.minikube.warning"
}