mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ func Test_OutputFormatter(t *testing.T) {
|
||||
require.Panics(t, func() {
|
||||
cliui.NewOutputFormatter(cliui.JSONFormat())
|
||||
})
|
||||
require.NotPanics(t, func() {
|
||||
cliui.NewOutputFormatter(cliui.JSONFormat(), cliui.TextFormat())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("NoMissingFormatID", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user