Skip to content

feat(cli): add --create flag to templates push #8454

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create the template if it does not exist
  • Loading branch information
mtojek committed Jul 12, 2023
commit ceebc7580dabb7e70581ef0013c0cfd9c55de815
36 changes: 28 additions & 8 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return err
}

var templateExists bool
template, err := client.TemplateByName(inv.Context(), organization.ID, name)
if err != nil {
if create && err == nil {
templateExists = true
} else if !create && err != nil {
return err
}

Expand All @@ -208,19 +211,24 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return err
}

job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{
Name: versionName,
args := createValidTemplateVersionArgs{
Message: message,
Client: client,
Organization: organization,
Provisioner: database.ProvisionerType(provisioner),
FileID: resp.ID,
ProvisionerTags: tags,
VariablesFile: variablesFile,
Variables: variables,
Template: &template,
ReuseParameters: !alwaysPrompt,
ProvisionerTags: tags,
})
}

if templateExists {
args.Name = versionName
args.Template = &template
args.ReuseParameters = !alwaysPrompt
}

job, err := createValidTemplateVersion(inv, args)
if err != nil {
return err
}
Expand All @@ -229,7 +237,19 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return xerrors.Errorf("job failed: %s", job.Job.Status)
}

if activate {
if !templateExists {
_, err = client.CreateTemplate(inv.Context(), organization.ID, codersdk.CreateTemplateRequest{
Name: name,
VersionID: job.ID,
})
if err != nil {
return err
}

_, _ = fmt.Fprintln(inv.Stdout, "\n"+cliui.DefaultStyles.Wrap.Render(
"The "+cliui.DefaultStyles.Keyword.Render(name)+" template has been created at "+cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))+"! "+
"Developers can provision a workspace with this template using:")+"\n")
} else if activate {
err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
ID: job.ID,
})
Expand Down