mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
feat: Add CLI support for workspace build parameters (#5768)
* WIP * WIP * CLI: handle workspace build parameters * fix: golintci * Fix: dry run * fix * CLI: is mutable * coderd: mutable * fix: golanci * fix: richParameterFile * CLI: create unit tests * CLI: update test * Fix * fix: order * fix
This commit is contained in:
@@ -60,3 +60,56 @@ func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.ParameterSchem
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func RichParameter(cmd *cobra.Command, templateVersionParameter codersdk.TemplateVersionParameter) (string, error) {
|
||||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), Styles.Bold.Render(templateVersionParameter.Name))
|
||||
if templateVersionParameter.Description != "" {
|
||||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+strings.TrimSpace(strings.Join(strings.Split(templateVersionParameter.Description, "\n"), "\n "))+"\n")
|
||||
}
|
||||
|
||||
// TODO Implement full validation and show descriptions.
|
||||
var err error
|
||||
var value string
|
||||
if len(templateVersionParameter.Options) > 0 {
|
||||
// Move the cursor up a single line for nicer display!
|
||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
|
||||
value, err = Select(cmd, SelectOptions{
|
||||
Options: templateVersionParameterOptionValues(templateVersionParameter),
|
||||
Default: templateVersionParameter.DefaultValue,
|
||||
HideSearch: true,
|
||||
})
|
||||
if err == nil {
|
||||
_, _ = fmt.Fprintln(cmd.OutOrStdout())
|
||||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(value))
|
||||
}
|
||||
} else {
|
||||
text := "Enter a value"
|
||||
if templateVersionParameter.DefaultValue != "" {
|
||||
text += fmt.Sprintf(" (default: %q)", templateVersionParameter.DefaultValue)
|
||||
}
|
||||
text += ":"
|
||||
|
||||
value, err = Prompt(cmd, PromptOptions{
|
||||
Text: Styles.Bold.Render(text),
|
||||
})
|
||||
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
|
||||
}
|
||||
|
||||
func templateVersionParameterOptionValues(param codersdk.TemplateVersionParameter) []string {
|
||||
var options []string
|
||||
for _, opt := range param.Options {
|
||||
options = append(options, opt.Value)
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user