mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
931b97caab
## Description
This PR introduces a new `list presets` command to display the presets
associated with a given template.
By default, it displays the presets for the template's active version,
unless a `--template-version` flag is provided.
## Changes
* Added a new `list presets` command under `coder templates presets` to
display presets associated with a template.
* By default, the command lists presets from the template’s active
version.
* Users can override the default behavior by providing the
`--template-version` flag to target a specific version.
```
> coder templates versions presets list --help
USAGE:
coder templates presets list [flags] <template>
List all presets of the specified template. Defaults to the active template version.
OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Select which organization (uuid or name) to use.
-c, --column [name|parameters|default|desired prebuild instances] (default: name,parameters,default,desired prebuild instances)
Columns to display in table output.
-o, --output table|json (default: table)
Output format.
--template-version string
Specify a template version to list presets for. Defaults to the active version.
```
Related PR: https://github.com/coder/coder/pull/18912 - please consider
both PRs together as they’re part of the same workflow
Relates to issue: https://github.com/coder/coder/issues/16594
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added CLI commands to manage and list presets for specific template
versions, supporting tabular and JSON output.
* Introduced a new CLI subcommand group for template version presets,
including detailed help and documentation.
* Added support for displaying and managing the desired number of
prebuild instances for presets in CLI, API, and UI.
* **Documentation**
* Updated and expanded CLI and API documentation to describe new
commands, options, and the desired prebuild instances field in presets.
* Added new help output and reference files for template version presets
commands.
* **Bug Fixes**
* Ensured correct handling and display of the desired prebuild instances
property for presets across CLI, API, and UI.
* **Tests**
* Introduced end-to-end tests for listing template version presets,
covering scenarios with and without presets.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
115 lines
3.6 KiB
Go
115 lines
3.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/pretty"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) templates() *serpent.Command {
|
|
cmd := &serpent.Command{
|
|
Use: "templates",
|
|
Short: "Manage templates",
|
|
Long: "Templates are written in standard Terraform and describe the infrastructure for workspaces\n" + FormatExamples(
|
|
Example{
|
|
Description: "Create or push an update to the template. Your developers can update their workspaces",
|
|
Command: "coder templates push my-template",
|
|
},
|
|
),
|
|
Aliases: []string{"template"},
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
return inv.Command.HelpHandler(inv)
|
|
},
|
|
Children: []*serpent.Command{
|
|
r.templateCreate(),
|
|
r.templateEdit(),
|
|
r.templateInit(),
|
|
r.templateList(),
|
|
r.templatePush(),
|
|
r.templateVersions(),
|
|
r.templatePresets(),
|
|
r.templateDelete(),
|
|
r.templatePull(),
|
|
r.archiveTemplateVersions(),
|
|
},
|
|
}
|
|
|
|
return cmd
|
|
}
|
|
|
|
func selectTemplate(inv *serpent.Invocation, client *codersdk.Client, organization codersdk.Organization) (codersdk.Template, error) {
|
|
var empty codersdk.Template
|
|
ctx := inv.Context()
|
|
allTemplates, err := client.TemplatesByOrganization(ctx, organization.ID)
|
|
if err != nil {
|
|
return empty, xerrors.Errorf("get templates by organization: %w", err)
|
|
}
|
|
|
|
if len(allTemplates) == 0 {
|
|
return empty, xerrors.Errorf("no templates exist in the current organization %q", organization.Name)
|
|
}
|
|
|
|
opts := make([]string, 0, len(allTemplates))
|
|
for _, template := range allTemplates {
|
|
opts = append(opts, template.Name)
|
|
}
|
|
|
|
selection, err := cliui.Select(inv, cliui.SelectOptions{
|
|
Options: opts,
|
|
})
|
|
if err != nil {
|
|
return empty, xerrors.Errorf("select template: %w", err)
|
|
}
|
|
|
|
for _, template := range allTemplates {
|
|
if template.Name == selection {
|
|
return template, nil
|
|
}
|
|
}
|
|
return empty, xerrors.Errorf("no template selected")
|
|
}
|
|
|
|
type templateTableRow struct {
|
|
// Used by json format:
|
|
Template codersdk.Template
|
|
|
|
// Used by table format:
|
|
Name string `json:"-" table:"name,default_sort"`
|
|
CreatedAt string `json:"-" table:"created at"`
|
|
LastUpdated string `json:"-" table:"last updated"`
|
|
OrganizationID uuid.UUID `json:"-" table:"organization id"`
|
|
OrganizationName string `json:"-" table:"organization name"`
|
|
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
|
|
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
|
|
UsedBy string `json:"-" table:"used by"`
|
|
DefaultTTL time.Duration `json:"-" table:"default ttl"`
|
|
}
|
|
|
|
// templateToRows converts a list of templates to a list of templateTableRow for
|
|
// outputting.
|
|
func templatesToRows(templates ...codersdk.Template) []templateTableRow {
|
|
rows := make([]templateTableRow, len(templates))
|
|
for i, template := range templates {
|
|
rows[i] = templateTableRow{
|
|
Template: template,
|
|
Name: template.Name,
|
|
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
|
|
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
|
|
OrganizationID: template.OrganizationID,
|
|
OrganizationName: template.OrganizationName,
|
|
Provisioner: template.Provisioner,
|
|
ActiveVersionID: template.ActiveVersionID,
|
|
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
|
|
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
|
|
}
|
|
}
|
|
|
|
return rows
|
|
}
|