From 26fc837598dacb8aabda46f453966d62229a76a2 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Fri, 16 Jun 2023 13:00:05 +0300 Subject: [PATCH 01/23] ci: check for unused packages --- .github/workflows/ci.yaml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b8e73ca7e1e76..59545364a0397 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -181,6 +181,27 @@ jobs: - name: Check for unstaged files run: ./scripts/check_unstaged.sh + + - name: Check for unsued npm packages + run: | + depcheck || true + if [ -f unused.json ]; then + echo "Unused dependencies found:" + cat unused.json + echo "Please review and remove these dependencies manually." + exit 1 + fi + + - name: Check for unsued go packages + run: | + go mod tidy + git diff --exit-code -- go.mod go.sum + if [ $? -ne 0 ]; then + echo "Unused Go dependencies found:" + git diff go.mod go.sum + echo "Please review and remove these dependencies manually." + exit 1 + fi fmt: runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} From d12525125556f5821efd8d25360a1afc51d5d666 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 10:06:43 +0000 Subject: [PATCH 02/23] make fmt --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 59545364a0397..6f8706c34d862 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -181,7 +181,7 @@ jobs: - name: Check for unstaged files run: ./scripts/check_unstaged.sh - + - name: Check for unsued npm packages run: | depcheck || true From f235f7c9f8de644d4a7b66bc3dfade0ee4fffcff Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 10:20:48 +0000 Subject: [PATCH 03/23] move to lint check --- .github/workflows/ci.yaml | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6f8706c34d862..226f71f1dbf39 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -140,6 +140,27 @@ jobs: run: | make --output-sync=line -j lint + - name: Check for unsued npm packages + run: | + depcheck || true + if [ -f unused.json ]; then + echo "Unused dependencies found:" + cat unused.json + echo "Please review and remove these dependencies manually." + exit 1 + fi + + - name: Check for unsued go packages + run: | + go mod tidy + git diff --exit-code -- go.mod go.sum + if [ $? -ne 0 ]; then + echo "Unused Go dependencies found:" + git diff go.mod go.sum + echo "Please review and remove these dependencies manually." + exit 1 + fi + gen: timeout-minutes: 8 runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} @@ -182,27 +203,6 @@ jobs: - name: Check for unstaged files run: ./scripts/check_unstaged.sh - - name: Check for unsued npm packages - run: | - depcheck || true - if [ -f unused.json ]; then - echo "Unused dependencies found:" - cat unused.json - echo "Please review and remove these dependencies manually." - exit 1 - fi - - - name: Check for unsued go packages - run: | - go mod tidy - git diff --exit-code -- go.mod go.sum - if [ $? -ne 0 ]; then - echo "Unused Go dependencies found:" - git diff go.mod go.sum - echo "Please review and remove these dependencies manually." - exit 1 - fi - fmt: runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} timeout-minutes: 5 From f9b2ab243759e6da6063113716bd22653387af2e Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 10:33:51 +0000 Subject: [PATCH 04/23] move to Makefile --- .github/workflows/ci.yaml | 23 +---------------------- Makefile | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 226f71f1dbf39..6ad6f4e27d82b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -140,27 +140,6 @@ jobs: run: | make --output-sync=line -j lint - - name: Check for unsued npm packages - run: | - depcheck || true - if [ -f unused.json ]; then - echo "Unused dependencies found:" - cat unused.json - echo "Please review and remove these dependencies manually." - exit 1 - fi - - - name: Check for unsued go packages - run: | - go mod tidy - git diff --exit-code -- go.mod go.sum - if [ $? -ne 0 ]; then - echo "Unused Go dependencies found:" - git diff go.mod go.sum - echo "Please review and remove these dependencies manually." - exit 1 - fi - gen: timeout-minutes: 8 runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} @@ -174,7 +153,7 @@ jobs: - name: Install sqlc run: | - curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.17.2/sqlc_1.17.2_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc + curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.18.0/sqlc_1.18.0_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc - name: go install tools run: | diff --git a/Makefile b/Makefile index a96f216ee088f..5a142f2345891 100644 --- a/Makefile +++ b/Makefile @@ -408,12 +408,27 @@ lint: lint/shellcheck lint/go lint/ts lint/helm lint/ts: cd site yarn && yarn lint + depcheck || true + if [ -f unused.json ]; then + echo "Unused dependencies found:" + cat unused.json + echo "Please review and remove these dependencies manually." + exit 1 + fi .PHONY: lint/ts lint/go: ./scripts/check_enterprise_imports.sh go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.2 golangci-lint run + go mod tidy + git diff --exit-code -- go.mod go.sum + if [ $? -ne 0 ]; then + echo "Unused Go dependencies found:" + git diff go.mod go.sum + echo "Please review and remove these dependencies manually." + exit 1 + fi .PHONY: lint/go # Use shfmt to determine the shell files, takes editorconfig into consideration. From d926d21db7171b8b686bfe740adcff6f50863a79 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 10:36:24 +0000 Subject: [PATCH 05/23] update sqlc --- .github/workflows/security.yaml | 2 +- dogfood/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/security.yaml b/.github/workflows/security.yaml index 90bbcc78ca32f..256daa6e3ae7b 100644 --- a/.github/workflows/security.yaml +++ b/.github/workflows/security.yaml @@ -75,7 +75,7 @@ jobs: - name: Install sqlc run: | - curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.17.2/sqlc_1.17.2_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc + curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.18.0/sqlc_1.18.0_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc - name: Install yq run: go run github.com/mikefarah/yq/v4@v4.30.6 - name: Install mockgen diff --git a/dogfood/Dockerfile b/dogfood/Dockerfile index cf9b3a017b7e6..4bb0e45088fbc 100644 --- a/dogfood/Dockerfile +++ b/dogfood/Dockerfile @@ -53,7 +53,7 @@ RUN mkdir --parents "$GOPATH" && \ # charts and values files go install github.com/norwoodj/helm-docs/cmd/helm-docs@v1.5.0 && \ # sqlc for Go code generation - go install github.com/kyleconroy/sqlc/cmd/sqlc@v1.17.2 && \ + go install github.com/kyleconroy/sqlc/cmd/sqlc@v1.18.0 && \ # gcr-cleaner-cli used by CI to prune unused images go install github.com/sethvargo/gcr-cleaner/cmd/gcr-cleaner-cli@v0.5.1 && \ # ruleguard for checking custom rules, without needing to run all of From 919974583c0668af3cba87aa87c53e9b09c178aa Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 11:42:49 +0000 Subject: [PATCH 06/23] install depcheck --- .github/actions/setup-node/action.yaml | 3 +++ dogfood/Dockerfile | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/actions/setup-node/action.yaml b/.github/actions/setup-node/action.yaml index aa10f5302d825..ba1c5ae530a9f 100644 --- a/.github/actions/setup-node/action.yaml +++ b/.github/actions/setup-node/action.yaml @@ -13,3 +13,6 @@ runs: - name: Install node_modules shell: bash run: ./scripts/yarn_install.sh + - name: Install depcheck + shell: bash + run : npm install -g depcheck diff --git a/dogfood/Dockerfile b/dogfood/Dockerfile index 4bb0e45088fbc..cf6736a724f36 100644 --- a/dogfood/Dockerfile +++ b/dogfood/Dockerfile @@ -191,7 +191,7 @@ RUN apt-get update && \ google-chrome-stable microsoft-edge-beta && \ # Pre-install system dependencies that Playwright needs. npx doesn't work here # for some reason. See https://github.com/microsoft/playwright-cli/issues/136 - npm i -g playwright@1.19.1 && playwright install-deps + npm i -g playwright@1.19.1 && playwright install-deps depcheck # Ensure PostgreSQL binaries are in the users $PATH. RUN update-alternatives --install /usr/local/bin/initdb initdb /usr/lib/postgresql/13/bin/initdb 100 && \ From a85a0aa6477dfd1082a96505d8e1910b1d257b62 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 13:32:16 +0000 Subject: [PATCH 07/23] revert wrong merge --- .github/actions/setup-node/action.yaml | 3 --- Makefile | 15 --------------- dogfood/Dockerfile | 2 +- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/.github/actions/setup-node/action.yaml b/.github/actions/setup-node/action.yaml index ba1c5ae530a9f..aa10f5302d825 100644 --- a/.github/actions/setup-node/action.yaml +++ b/.github/actions/setup-node/action.yaml @@ -13,6 +13,3 @@ runs: - name: Install node_modules shell: bash run: ./scripts/yarn_install.sh - - name: Install depcheck - shell: bash - run : npm install -g depcheck diff --git a/Makefile b/Makefile index 5a142f2345891..a96f216ee088f 100644 --- a/Makefile +++ b/Makefile @@ -408,27 +408,12 @@ lint: lint/shellcheck lint/go lint/ts lint/helm lint/ts: cd site yarn && yarn lint - depcheck || true - if [ -f unused.json ]; then - echo "Unused dependencies found:" - cat unused.json - echo "Please review and remove these dependencies manually." - exit 1 - fi .PHONY: lint/ts lint/go: ./scripts/check_enterprise_imports.sh go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.2 golangci-lint run - go mod tidy - git diff --exit-code -- go.mod go.sum - if [ $? -ne 0 ]; then - echo "Unused Go dependencies found:" - git diff go.mod go.sum - echo "Please review and remove these dependencies manually." - exit 1 - fi .PHONY: lint/go # Use shfmt to determine the shell files, takes editorconfig into consideration. diff --git a/dogfood/Dockerfile b/dogfood/Dockerfile index cf6736a724f36..4bb0e45088fbc 100644 --- a/dogfood/Dockerfile +++ b/dogfood/Dockerfile @@ -191,7 +191,7 @@ RUN apt-get update && \ google-chrome-stable microsoft-edge-beta && \ # Pre-install system dependencies that Playwright needs. npx doesn't work here # for some reason. See https://github.com/microsoft/playwright-cli/issues/136 - npm i -g playwright@1.19.1 && playwright install-deps depcheck + npm i -g playwright@1.19.1 && playwright install-deps # Ensure PostgreSQL binaries are in the users $PATH. RUN update-alternatives --install /usr/local/bin/initdb initdb /usr/lib/postgresql/13/bin/initdb 100 && \ From c9220af4fcd128d46a7ab29c9856ae7ac3fd931b Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 13:38:25 +0000 Subject: [PATCH 08/23] wip --- cli/templatepush.go | 76 ++++++++++++++++++++++++---------------- cli/templatepush_test.go | 32 +++++++++++++++++ 2 files changed, 78 insertions(+), 30 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 29201b8510b9f..8729fae8f2b04 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -13,6 +13,7 @@ import ( "github.com/coder/coder/cli/clibase" "github.com/coder/coder/cli/cliui" "github.com/coder/coder/coderd/database" + "github.com/coder/coder/coderd/util/ptr" "github.com/coder/coder/codersdk" "github.com/coder/coder/provisionersdk" ) @@ -116,75 +117,84 @@ func (r *RootCmd) templatePush() *clibase.Cmd { provisionerTags []string uploadFlags templateUploadFlags activate bool + create bool ) client := new(codersdk.Client) cmd := &clibase.Cmd{ - Use: "push [template]", - Short: "Push a new template version from the current directory or as specified by flag", - Middleware: clibase.Chain( - clibase.RequireRangeArgs(0, 1), - r.InitClient(client), - ), + Parent: &clibase.Cmd{}, + Children: []*clibase.Cmd{}, + Use: "push [template]", + Aliases: []string{}, + Short: "Push a new template version from the current directory or as specified by flag", + Hidden: false, + RawArgs: false, + Long: "", + Options: []clibase.Option{}, + Annotations: map[string]string{}, + Middleware: clibase.Chain(clibase.RequireRangeArgs(0, 1), r.InitClient(client)), Handler: func(inv *clibase.Invocation) error { uploadFlags.setWorkdir(workdir) - organization, err := CurrentOrganization(inv, client) if err != nil { return err } - name, err := uploadFlags.templateName(inv.Args) if err != nil { return err } - template, err := client.TemplateByName(inv.Context(), organization.ID, name) if err != nil { + if create { + resp, err := uploadFlags.upload(inv, client) + if err != nil { + return err + } + tags, err := ParseProvisionerTags(provisionerTags) + if err != nil { + return err + } + job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{Client: client, Organization: organization, Provisioner: database.ProvisionerType(provisioner), FileID: resp.ID, ProvisionerTags: tags, VariablesFile: variablesFile, Variables: variables}) + if err != nil { + return err + } + defaultTTL := 24 * time.Hour + failureTTL := 0 * time.Hour + inactivityTTL := 0 * time.Hour + disableEveryone := false + createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} + template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) + if err != nil { + return err + } + } return err } - resp, err := uploadFlags.upload(inv, client) if err != nil { return err } - tags, err := ParseProvisionerTags(provisionerTags) if err != nil { return err } - - job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ - Name: versionName, - Client: client, - Organization: organization, - Provisioner: database.ProvisionerType(provisioner), - FileID: resp.ID, - VariablesFile: variablesFile, - Variables: variables, - Template: &template, - ReuseParameters: !alwaysPrompt, - ProvisionerTags: tags, - }) + job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{Name: versionName, Client: client, Organization: organization, Provisioner: database.ProvisionerType(provisioner), FileID: resp.ID, VariablesFile: variablesFile, Variables: variables, Template: &template, ReuseParameters: !alwaysPrompt, ProvisionerTags: tags}) if err != nil { return err } - if job.Job.Status != codersdk.ProvisionerJobSucceeded { return xerrors.Errorf("job failed: %s", job.Job.Status) } - if activate { - err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ - ID: job.ID, - }) + err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ID: job.ID}) if err != nil { return err } } - _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) return nil }, + HelpHandler: func(i *clibase.Invocation) error { + }, } cmd.Options = clibase.OptionSet{ @@ -235,6 +245,12 @@ func (r *RootCmd) templatePush() *clibase.Cmd { Default: "true", Value: clibase.BoolOf(&activate), }, + { + Flag: "create", + Description: "Create a new template if one does not already exist.", + Default: "false", + Value: clibase.BoolOf(&create), + }, cliui.SkipPromptOption(), uploadFlags.option(), } diff --git a/cli/templatepush_test.go b/cli/templatepush_test.go index 82b0fd51980d1..0d7671fc98bf2 100644 --- a/cli/templatepush_test.go +++ b/cli/templatepush_test.go @@ -111,6 +111,38 @@ func TestTemplatePush(t *testing.T) { require.NotEqual(t, "example", templateVersions[0].Name) }) + t.Run("PushTemplateWithCreate", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) + user := coderdtest.CreateFirstUser(t, client) + version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) + _ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) + + source := clitest.CreateTemplateVersionSource(t, &echo.Responses{ + Parse: echo.ParseComplete, + ProvisionApply: echo.ProvisionComplete, + }) + + inv, root := clitest.New(t, "templates", "push", "--create", "--directory", source, "--name", "example") + clitest.SetupConfig(t, client, root) + pty := ptytest.New(t).Attach(inv) + + clitest.Start(t, inv) + + matches := []struct { + match string + write string + }{ + {match: "Upload", write: "yes"}, + } + for _, m := range matches { + pty.ExpectMatch(m.match) + if len(m.write) > 0 { + pty.WriteLine(m.write) + } + } + }) + t.Run("UseWorkingDir", func(t *testing.T) { t.Parallel() From 76fe0015658b7cc0ed449f23301e21471b2f20e1 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Fri, 16 Jun 2023 16:40:44 +0300 Subject: [PATCH 09/23] Discard changes to .github/workflows/ci.yaml --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6ad6f4e27d82b..b8e73ca7e1e76 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -153,7 +153,7 @@ jobs: - name: Install sqlc run: | - curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.18.0/sqlc_1.18.0_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc + curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.17.2/sqlc_1.17.2_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc - name: go install tools run: | From eea488ed9bc5e0aa7b8763c44e6c3e508f45f447 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Fri, 16 Jun 2023 16:40:49 +0300 Subject: [PATCH 10/23] Discard changes to .github/workflows/security.yaml --- .github/workflows/security.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/security.yaml b/.github/workflows/security.yaml index 256daa6e3ae7b..90bbcc78ca32f 100644 --- a/.github/workflows/security.yaml +++ b/.github/workflows/security.yaml @@ -75,7 +75,7 @@ jobs: - name: Install sqlc run: | - curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.18.0/sqlc_1.18.0_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc + curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.17.2/sqlc_1.17.2_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc - name: Install yq run: go run github.com/mikefarah/yq/v4@v4.30.6 - name: Install mockgen From 229af8016f2d891520a1c5531316abe2782dfc75 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Fri, 16 Jun 2023 16:40:56 +0300 Subject: [PATCH 11/23] Discard changes to dogfood/Dockerfile --- dogfood/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dogfood/Dockerfile b/dogfood/Dockerfile index 4bb0e45088fbc..cf9b3a017b7e6 100644 --- a/dogfood/Dockerfile +++ b/dogfood/Dockerfile @@ -53,7 +53,7 @@ RUN mkdir --parents "$GOPATH" && \ # charts and values files go install github.com/norwoodj/helm-docs/cmd/helm-docs@v1.5.0 && \ # sqlc for Go code generation - go install github.com/kyleconroy/sqlc/cmd/sqlc@v1.18.0 && \ + go install github.com/kyleconroy/sqlc/cmd/sqlc@v1.17.2 && \ # gcr-cleaner-cli used by CI to prune unused images go install github.com/sethvargo/gcr-cleaner/cmd/gcr-cleaner-cli@v0.5.1 && \ # ruleguard for checking custom rules, without needing to run all of From 72291f5fa8b9b626d6f11d9466acfb6653fc4f35 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 14:10:24 +0000 Subject: [PATCH 12/23] make lint --- cli/templatepush.go | 44 +++++++++++++++++++++++++------------- docs/cli/templates_push.md | 9 ++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 8729fae8f2b04..910ae94d302c3 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -121,27 +121,25 @@ func (r *RootCmd) templatePush() *clibase.Cmd { ) client := new(codersdk.Client) cmd := &clibase.Cmd{ - Parent: &clibase.Cmd{}, - Children: []*clibase.Cmd{}, - Use: "push [template]", - Aliases: []string{}, - Short: "Push a new template version from the current directory or as specified by flag", - Hidden: false, - RawArgs: false, - Long: "", - Options: []clibase.Option{}, - Annotations: map[string]string{}, - Middleware: clibase.Chain(clibase.RequireRangeArgs(0, 1), r.InitClient(client)), + Use: "push [template]", + Short: "Push a new template version from the current directory or as specified by flag", + Middleware: clibase.Chain( + clibase.RequireRangeArgs(0, 1), + r.InitClient(client), + ), Handler: func(inv *clibase.Invocation) error { uploadFlags.setWorkdir(workdir) + organization, err := CurrentOrganization(inv, client) if err != nil { return err } + name, err := uploadFlags.templateName(inv.Args) if err != nil { return err } + template, err := client.TemplateByName(inv.Context(), organization.ID, name) if err != nil { if create { @@ -173,28 +171,44 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if err != nil { return err } + tags, err := ParseProvisionerTags(provisionerTags) if err != nil { return err } - job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{Name: versionName, Client: client, Organization: organization, Provisioner: database.ProvisionerType(provisioner), FileID: resp.ID, VariablesFile: variablesFile, Variables: variables, Template: &template, ReuseParameters: !alwaysPrompt, ProvisionerTags: tags}) + + job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ + Name: versionName, + Client: client, + Organization: organization, + Provisioner: database.ProvisionerType(provisioner), + FileID: resp.ID, + VariablesFile: variablesFile, + Variables: variables, + Template: &template, + ReuseParameters: !alwaysPrompt, + ProvisionerTags: tags, + }) if err != nil { return err } + if job.Job.Status != codersdk.ProvisionerJobSucceeded { return xerrors.Errorf("job failed: %s", job.Job.Status) } + if activate { - err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ID: job.ID}) + err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ + ID: job.ID, + }) if err != nil { return err } } + _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) return nil }, - HelpHandler: func(i *clibase.Invocation) error { - }, } cmd.Options = clibase.OptionSet{ diff --git a/docs/cli/templates_push.md b/docs/cli/templates_push.md index b3762a641eb56..51d7231afc22e 100644 --- a/docs/cli/templates_push.md +++ b/docs/cli/templates_push.md @@ -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 | bool | +| Default | false | + +Create a new template if one does not already exist. + ### -d, --directory | | | From 910bfbb9fd64273a3b08671d9f209ceb5274d071 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Fri, 16 Jun 2023 14:26:45 +0000 Subject: [PATCH 13/23] make update-golden-files --- cli/testdata/coder_templates_push_--help.golden | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/testdata/coder_templates_push_--help.golden b/cli/testdata/coder_templates_push_--help.golden index c544ecc341f60..d9f6fe343e602 100644 --- a/cli/testdata/coder_templates_push_--help.golden +++ b/cli/testdata/coder_templates_push_--help.golden @@ -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 a new template if one does not already exist. + -d, --directory string (default: .) Specify the directory to create from, use '-' to read tar from stdin. From c99baaf3d832aeb99d37cd103004d779a1443209 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Mon, 19 Jun 2023 10:51:44 +0000 Subject: [PATCH 14/23] make gen --- coderd/database/models.go | 2 +- coderd/database/querier.go | 2 +- coderd/database/queries.sql.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/database/models.go b/coderd/database/models.go index c6bb681d5c00f..9f300614731e2 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.17.2 +// sqlc v1.18.0 package database diff --git a/coderd/database/querier.go b/coderd/database/querier.go index f07b5e5b8d59a..5ecf610ae8c43 100644 --- a/coderd/database/querier.go +++ b/coderd/database/querier.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.17.2 +// sqlc v1.18.0 package database diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index bab3b845b24d9..bf807176df367 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.17.2 +// sqlc v1.18.0 package database From f4e6eabf50277d9292fba93ec8339aa0e5755d17 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Mon, 19 Jun 2023 14:14:18 +0000 Subject: [PATCH 15/23] refactor --- cli/templatepush.go | 47 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 910ae94d302c3..df0b387261bb5 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -140,18 +140,34 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return err } + resp, err := uploadFlags.upload(inv, client) + if err != nil { + return err + } + + tags, err := ParseProvisionerTags(provisionerTags) + if err != nil { + return err + } + template, err := client.TemplateByName(inv.Context(), organization.ID, name) if err != nil { - if create { - resp, err := uploadFlags.upload(inv, client) - if err != nil { - return err - } - tags, err := ParseProvisionerTags(provisionerTags) - if err != nil { - return err - } - job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{Client: client, Organization: organization, Provisioner: database.ProvisionerType(provisioner), FileID: resp.ID, ProvisionerTags: tags, VariablesFile: variablesFile, Variables: variables}) + if !create { + _, _ = fmt.Fprintf(inv.Stdout, "Template %s not found, create a new template with the --create flag\n or use `coder template create` to create a new template\n", name) + return xerrors.Errorf("template %q not found: %w", name, err) + } else { + job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ + Name: versionName, + Client: client, + Organization: organization, + Provisioner: database.ProvisionerType(provisioner), + FileID: resp.ID, + VariablesFile: variablesFile, + Variables: variables, + Template: &template, + ReuseParameters: !alwaysPrompt, + ProvisionerTags: tags, + }) if err != nil { return err } @@ -164,17 +180,8 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if err != nil { return err } + _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) } - return err - } - resp, err := uploadFlags.upload(inv, client) - if err != nil { - return err - } - - tags, err := ParseProvisionerTags(provisionerTags) - if err != nil { - return err } job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ From d7272b9985cea08883cdfbaef9bfdf873b374e17 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Mon, 19 Jun 2023 14:25:15 +0000 Subject: [PATCH 16/23] more refactor --- cli/templatepush.go | 57 ++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index df0b387261bb5..03ba8a2e2895c 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -155,33 +155,33 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if !create { _, _ = fmt.Fprintf(inv.Stdout, "Template %s not found, create a new template with the --create flag\n or use `coder template create` to create a new template\n", name) return xerrors.Errorf("template %q not found: %w", name, err) - } else { - job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ - Name: versionName, - Client: client, - Organization: organization, - Provisioner: database.ProvisionerType(provisioner), - FileID: resp.ID, - VariablesFile: variablesFile, - Variables: variables, - Template: &template, - ReuseParameters: !alwaysPrompt, - ProvisionerTags: tags, - }) - if err != nil { - return err - } - defaultTTL := 24 * time.Hour - failureTTL := 0 * time.Hour - inactivityTTL := 0 * time.Hour - disableEveryone := false - createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} - template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) - if err != nil { - return err - } - _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) } + + job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ + Name: versionName, + Client: client, + Organization: organization, + Provisioner: database.ProvisionerType(provisioner), + FileID: resp.ID, + VariablesFile: variablesFile, + Variables: variables, + Template: &template, + ReuseParameters: !alwaysPrompt, + ProvisionerTags: tags, + }) + if err != nil { + return err + } + defaultTTL := 24 * time.Hour + failureTTL := 0 * time.Hour + inactivityTTL := 0 * time.Hour + disableEveryone := false + createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} + template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) + if err != nil { + return err + } + return nil } job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ @@ -212,7 +212,10 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return err } } - + if create { + _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) + return nil + } _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) return nil }, From 379f67072adeb4e0c5d0c9f1f512aabd047f519f Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Mon, 19 Jun 2023 15:16:42 +0000 Subject: [PATCH 17/23] more refactor --- cli/templatepush.go | 51 ++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 03ba8a2e2895c..5cb4612eb8c14 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -151,37 +151,9 @@ func (r *RootCmd) templatePush() *clibase.Cmd { } template, err := client.TemplateByName(inv.Context(), organization.ID, name) - if err != nil { - if !create { - _, _ = fmt.Fprintf(inv.Stdout, "Template %s not found, create a new template with the --create flag\n or use `coder template create` to create a new template\n", name) - return xerrors.Errorf("template %q not found: %w", name, err) - } - - job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ - Name: versionName, - Client: client, - Organization: organization, - Provisioner: database.ProvisionerType(provisioner), - FileID: resp.ID, - VariablesFile: variablesFile, - Variables: variables, - Template: &template, - ReuseParameters: !alwaysPrompt, - ProvisionerTags: tags, - }) - if err != nil { - return err - } - defaultTTL := 24 * time.Hour - failureTTL := 0 * time.Hour - inactivityTTL := 0 * time.Hour - disableEveryone := false - createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} - template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) - if err != nil { - return err - } - return nil + if err != nil && !create { + _, _ = fmt.Fprintf(inv.Stdout, "Template %s not found, create a new template with the --create flag\n or use `coder template create` to create a new template\n", name) + return xerrors.Errorf("template %q not found: %w", name, err) } job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ @@ -199,6 +171,19 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if err != nil { return err } + if create { + defaultTTL := 24 * time.Hour + failureTTL := 0 * time.Hour + inactivityTTL := 0 * time.Hour + disableEveryone := false + createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} + template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) + if err != nil { + return err + } + _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) + return nil + } if job.Job.Status != codersdk.ProvisionerJobSucceeded { return xerrors.Errorf("job failed: %s", job.Job.Status) @@ -212,10 +197,6 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return err } } - if create { - _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) - return nil - } _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) return nil }, From 348535246243876ebe56a05e23d079fe1f0f66a8 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Mon, 19 Jun 2023 15:19:36 +0000 Subject: [PATCH 18/23] better cli message --- cli/templatepush.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 5cb4612eb8c14..227fe386459ee 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -171,6 +171,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if err != nil { return err } + if create { defaultTTL := 24 * time.Hour failureTTL := 0 * time.Hour @@ -181,8 +182,6 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if err != nil { return err } - _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) - return nil } if job.Job.Status != codersdk.ProvisionerJobSucceeded { @@ -197,6 +196,11 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return err } } + + if create { + _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) + return nil + } _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) return nil }, From 8b3137211a2630985f3a5ad4514163831b8f3233 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Mon, 19 Jun 2023 15:24:28 +0000 Subject: [PATCH 19/23] refactor --- cli/templatepush.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 227fe386459ee..5641a2342c742 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -182,6 +182,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd { if err != nil { return err } + _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) } if job.Job.Status != codersdk.ProvisionerJobSucceeded { @@ -196,11 +197,6 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return err } } - - if create { - _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) - return nil - } _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) return nil }, From c3d87e929961a3cc2bbd596c75a0661b23d013ee Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Wed, 21 Jun 2023 07:32:01 +0000 Subject: [PATCH 20/23] refactor --- cli/templatepush.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 5641a2342c742..d3ca39efab76e 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -151,9 +151,22 @@ func (r *RootCmd) templatePush() *clibase.Cmd { } template, err := client.TemplateByName(inv.Context(), organization.ID, name) - if err != nil && !create { - _, _ = fmt.Fprintf(inv.Stdout, "Template %s not found, create a new template with the --create flag\n or use `coder template create` to create a new template\n", name) - return xerrors.Errorf("template %q not found: %w", name, err) + if err != nil { + if !create { + _, _ = fmt.Fprintf(inv.Stdout, "Create a new template with `coder templates create %s`.\n", name) + _, _ = fmt.Fprintf(inv.Stdout, "Or use `coder templates push %s --create`.\n", name) + return xerrors.Errorf("template %q not found: %w", name, err) + } + _, _ = fmt.Fprintf(inv.Stdout, "Creating a new template: %s\n", name) + defaultTTL := 24 * time.Hour + failureTTL := 0 * time.Hour + inactivityTTL := 0 * time.Hour + disableEveryone := false + createReq := codersdk.CreateTemplateRequest{Name: name, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} + template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) + if err != nil { + return err + } } job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ @@ -172,19 +185,6 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return err } - if create { - defaultTTL := 24 * time.Hour - failureTTL := 0 * time.Hour - inactivityTTL := 0 * time.Hour - disableEveryone := false - createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} - template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) - if err != nil { - return err - } - _, _ = fmt.Fprintf(inv.Stdout, "Created template %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) - } - if job.Job.Status != codersdk.ProvisionerJobSucceeded { return xerrors.Errorf("job failed: %s", job.Job.Status) } From c2b5c68430b6513d7a01edbe74bc07ad98f653f8 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Thu, 29 Jun 2023 17:36:08 +0300 Subject: [PATCH 21/23] Apply suggestions from code review Co-authored-by: Cian Johnston --- cli/templatepush_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cli/templatepush_test.go b/cli/templatepush_test.go index 03c7a87ef3389..636a7c7cfce7a 100644 --- a/cli/templatepush_test.go +++ b/cli/templatepush_test.go @@ -194,10 +194,10 @@ func TestTemplatePush(t *testing.T) { t.Run("PushTemplateWithCreate", func(t *testing.T) { t.Parallel() + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium) + t.Cleanup(cancel) client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) user := coderdtest.CreateFirstUser(t, client) - version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) - _ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) source := clitest.CreateTemplateVersionSource(t, &echo.Responses{ Parse: echo.ParseComplete, @@ -215,13 +215,19 @@ func TestTemplatePush(t *testing.T) { write string }{ {match: "Upload", write: "yes"}, + {match: "Creating a new template"}, + } for _, m := range matches { pty.ExpectMatch(m.match) if len(m.write) > 0 { pty.WriteLine(m.write) } - } + // Assert that the template was created. + template, err := client.TemplateByName(ctx, user.OrganizationID, "example") + require.NoError(t, err) + require.NotNil(t, template.ID) + } }) t.Run("UseWorkingDir", func(t *testing.T) { From 143e84d7a568d6b4d68ad060f9e1221961171a9d Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Thu, 29 Jun 2023 14:52:37 +0000 Subject: [PATCH 22/23] make fmt and fix --- cli/templatepush.go | 6 +++++- cli/templatepush_test.go | 11 +++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index d684280e1389c..2fecf0ff5643f 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "net/http" "path/filepath" "time" @@ -178,6 +179,9 @@ func (r *RootCmd) templatePush() *clibase.Cmd { template, err := client.TemplateByName(inv.Context(), organization.ID, name) if err != nil { + if sdkErr, ok := codersdk.AsError(err); ok && sdkErr.StatusCode() != http.StatusNotFound { + return xerrors.Errorf("get template: %w", err) + } if !create { _, _ = fmt.Fprintf(inv.Stdout, "Create a new template with `coder templates create %s`.\n", name) _, _ = fmt.Fprintf(inv.Stdout, "Or use `coder templates push %s --create`.\n", name) @@ -188,7 +192,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd { failureTTL := 0 * time.Hour inactivityTTL := 0 * time.Hour disableEveryone := false - createReq := codersdk.CreateTemplateRequest{Name: name, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} + createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone} template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq) if err != nil { return err diff --git a/cli/templatepush_test.go b/cli/templatepush_test.go index 636a7c7cfce7a..75d7cd1007cd4 100644 --- a/cli/templatepush_test.go +++ b/cli/templatepush_test.go @@ -216,18 +216,17 @@ func TestTemplatePush(t *testing.T) { }{ {match: "Upload", write: "yes"}, {match: "Creating a new template"}, - } for _, m := range matches { pty.ExpectMatch(m.match) if len(m.write) > 0 { pty.WriteLine(m.write) } - // Assert that the template was created. - template, err := client.TemplateByName(ctx, user.OrganizationID, "example") - require.NoError(t, err) - require.NotNil(t, template.ID) - } + // Assert that the template was created. + template, err := client.TemplateByName(ctx, user.OrganizationID, "example") + require.NoError(t, err) + require.NotNil(t, template.ID) + } }) t.Run("UseWorkingDir", func(t *testing.T) { From 2fc0729f5193bd76b95be26182aa5e84bfda2d79 Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Thu, 29 Jun 2023 15:14:54 +0000 Subject: [PATCH 23/23] create version before template creation --- cli/templatepush.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 2fecf0ff5643f..ef511f595898f 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -179,15 +179,27 @@ func (r *RootCmd) templatePush() *clibase.Cmd { template, err := client.TemplateByName(inv.Context(), organization.ID, name) if err != nil { - if sdkErr, ok := codersdk.AsError(err); ok && sdkErr.StatusCode() != http.StatusNotFound { - return xerrors.Errorf("get template: %w", err) - } if !create { + if sdkErr, ok := codersdk.AsError(err); ok && sdkErr.StatusCode() != http.StatusNotFound { + return xerrors.Errorf("get template: %w", err) + } _, _ = fmt.Fprintf(inv.Stdout, "Create a new template with `coder templates create %s`.\n", name) _, _ = fmt.Fprintf(inv.Stdout, "Or use `coder templates push %s --create`.\n", name) return xerrors.Errorf("template %q not found: %w", name, err) } _, _ = fmt.Fprintf(inv.Stdout, "Creating a new template: %s\n", name) + job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{ + Client: client, + Organization: organization, + Provisioner: database.ProvisionerType(provisioner), + FileID: resp.ID, + ProvisionerTags: tags, + VariablesFile: variablesFile, + Variables: variables, + }) + if err != nil { + return err + } defaultTTL := 24 * time.Hour failureTTL := 0 * time.Hour inactivityTTL := 0 * time.Hour