feat(cli): add --output={text,json} to version cmd (#7010)

* feat(cliui): add TextFormat
* feat(cli): add --format={text,json} to version cmd
This commit is contained in:
Cian Johnston
2023-04-05 13:16:05 +01:00
committed by GitHub
parent 9c4ccd76a0
commit 00d468b964
7 changed files with 192 additions and 33 deletions

View File

@ -3,6 +3,7 @@ package cliui
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
@ -171,3 +172,23 @@ func (jsonFormat) Format(_ context.Context, data any) (string, error) {
return string(outBytes), nil
}
type textFormat struct{}
var _ OutputFormat = textFormat{}
// TextFormat is a formatter that just outputs unstructured text.
// It uses fmt.Sprintf under the hood.
func TextFormat() OutputFormat {
return textFormat{}
}
func (textFormat) ID() string {
return "text"
}
func (textFormat) AttachOptions(_ *clibase.OptionSet) {}
func (textFormat) Format(_ context.Context, data any) (string, error) {
return fmt.Sprintf("%s", data), nil
}