Skip to content

Commit 08f84b6

Browse files
authored
Merge branch 'coder:main' into main
2 parents 05b8034 + 38a6d54 commit 08f84b6

File tree

68 files changed

+2618
-1275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2618
-1275
lines changed

cli/clibase/option.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ func (s *OptionSet) Add(opts ...Option) {
8080
*s = append(*s, opts...)
8181
}
8282

83+
// Filter will only return options that match the given filter. (return true)
84+
func (s OptionSet) Filter(filter func(opt Option) bool) OptionSet {
85+
cpy := make(OptionSet, 0)
86+
for _, opt := range s {
87+
if filter(opt) {
88+
cpy = append(cpy, opt)
89+
}
90+
}
91+
return cpy
92+
}
93+
8394
// FlagSet returns a pflag.FlagSet for the OptionSet.
8495
func (s *OptionSet) FlagSet() *pflag.FlagSet {
8596
if s == nil {

cli/clitest/clitest.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func extractTar(t *testing.T, data []byte, directory string) {
127127
}
128128
}
129129

130-
// Start runs the command in a goroutine and cleans it up when
131-
// the test completed.
130+
// Start runs the command in a goroutine and cleans it up when the test
131+
// completed.
132132
func Start(t *testing.T, inv *clibase.Invocation) {
133133
t.Helper()
134134

@@ -170,7 +170,7 @@ func (w *ErrorWaiter) Wait() error {
170170
var ok bool
171171
w.cachedError, ok = <-w.c
172172
if !ok {
173-
panic("unexpoected channel close")
173+
panic("unexpected channel close")
174174
}
175175
})
176176
return w.cachedError
@@ -196,18 +196,18 @@ func (w *ErrorWaiter) RequireAs(want interface{}) {
196196
require.ErrorAs(w.t, w.Wait(), want)
197197
}
198198

199-
// StartWithWaiter runs the command in a goroutine but returns the error
200-
// instead of asserting it. This is useful for testing error cases.
199+
// StartWithWaiter runs the command in a goroutine but returns the error instead
200+
// of asserting it. This is useful for testing error cases.
201201
func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
202202
t.Helper()
203203

204-
errCh := make(chan error, 1)
205-
206-
var cleaningUp atomic.Bool
207-
208204
var (
209205
ctx = inv.Context()
210206
cancel func()
207+
208+
cleaningUp atomic.Bool
209+
errCh = make(chan error, 1)
210+
doneCh = make(chan struct{})
211211
)
212212
if _, ok := ctx.Deadline(); !ok {
213213
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(testutil.WaitMedium))
@@ -218,12 +218,13 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
218218
inv = inv.WithContext(ctx)
219219

220220
go func() {
221+
defer close(doneCh)
221222
defer close(errCh)
222223
err := inv.Run()
223224
if cleaningUp.Load() && errors.Is(err, context.DeadlineExceeded) {
224-
// If we're cleaning up, this error is likely related to the
225-
// CLI teardown process. E.g., the server could be slow to shut
226-
// down Postgres.
225+
// If we're cleaning up, this error is likely related to the CLI
226+
// teardown process. E.g., the server could be slow to shut down
227+
// Postgres.
227228
t.Logf("command %q timed out during test cleanup", inv.Command.FullName())
228229
}
229230
// Whether or not this fails the test is left to the caller.
@@ -235,7 +236,7 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
235236
t.Cleanup(func() {
236237
cancel()
237238
cleaningUp.Store(true)
238-
<-errCh
239+
<-doneCh
239240
})
240241
return &ErrorWaiter{c: errCh, t: t}
241242
}

cli/cliui/output.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,35 @@ func (textFormat) AttachOptions(_ *clibase.OptionSet) {}
192192
func (textFormat) Format(_ context.Context, data any) (string, error) {
193193
return fmt.Sprintf("%s", data), nil
194194
}
195+
196+
// DataChangeFormat allows manipulating the data passed to an output format.
197+
// This is because sometimes the data needs to be manipulated before it can be
198+
// passed to the output format.
199+
// For example, you may want to pass something different to the text formatter
200+
// than what you pass to the json formatter.
201+
type DataChangeFormat struct {
202+
format OutputFormat
203+
change func(data any) (any, error)
204+
}
205+
206+
// ChangeFormatterData allows manipulating the data passed to an output
207+
// format.
208+
func ChangeFormatterData(format OutputFormat, change func(data any) (any, error)) *DataChangeFormat {
209+
return &DataChangeFormat{format: format, change: change}
210+
}
211+
212+
func (d *DataChangeFormat) ID() string {
213+
return d.format.ID()
214+
}
215+
216+
func (d *DataChangeFormat) AttachOptions(opts *clibase.OptionSet) {
217+
d.format.AttachOptions(opts)
218+
}
219+
220+
func (d *DataChangeFormat) Format(ctx context.Context, data any) (string, error) {
221+
newData, err := d.change(data)
222+
if err != nil {
223+
return "", err
224+
}
225+
return d.format.Format(ctx, newData)
226+
}

cli/templateedit_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ func TestTemplateEdit(t *testing.T) {
167167

168168
// Test the cli command.
169169
displayName := "New Display Name 789"
170+
description := "New Description ABC"
170171
icon := "/icons/new-icon.png"
171172
cmdArgs := []string{
172173
"templates",
173174
"edit",
174175
template.Name,
176+
"--description", description,
175177
"--display-name", displayName,
176178
"--icon", icon,
177179
}
@@ -186,8 +188,8 @@ func TestTemplateEdit(t *testing.T) {
186188
// Assert that the template metadata changed.
187189
updated, err := client.Template(context.Background(), template.ID)
188190
require.NoError(t, err)
189-
assert.Equal(t, template.Name, updated.Name) // doesn't change
190-
assert.Equal(t, initialDescription, updated.Description) // doesn't change
191+
assert.Equal(t, template.Name, updated.Name) // doesn't change
192+
assert.Equal(t, description, updated.Description)
191193
assert.Equal(t, displayName, updated.DisplayName)
192194
assert.Equal(t, icon, updated.Icon)
193195
})
@@ -234,9 +236,9 @@ func TestTemplateEdit(t *testing.T) {
234236
require.NoError(t, err)
235237
// Properties don't change
236238
assert.Equal(t, template.Name, updated.Name)
237-
assert.Equal(t, template.Description, updated.Description)
238239
// These properties are removed, as the API considers it as "delete" request
239240
// See: https://github.com/coder/coder/issues/5066
241+
assert.Equal(t, "", updated.Description)
240242
assert.Equal(t, "", updated.Icon)
241243
assert.Equal(t, "", updated.DisplayName)
242244
})

coderd/apidoc/docs.go

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)