mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add --version flag to coder templates pull, default to active version (#10153)
Fixes https://github.com/coder/coder/issues/9837
This commit is contained in:
+58
-21
@@ -15,12 +15,15 @@ import (
|
||||
)
|
||||
|
||||
func (r *RootCmd) templatePull() *clibase.Cmd {
|
||||
var tarMode bool
|
||||
var (
|
||||
tarMode bool
|
||||
versionName string
|
||||
)
|
||||
|
||||
client := new(codersdk.Client)
|
||||
cmd := &clibase.Cmd{
|
||||
Use: "pull <name> [destination]",
|
||||
Short: "Download the latest version of a template to a path.",
|
||||
Short: "Download the active, latest, or specified version of a template to a path.",
|
||||
Middleware: clibase.Chain(
|
||||
clibase.RequireRangeArgs(1, 2),
|
||||
r.InitClient(client),
|
||||
@@ -36,39 +39,67 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
|
||||
dest = inv.Args[1]
|
||||
}
|
||||
|
||||
// TODO(JonA): Do we need to add a flag for organization?
|
||||
organization, err := CurrentOrganization(inv, client)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("current organization: %w", err)
|
||||
return xerrors.Errorf("get current organization: %w", err)
|
||||
}
|
||||
|
||||
template, err := client.TemplateByName(ctx, organization.ID, templateName)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("template by name: %w", err)
|
||||
return xerrors.Errorf("get template by name: %w", err)
|
||||
}
|
||||
|
||||
// Pull the versions for the template. We'll find the latest
|
||||
// one and download the source.
|
||||
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
|
||||
TemplateID: template.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("template versions by template: %w", err)
|
||||
var latestVersion codersdk.TemplateVersion
|
||||
{
|
||||
// Determine the latest template version and compare with the
|
||||
// active version. If they aren't the same, warn the user.
|
||||
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
|
||||
TemplateID: template.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("template versions by template: %w", err)
|
||||
}
|
||||
|
||||
if len(versions) == 0 {
|
||||
return xerrors.Errorf("no template versions for template %q", templateName)
|
||||
}
|
||||
|
||||
// Sort the slice from newest to oldest template.
|
||||
sort.SliceStable(versions, func(i, j int) bool {
|
||||
return versions[i].CreatedAt.After(versions[j].CreatedAt)
|
||||
})
|
||||
|
||||
latestVersion = versions[0]
|
||||
}
|
||||
|
||||
if len(versions) == 0 {
|
||||
return xerrors.Errorf("no template versions for template %q", templateName)
|
||||
var templateVersion codersdk.TemplateVersion
|
||||
switch versionName {
|
||||
case "", "active":
|
||||
activeVersion, err := client.TemplateVersion(ctx, template.ActiveVersionID)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get active template version: %w", err)
|
||||
}
|
||||
if versionName == "" && activeVersion.ID != latestVersion.ID {
|
||||
cliui.Warn(inv.Stderr,
|
||||
"A newer template version than the active version exists. Pulling the active version instead.",
|
||||
"Use "+cliui.Code("--template latest")+" to pull the latest version.",
|
||||
)
|
||||
}
|
||||
templateVersion = activeVersion
|
||||
case "latest":
|
||||
templateVersion = latestVersion
|
||||
default:
|
||||
version, err := client.TemplateVersionByName(ctx, template.ID, versionName)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get template version: %w", err)
|
||||
}
|
||||
templateVersion = version
|
||||
}
|
||||
|
||||
// Sort the slice from newest to oldest template.
|
||||
sort.SliceStable(versions, func(i, j int) bool {
|
||||
return versions[i].CreatedAt.After(versions[j].CreatedAt)
|
||||
})
|
||||
|
||||
latest := versions[0]
|
||||
cliui.Info(inv.Stderr, "Pulling template version "+cliui.Bold(templateVersion.Name)+"...")
|
||||
|
||||
// Download the tar archive.
|
||||
raw, ctype, err := client.Download(ctx, latest.Job.FileID)
|
||||
raw, ctype, err := client.Download(ctx, templateVersion.Job.FileID)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("download template: %w", err)
|
||||
}
|
||||
@@ -121,6 +152,12 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
|
||||
|
||||
Value: clibase.BoolOf(&tarMode),
|
||||
},
|
||||
{
|
||||
Description: "The name of the template version to pull. Use 'active' to pull the active version, 'latest' to pull the latest version, or the name of the template version to pull.",
|
||||
Flag: "version",
|
||||
|
||||
Value: clibase.StringOf(&versionName),
|
||||
},
|
||||
cliui.SkipPromptOption(),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user