Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37d419d

Browse files
committed
fix: template edit not change defaults if user unset
1 parent fbec79f commit 37d419d

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

cli/templateedit.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,19 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
118118
autostopRequirementDaysOfWeek = []string{}
119119
}
120120

121-
// NOTE: coderd will ignore empty fields.
121+
// Default values
122+
if !userSetOption(inv, "description") {
123+
description = template.Description
124+
}
125+
126+
if !userSetOption(inv, "icon") {
127+
icon = template.Icon
128+
}
129+
130+
if !userSetOption(inv, "display-name") {
131+
displayName = template.DisplayName
132+
}
133+
122134
req := codersdk.UpdateTemplateMeta{
123135
Name: name,
124136
DisplayName: displayName,

cli/templateedit_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,4 +1047,40 @@ func TestTemplateEdit(t *testing.T) {
10471047
require.Error(t, err)
10481048
require.ErrorContains(t, err, "appears to be an AGPL deployment")
10491049
})
1050+
t.Run("DefaultValues", func(t *testing.T) {
1051+
t.Parallel()
1052+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
1053+
owner := coderdtest.CreateFirstUser(t, client)
1054+
1055+
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
1056+
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
1057+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
1058+
ctr.Name = "random"
1059+
ctr.Icon = "/icon/foobar.png"
1060+
ctr.DisplayName = "Foobar"
1061+
ctr.Description = "Some description"
1062+
})
1063+
1064+
// We need to change some field to get a db write.
1065+
cmdArgs := []string{
1066+
"templates",
1067+
"edit",
1068+
template.Name,
1069+
"--name", "something-new",
1070+
}
1071+
inv, root := clitest.New(t, cmdArgs...)
1072+
//nolint
1073+
clitest.SetupConfig(t, client, root)
1074+
1075+
ctx := testutil.Context(t, testutil.WaitLong)
1076+
err := inv.WithContext(ctx).Run()
1077+
require.NoError(t, err)
1078+
1079+
updated, err := client.Template(context.Background(), template.ID)
1080+
require.NoError(t, err)
1081+
assert.Equal(t, "something-new", updated.Name)
1082+
assert.Equal(t, template.Icon, updated.Icon)
1083+
assert.Equal(t, template.DisplayName, updated.DisplayName)
1084+
assert.Equal(t, template.Description, updated.Description)
1085+
})
10501086
}

cli/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"golang.org/x/xerrors"
1010

11+
"github.com/coder/coder/v2/cli/clibase"
1112
"github.com/coder/coder/v2/coderd/schedule/cron"
1213
"github.com/coder/coder/v2/coderd/util/tz"
1314
)
@@ -18,6 +19,19 @@ var (
1819
errUnsupportedTimezone = xerrors.New("The location you provided looks like a timezone. Check https://ipinfo.io for your location.")
1920
)
2021

22+
// userSetOption returns true if the option was set by the user.
23+
// This is helpful if the zero value of a flag is meaningful, and you need
24+
// to distinguish between the user setting the flag to the zero value and
25+
// the user not setting the flag at all.
26+
func userSetOption(inv *clibase.Invocation, flagName string) bool {
27+
for _, opt := range inv.Command.Options {
28+
if opt.Name == flagName {
29+
return !(opt.ValueSource == clibase.ValueSourceNone || opt.ValueSource == clibase.ValueSourceDefault)
30+
}
31+
}
32+
return false
33+
}
34+
2135
// durationDisplay formats a duration for easier display:
2236
// - Durations of 24 hours or greater are displays as Xd
2337
// - Durations less than 1 minute are displayed as <1m

0 commit comments

Comments
 (0)