feat: Validate workspace build parameters (#5807)

This commit is contained in:
Marcin Tojek
2023-01-24 14:22:00 +01:00
committed by GitHub
parent 138887de7e
commit 26c69525d1
23 changed files with 1051 additions and 268 deletions
+39
View File
@@ -9,6 +9,9 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"github.com/coder/coder/codersdk"
)
func init() {
@@ -42,6 +45,42 @@ type SelectOptions struct {
HideSearch bool
}
type RichSelectOptions struct {
Options []codersdk.TemplateVersionParameterOption
Default string
Size int
HideSearch bool
}
// RichSelect displays a list of user options including name and description.
func RichSelect(cmd *cobra.Command, richOptions RichSelectOptions) (*codersdk.TemplateVersionParameterOption, error) {
opts := make([]string, len(richOptions.Options))
for i, option := range richOptions.Options {
line := option.Name
if len(option.Description) > 0 {
line += ": " + option.Description
}
opts[i] = line
}
selected, err := Select(cmd, SelectOptions{
Options: opts,
Default: richOptions.Default,
Size: richOptions.Size,
HideSearch: richOptions.HideSearch,
})
if err != nil {
return nil, err
}
for i, option := range opts {
if option == selected {
return &richOptions.Options[i], nil
}
}
return nil, xerrors.Errorf("unknown option selected: %s", selected)
}
// Select displays a list of user options.
func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
// The survey library used *always* fails when testing on Windows,