Skip to content

Commit 0dbbb0c

Browse files
committed
354 problems left...
- Fix output formats
1 parent 14b4c6e commit 0dbbb0c

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

cli/cliui/output.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"strings"
88

99
"github.com/coder/coder/cli/clibase"
10+
"github.com/spf13/pflag"
1011
"golang.org/x/xerrors"
1112
)
1213

1314
type OutputFormat interface {
1415
ID() string
15-
AttachFlags(cmd *clibase.Command)
16+
AttachFlags(fs *pflag.FlagSet)
1617
Format(ctx context.Context, data any) (string, error)
1718
}
1819

@@ -47,17 +48,19 @@ func NewOutputFormatter(formats ...OutputFormat) *OutputFormatter {
4748

4849
// AttachFlags attaches the --output flag to the given command, and any
4950
// additional flags required by the output formatters.
50-
func (f *OutputFormatter) AttachFlags(cmd *clibase.Command) {
51+
func (f *OutputFormatter) AttachFlags(cmd *clibase.Command) *pflag.FlagSet {
52+
fs := cmd.Options.FlagSet()
5153
for _, format := range f.formats {
52-
format.AttachFlags(cmd)
54+
format.AttachFlags(fs)
5355
}
5456

5557
formatNames := make([]string, 0, len(f.formats))
5658
for _, format := range f.formats {
5759
formatNames = append(formatNames, format.ID())
5860
}
5961

60-
cmd.Flags().StringVarP(&f.formatID, "output", "o", f.formats[0].ID(), "Output format. Available formats: "+strings.Join(formatNames, ", "))
62+
fs.StringVarP(&f.formatID, "output", "o", f.formats[0].ID(), "Output format. Available formats: "+strings.Join(formatNames, ", "))
63+
return fs
6164
}
6265

6366
// Format formats the given data using the format specified by the --output
@@ -119,8 +122,8 @@ func (*tableFormat) ID() string {
119122
}
120123

121124
// AttachFlags implements OutputFormat.
122-
func (f *tableFormat) AttachFlags(cmd *clibase.Command) {
123-
cmd.Flags().StringSliceVarP(&f.columns, "column", "c", f.defaultColumns, "Columns to display in table output. Available columns: "+strings.Join(f.allColumns, ", "))
125+
func (f *tableFormat) AttachFlags(fs *pflag.FlagSet) {
126+
fs.StringSliceVarP(&f.columns, "column", "c", f.defaultColumns, "Columns to display in table output. Available columns: "+strings.Join(f.allColumns, ", "))
124127
}
125128

126129
// Format implements OutputFormat.
@@ -143,7 +146,7 @@ func (jsonFormat) ID() string {
143146
}
144147

145148
// AttachFlags implements OutputFormat.
146-
func (jsonFormat) AttachFlags(_ *clibase.Command) {}
149+
func (jsonFormat) AttachFlags(_ *pflag.FlagSet) {}
147150

148151
// Format implements OutputFormat.
149152
func (jsonFormat) Format(_ context.Context, data any) (string, error) {

cli/cliui/output_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync/atomic"
77
"testing"
88

9+
"github.com/spf13/pflag"
910
"github.com/stretchr/testify/require"
1011

1112
"github.com/coder/coder/cli/clibase"
@@ -14,7 +15,7 @@ import (
1415

1516
type format struct {
1617
id string
17-
attachFlagsFn func(cmd *clibase.Command)
18+
attachFlagsFn func(fs *pflag.FlagSet)
1819
formatFn func(ctx context.Context, data any) (string, error)
1920
}
2021

@@ -24,9 +25,9 @@ func (f *format) ID() string {
2425
return f.id
2526
}
2627

27-
func (f *format) AttachFlags(cmd *clibase.Command) {
28+
func (f *format) AttachFlags(fs *pflag.FlagSet) {
2829
if f.attachFlagsFn != nil {
29-
f.attachFlagsFn(cmd)
30+
f.attachFlagsFn(fs)
3031
}
3132
}
3233

@@ -82,8 +83,8 @@ func Test_OutputFormatter(t *testing.T) {
8283
cliui.JSONFormat(),
8384
&format{
8485
id: "foo",
85-
attachFlagsFn: func(cmd *clibase.Command) {
86-
cmd.Flags().StringP("foo", "f", "", "foo flag 1234")
86+
attachFlagsFn: func(fs *pflag.FlagSet) {
87+
fs.StringP("foo", "f", "", "foo flag 1234")
8788
},
8889
formatFn: func(_ context.Context, _ any) (string, error) {
8990
atomic.AddInt64(&called, 1)
@@ -93,12 +94,12 @@ func Test_OutputFormatter(t *testing.T) {
9394
)
9495

9596
cmd := &clibase.Command{}
96-
f.AttachFlags(cmd)
97+
fs := f.AttachFlags(cmd)
9798

98-
selected, err := cmd.Flags().GetString("output")
99+
selected, err := fs.GetString("output")
99100
require.NoError(t, err)
100101
require.Equal(t, "json", selected)
101-
usage := cmd.Flags().FlagUsages()
102+
usage := fs.FlagUsages()
102103
require.Contains(t, usage, "Available formats: json, foo")
103104
require.Contains(t, usage, "foo flag 1234")
104105

@@ -112,13 +113,13 @@ func Test_OutputFormatter(t *testing.T) {
112113
require.Equal(t, data, got)
113114
require.EqualValues(t, 0, atomic.LoadInt64(&called))
114115

115-
require.NoError(t, cmd.Flags().Set("output", "foo"))
116+
require.NoError(t, fs.Set("output", "foo"))
116117
out, err = f.Format(ctx, data)
117118
require.NoError(t, err)
118119
require.Equal(t, "foo", out)
119120
require.EqualValues(t, 1, atomic.LoadInt64(&called))
120121

121-
require.NoError(t, cmd.Flags().Set("output", "bar"))
122+
require.NoError(t, fs.Set("output", "bar"))
122123
out, err = f.Format(ctx, data)
123124
require.Error(t, err)
124125
require.ErrorContains(t, err, "bar")

0 commit comments

Comments
 (0)