Skip to content

fix: prompt when parameter options are incompatible #9247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 23, 2023
Prev Previous commit
Next Next commit
Prompt for changed parameter option
  • Loading branch information
mtojek committed Aug 22, 2023
commit 1e41500fdeaaa671e80f69f9d1a4628f50b714a3
29 changes: 28 additions & 1 deletion cli/parameterresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ next:
continue // immutables should not be passed to consecutive builds
}

if len(tvp.Options) > 0 && !isValidTemplateParameterOption(buildParameter, tvp.Options) {
continue // do not propagate invalid options
}

for i, r := range resolved {
if r.Name == buildParameter.Name {
resolved[i].Value = buildParameter.Value
Expand Down Expand Up @@ -180,10 +184,11 @@ func (pr *ParameterResolver) resolveWithInput(resolved []codersdk.WorkspaceBuild
// Parameter has not been resolved yet, so CLI needs to determine if user should input it.

firstTimeUse := pr.isFirstTimeUse(tvp.Name)

promptParameterOption := pr.isLastBuildParameterInvalidOption(tvp)
if (tvp.Ephemeral && pr.promptBuildOptions) ||
(action == WorkspaceCreate && tvp.Required) ||
(action == WorkspaceCreate && !tvp.Ephemeral) ||
(action == WorkspaceUpdate && promptParameterOption) ||
(action == WorkspaceUpdate && !tvp.Mutable && firstTimeUse) ||
(action == WorkspaceUpdate && tvp.Mutable && !tvp.Ephemeral && pr.promptRichParameters) {
parameterValue, err := cliui.RichParameter(inv, tvp)
Expand All @@ -206,6 +211,19 @@ func (pr *ParameterResolver) isFirstTimeUse(parameterName string) bool {
return findWorkspaceBuildParameter(parameterName, pr.lastBuildParameters) == nil
}

func (pr *ParameterResolver) isLastBuildParameterInvalidOption(templateVersionParameter codersdk.TemplateVersionParameter) bool {
if len(templateVersionParameter.Options) == 0 {
return false
}

for _, buildParameter := range pr.lastBuildParameters {
if buildParameter.Name == templateVersionParameter.Name {
return !isValidTemplateParameterOption(buildParameter, templateVersionParameter.Options)
}
}
return false
}

func findTemplateVersionParameter(workspaceBuildParameter codersdk.WorkspaceBuildParameter, templateVersionParameters []codersdk.TemplateVersionParameter) *codersdk.TemplateVersionParameter {
for _, tvp := range templateVersionParameters {
if tvp.Name == workspaceBuildParameter.Name {
Expand All @@ -223,3 +241,12 @@ func findWorkspaceBuildParameter(parameterName string, params []codersdk.Workspa
}
return nil
}

func isValidTemplateParameterOption(buildParameter codersdk.WorkspaceBuildParameter, options []codersdk.TemplateVersionParameterOption) bool {
for _, opt := range options {
if opt.Value == buildParameter.Value {
return true
}
}
return false
}
3 changes: 2 additions & 1 deletion cli/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,8 @@ func TestUpdateValidateRichParameters(t *testing.T) {
}()

matches := []string{
"aaaa", "",
stringParameterName, "second_option",
"Planning workspace...", "",
}
for i := 0; i < len(matches); i += 2 {
match := matches[i]
Expand Down