- Notifications
You must be signed in to change notification settings - Fork928
feat: add--version
flag tocoder templates pull
, default to active version#10153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -15,12 +15,15 @@ import ( | ||
) | ||
func (r *RootCmd) templatePull() *clibase.Cmd { | ||
var ( | ||
tarMode bool | ||
versionName string | ||
) | ||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
Use: "pull <name> [destination]", | ||
Short: "Download theactive,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] | ||
} | ||
organization, err := CurrentOrganization(inv, client) | ||
if err != nil { | ||
return xerrors.Errorf("getcurrent organization: %w", err) | ||
} | ||
template, err := client.TemplateByName(ctx, organization.ID, templateName) | ||
if err != nil { | ||
return xerrors.Errorf("gettemplate by name: %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] | ||
} | ||
Comment on lines +52 to 73 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You should move this into the switch when you pick latest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I need to know the latest version for the warning when the version isn't specified. | ||
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 | ||
} | ||
cliui.Info(inv.Stderr, "Pulling template version "+cliui.Bold(templateVersion.Name)+"...") | ||
// Download the tar archive. | ||
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(), | ||
} | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.