Skip to content

Make commands output agnostic #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 9, 2019
Next Next commit
make Print format gnostic
  • Loading branch information
Massimiliano Pippi committed Sep 20, 2019
commit 2cf99bc047f80c1cae7d9c79c8699af8e76be766
8 changes: 5 additions & 3 deletions cli/feedback/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func Printf(format string, v ...interface{}) {
}

// Print behaves like fmt.Print but writes on the out writer and adds a newline.
func Print(v ...interface{}) {
fb.Print(v...)
func Print(v interface{}) {
fb.Print(v)
}

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

// PrintResult is a convenient wrapper...
// PrintResult is a convenient wrapper to provide feedback for complex data,
// where the contents can't be just serialized to JSON but requires more
// structure.
func PrintResult(res Result) {
fb.PrintResult(res)
}
20 changes: 11 additions & 9 deletions cli/feedback/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ func (fb *Feedback) Printf(format string, v ...interface{}) {
}

// Print behaves like fmt.Print but writes on the out writer and adds a newline.
func (fb *Feedback) Print(v ...interface{}) {
fmt.Fprintln(fb.out, v...)
func (fb *Feedback) Print(v interface{}) {
if fb.format == JSON {
fb.PrintJSON(v)
} else {
fmt.Fprintln(fb.out, v)
}
}

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

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