fix: prevent change in defaults if user unsets in template edit (#10793)

* fix: template edit not change defaults if user unset
This commit is contained in:
Steven Masley
2023-11-20 18:14:30 -06:00
committed by GitHub
parent e6dc9eeffc
commit 7060069034
3 changed files with 68 additions and 6 deletions
+14
View File
@@ -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