From 37d419dd43d654961f2925c9a61ec76505b7d1e1 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 20 Nov 2023 09:20:04 -0600 Subject: [PATCH 1/2] fix: template edit not change defaults if user unset --- cli/templateedit.go | 14 +++++++++++++- cli/templateedit_test.go | 36 ++++++++++++++++++++++++++++++++++++ cli/util.go | 14 ++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cli/templateedit.go b/cli/templateedit.go index 1c17ec52bcab3..daa9d6eaccfeb 100644 --- a/cli/templateedit.go +++ b/cli/templateedit.go @@ -118,7 +118,19 @@ func (r *RootCmd) templateEdit() *clibase.Cmd { autostopRequirementDaysOfWeek = []string{} } - // NOTE: coderd will ignore empty fields. + // Default values + if !userSetOption(inv, "description") { + description = template.Description + } + + if !userSetOption(inv, "icon") { + icon = template.Icon + } + + if !userSetOption(inv, "display-name") { + displayName = template.DisplayName + } + req := codersdk.UpdateTemplateMeta{ Name: name, DisplayName: displayName, diff --git a/cli/templateedit_test.go b/cli/templateedit_test.go index de2e52894a444..a17933ca98d19 100644 --- a/cli/templateedit_test.go +++ b/cli/templateedit_test.go @@ -1047,4 +1047,40 @@ func TestTemplateEdit(t *testing.T) { require.Error(t, err) require.ErrorContains(t, err, "appears to be an AGPL deployment") }) + t.Run("DefaultValues", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) + owner := coderdtest.CreateFirstUser(t, client) + + version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil) + _ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) + template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) { + ctr.Name = "random" + ctr.Icon = "/icon/foobar.png" + ctr.DisplayName = "Foobar" + ctr.Description = "Some description" + }) + + // We need to change some field to get a db write. + cmdArgs := []string{ + "templates", + "edit", + template.Name, + "--name", "something-new", + } + inv, root := clitest.New(t, cmdArgs...) + //nolint + clitest.SetupConfig(t, client, root) + + ctx := testutil.Context(t, testutil.WaitLong) + err := inv.WithContext(ctx).Run() + require.NoError(t, err) + + updated, err := client.Template(context.Background(), template.ID) + require.NoError(t, err) + assert.Equal(t, "something-new", updated.Name) + assert.Equal(t, template.Icon, updated.Icon) + assert.Equal(t, template.DisplayName, updated.DisplayName) + assert.Equal(t, template.Description, updated.Description) + }) } diff --git a/cli/util.go b/cli/util.go index 90b745e003b10..ad221fa31cce5 100644 --- a/cli/util.go +++ b/cli/util.go @@ -8,6 +8,7 @@ import ( "golang.org/x/xerrors" + "github.com/coder/coder/v2/cli/clibase" "github.com/coder/coder/v2/coderd/schedule/cron" "github.com/coder/coder/v2/coderd/util/tz" ) @@ -18,6 +19,19 @@ var ( errUnsupportedTimezone = xerrors.New("The location you provided looks like a timezone. Check https://ipinfo.io for your location.") ) +// userSetOption returns true if the option was set by the user. +// This is helpful if the zero value of a flag is meaningful, and you need +// to distinguish between the user setting the flag to the zero value and +// the user not setting the flag at all. +func userSetOption(inv *clibase.Invocation, flagName string) bool { + for _, opt := range inv.Command.Options { + if opt.Name == flagName { + return !(opt.ValueSource == clibase.ValueSourceNone || opt.ValueSource == clibase.ValueSourceDefault) + } + } + return false +} + // durationDisplay formats a duration for easier display: // - Durations of 24 hours or greater are displays as Xd // - Durations less than 1 minute are displayed as <1m From dd05c602108527234c1cd41ea2ff51e7d488f4a7 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 20 Nov 2023 11:48:14 -0600 Subject: [PATCH 2/2] fix unit test --- cli/templateedit_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/templateedit_test.go b/cli/templateedit_test.go index a17933ca98d19..49c27c89e89bf 100644 --- a/cli/templateedit_test.go +++ b/cli/templateedit_test.go @@ -228,6 +228,9 @@ func TestTemplateEdit(t *testing.T) { "templates", "edit", template.Name, + "--description", "", + "--display-name", "", + "--icon", "", } inv, root := clitest.New(t, cmdArgs...) clitest.SetupConfig(t, templateAdmin, root)