8000 feat(cli): add `--create` flag to `templates push` by mtojek · Pull Request #8454 · coder/coder · GitHub
[go: up one dir, main page]

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
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 36 additions & 8 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
provisionerTags []string
uploadFlags templateUploadFlags
activate bool
create bool
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Expand All @@ -185,9 +186,13 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return err
}

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

err = uploadFlags.checkForLockfile(inv)
Expand All @@ -207,19 +212,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 !createTemplate {
args.Name = versionName
args.Template = &template
args.ReuseParameters = !alwaysPrompt
}

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

if activate {
if createTemplate {
_, 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 Expand Up @@ -290,6 +312,12 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
Default: "true",
Value: clibase.BoolOf(&activate),
},
{
Flag: "create",
Description: "Create the template if it does not exist.",
Default: "false",
Value: clibase.BoolOf(&create),
},
cliui.SkipPromptOption(),
}
cmd.Options = append(cmd.Options, uploadFlags.options()...)
Expand Down
47 changes: 47 additions & 0 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -613,6 +614,52 @@ func TestTemplatePush(t *testing.T) {
require.Equal(t, "second_variable", templateVariables[1].Name)
require.Equal(t, "foobar", templateVariables[1].Value)
})

t.Run("CreateTemplate", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
ProvisionApply: provisionCompleteWithAgent,
})

const templateName = "my-template"
args := []string{
"templates",
"push",
templateName,
"--directory", source,
"--test.provisioner", string(database.ProvisionerTypeEcho),
"--create",
}
inv, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

waiter := clitest.StartWithWaiter(t, inv)

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
{match: "template has been created"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
if m.write != "" {
pty.WriteLine(m.write)
}
}

waiter.RequireSuccess()

template, err := client.TemplateByName(context.Background(), user.OrganizationID, templateName)
require.NoError(t, err)
require.Equal(t, templateName, template.Name)
require.NotEqual(t, uuid.Nil, template.ActiveVersionID)
})
})
}

Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_templates_push_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Push a new template version from the current directory or as specified by flag
Always prompt all parameters. Does not pull parameter values from
active template version.

--create bool (default: false)
Create the template if it does not exist.

-d, --directory string (default: .)
Specify the directory to create from, use '-' to read tar from stdin.

Expand Down
9 changes: 9 additions & 0 deletions docs/cli/templates_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ Whether the new template will be marked active.

Always prompt all parameters. Does not pull parameter values from active template version.

### --create

| | |
| ------- | ------------------ |
| Type | <code>bool</code> |
| Default | <code>false</code> |

Create the template if it does not exist.

### -d, --directory

| | |
Expand Down
0