Skip to content

Commit 2cf99bc

Browse files
author
Massimiliano Pippi
committed
make Print format gnostic
1 parent 8f73b78 commit 2cf99bc

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

cli/feedback/exported.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func Printf(format string, v ...interface{}) {
5252
}
5353

5454
// Print behaves like fmt.Print but writes on the out writer and adds a newline.
55-
func Print(v ...interface{}) {
56-
fb.Print(v...)
55+
func Print(v interface{}) {
56+
fb.Print(v)
5757
}
5858

5959
// Errorf behaves like fmt.Printf but writes on the error writer and adds a
@@ -74,7 +74,9 @@ func PrintJSON(v interface{}) {
7474
fb.PrintJSON(v)
7575
}
7676

77-
// PrintResult is a convenient wrapper...
77+
// PrintResult is a convenient wrapper to provide feedback for complex data,
78+
// where the contents can't be just serialized to JSON but requires more
79+
// structure.
7880
func PrintResult(res Result) {
7981
fb.PrintResult(res)
8082
}

cli/feedback/feedback.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ func (fb *Feedback) Printf(format string, v ...interface{}) {
8686
}
8787

8888
// Print behaves like fmt.Print but writes on the out writer and adds a newline.
89-
func (fb *Feedback) Print(v ...interface{}) {
90-
fmt.Fprintln(fb.out, v...)
89+
func (fb *Feedback) Print(v interface{}) {
90+
if fb.format == JSON {
91+
fb.PrintJSON(v)
92+
} else {
93+
fmt.Fprintln(fb.out, v)
94+
}
9195
}
9296

9397
// Errorf behaves like fmt.Printf but writes on the error writer and adds a
@@ -109,18 +113,16 @@ func (fb *Feedback) PrintJSON(v interface{}) {
109113
if d, err := json.MarshalIndent(v, "", " "); err != nil {
110114
fb.Errorf("Error during JSON encoding of the output: %v", err)
111115
} else {
112-
fb.Print(string(d))
116+
fmt.Fprint(fb.out, string(d))
113117
}
114118
}
115119

116-
// PrintResult is a convenient wrapper...
120+
// PrintResult is a convenient wrapper to provide feedback for complex data,
121+
// where the contents can't be just serialized to JSON but requires more
122+
// structure.
117123
func (fb *Feedback) PrintResult(res Result) {
118124
if fb.format == JSON {
119-
if d, err := json.MarshalIndent(res.Data(), "", " "); err != nil {
120-
fb.Errorf("Error during JSON encoding of the output: %v", err)
121-
} else {
122-
fb.Print(string(d))
123-
}
125+
fb.PrintJSON(res.Data())
124126
} else {
125127
fb.Print(fmt.Sprintf("%s", res))
126128
}

0 commit comments

Comments
 (0)