Merge pull request #8793 from priyawadhwa/warning
Implement Warning type for JSON outputpull/8744/head
commit
6df5d19d70
|
@ -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...)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue