2022-03-22 19:17:50 +00:00
|
|
|
package cliui
|
|
|
|
|
|
|
|
import (
|
2023-03-16 16:17:48 +00:00
|
|
|
"encoding/json"
|
2022-03-22 19:17:50 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
"github.com/coder/coder/cli/clibase"
|
2022-03-22 19:17:50 +00:00
|
|
|
"github.com/coder/coder/coderd/parameter"
|
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
)
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
func ParameterSchema(inv *clibase.Invocation, parameterSchema codersdk.ParameterSchema) (string, error) {
|
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, Styles.Bold.Render("var."+parameterSchema.Name))
|
2022-03-22 19:17:50 +00:00
|
|
|
if parameterSchema.Description != "" {
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, " "+strings.TrimSpace(strings.Join(strings.Split(parameterSchema.Description, "\n"), "\n "))+"\n")
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var options []string
|
|
|
|
if parameterSchema.ValidationCondition != "" {
|
|
|
|
options, _, err = parameter.Contains(parameterSchema.ValidationCondition)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var value string
|
|
|
|
if len(options) > 0 {
|
|
|
|
// Move the cursor up a single line for nicer display!
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprint(inv.Stdout, "\033[1A")
|
|
|
|
value, err = Select(inv, SelectOptions{
|
2022-03-22 19:17:50 +00:00
|
|
|
Options: options,
|
2022-05-19 12:49:40 +00:00
|
|
|
Default: parameterSchema.DefaultSourceValue,
|
2022-03-22 19:17:50 +00:00
|
|
|
HideSearch: true,
|
|
|
|
})
|
|
|
|
if err == nil {
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprintln(inv.Stdout)
|
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, " "+Styles.Prompt.String()+Styles.Field.Render(value))
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-19 12:49:40 +00:00
|
|
|
text := "Enter a value"
|
|
|
|
if parameterSchema.DefaultSourceValue != "" {
|
|
|
|
text += fmt.Sprintf(" (default: %q)", parameterSchema.DefaultSourceValue)
|
|
|
|
}
|
|
|
|
text += ":"
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
value, err = Prompt(inv, PromptOptions{
|
2022-05-19 12:49:40 +00:00
|
|
|
Text: Styles.Bold.Render(text),
|
2022-03-22 19:17:50 +00:00
|
|
|
})
|
2022-07-11 16:46:03 +00:00
|
|
|
value = strings.TrimSpace(value)
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
2022-05-19 12:49:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they didn't specify anything, use the default value if set.
|
|
|
|
if len(options) == 0 && value == "" {
|
|
|
|
value = parameterSchema.DefaultSourceValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
2023-01-23 14:01:22 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
func RichParameter(inv *clibase.Invocation, templateVersionParameter codersdk.TemplateVersionParameter) (string, error) {
|
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, Styles.Bold.Render(templateVersionParameter.Name))
|
2023-02-08 11:57:12 +00:00
|
|
|
if templateVersionParameter.DescriptionPlaintext != "" {
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, " "+strings.TrimSpace(strings.Join(strings.Split(templateVersionParameter.DescriptionPlaintext, "\n"), "\n "))+"\n")
|
2023-01-23 14:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var value string
|
2023-03-16 16:17:48 +00:00
|
|
|
if templateVersionParameter.Type == "list(string)" {
|
|
|
|
// Move the cursor up a single line for nicer display!
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprint(inv.Stdout, "\033[1A")
|
2023-03-16 16:17:48 +00:00
|
|
|
|
|
|
|
var options []string
|
|
|
|
err = json.Unmarshal([]byte(templateVersionParameter.DefaultValue), &options)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
values, err := MultiSelect(inv, options)
|
2023-03-16 16:17:48 +00:00
|
|
|
if err == nil {
|
|
|
|
v, err := json.Marshal(&values)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprintln(inv.Stdout)
|
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, " "+Styles.Prompt.String()+Styles.Field.Render(strings.Join(values, ", ")))
|
2023-03-16 16:17:48 +00:00
|
|
|
value = string(v)
|
|
|
|
}
|
|
|
|
} else if len(templateVersionParameter.Options) > 0 {
|
2023-01-23 14:01:22 +00:00
|
|
|
// Move the cursor up a single line for nicer display!
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprint(inv.Stdout, "\033[1A")
|
2023-01-24 13:22:00 +00:00
|
|
|
var richParameterOption *codersdk.TemplateVersionParameterOption
|
2023-03-23 22:42:20 +00:00
|
|
|
richParameterOption, err = RichSelect(inv, RichSelectOptions{
|
2023-01-24 13:22:00 +00:00
|
|
|
Options: templateVersionParameter.Options,
|
2023-01-23 14:01:22 +00:00
|
|
|
Default: templateVersionParameter.DefaultValue,
|
|
|
|
HideSearch: true,
|
|
|
|
})
|
|
|
|
if err == nil {
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprintln(inv.Stdout)
|
|
|
|
_, _ = fmt.Fprintln(inv.Stdout, " "+Styles.Prompt.String()+Styles.Field.Render(richParameterOption.Name))
|
2023-01-24 13:22:00 +00:00
|
|
|
value = richParameterOption.Value
|
2023-01-23 14:01:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text := "Enter a value"
|
2023-03-07 15:38:31 +00:00
|
|
|
if !templateVersionParameter.Required {
|
2023-01-23 14:01:22 +00:00
|
|
|
text += fmt.Sprintf(" (default: %q)", templateVersionParameter.DefaultValue)
|
|
|
|
}
|
|
|
|
text += ":"
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
value, err = Prompt(inv, PromptOptions{
|
2023-01-23 14:01:22 +00:00
|
|
|
Text: Styles.Bold.Render(text),
|
2023-01-24 13:22:00 +00:00
|
|
|
Validate: func(value string) error {
|
|
|
|
return validateRichPrompt(value, templateVersionParameter)
|
|
|
|
},
|
2023-01-23 14:01:22 +00:00
|
|
|
})
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they didn't specify anything, use the default value if set.
|
|
|
|
if len(templateVersionParameter.Options) == 0 && value == "" {
|
|
|
|
value = templateVersionParameter.DefaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:22:00 +00:00
|
|
|
func validateRichPrompt(value string, p codersdk.TemplateVersionParameter) error {
|
2023-03-07 15:38:31 +00:00
|
|
|
return codersdk.ValidateWorkspaceBuildParameter(p, &codersdk.WorkspaceBuildParameter{
|
2023-01-24 13:22:00 +00:00
|
|
|
Name: p.Name,
|
|
|
|
Value: value,
|
2023-02-07 08:36:13 +00:00
|
|
|
}, nil)
|
2023-01-23 14:01:22 +00:00
|
|
|
}
|