Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0cc46c8
fix: make template push a superset of template create
f0ssel Dec 20, 2023
93da7d0
add back work dir
f0ssel Dec 20, 2023
3c409f6
add groupacl
f0ssel Dec 20, 2023
4b763d7
Add back workdir
f0ssel Dec 20, 2023
d178cc7
combine edit flags
f0ssel Dec 20, 2023
5474942
fix edit
f0ssel Dec 20, 2023
2b9bcf6
unify edit and push
f0ssel Dec 20, 2023
44f264b
make gen
f0ssel Dec 20, 2023
d58cf18
fix test
f0ssel Dec 21, 2023
9b32922
make gen
f0ssel Dec 21, 2023
c6ef6f4
update golden
f0ssel Dec 21, 2023
e1e1653
fix tests:
f0ssel Dec 21, 2023
0cdefbc
fix merge
f0ssel Dec 21, 2023
d2a7866
add test
f0ssel Dec 22, 2023
63d57fd
remove test
f0ssel Dec 22, 2023
ca65869
add unset test
f0ssel Dec 22, 2023
9866235
Add deprecation warning
f0ssel Dec 22, 2023
35c7adf
fix
f0ssel Dec 22, 2023
4abf179
rename functions
f0ssel Jan 2, 2024
a3fdb76
test removing flags
f0ssel Jan 3, 2024
7599090
revert
f0ssel Jan 3, 2024
e958e1a
cleanup
f0ssel Jan 3, 2024
c8cae6c
add back flag
f0ssel Jan 3, 2024
1b03e89
remove create command
f0ssel Jan 4, 2024
8918cda
make gen
f0ssel Jan 4, 2024
7d2a7ac
fix gen
f0ssel Jan 4, 2024
5b2792d
add private flag to template edit
f0ssel Jan 4, 2024
b85a73c
fix test
f0ssel Jan 4, 2024
7655bc9
fix golden
f0ssel Jan 4, 2024
2f2911a
update mentions of templates create command
f0ssel Jan 4, 2024
5334e8e
update golden
f0ssel Jan 4, 2024
79c0c02
add removal comment
f0ssel Jan 4, 2024
2dfb98e
Add back create
f0ssel Jan 4, 2024
cb0aec4
fix formatting
f0ssel Jan 4, 2024
d1f13d1
add disableeveryonegroupaccess test
f0ssel Jan 4, 2024
5792279
update test
f0ssel Jan 5, 2024
2e54259
fix test lint
f0ssel Jan 5, 2024
de78f4b
pr comments
f0ssel Jan 5, 2024
0c859bc
fix text formatting
f0ssel Jan 5, 2024
b65ab81
Add deprecation to help text
f0ssel Jan 5, 2024
77c9edf
fix template push wording
f0ssel Jan 5, 2024
13794fd
golden
f0ssel Jan 5, 2024
8355850
fix gen again
f0ssel Jan 5, 2024
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
remove create command
  • Loading branch information
f0ssel committed Jan 5, 2024
commit 1b03e8934e5cc57250fb84ecc12296acb6e3ef04
235 changes: 9 additions & 226 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
@@ -1,245 +1,28 @@
package cli

import (
"errors"
"fmt"
"io"
"strings"
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/pretty"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/pretty"
)

func (r *RootCmd) templateCreate() *clibase.Cmd {
var (
provisioner string
provisionerTags []string
variablesFile string
commandLineVariables []string
versionName string

uploadFlags templateUploadFlags
)
client := new(codersdk.Client)
func (*RootCmd) templateCreate() *clibase.Cmd {
cmd := &clibase.Cmd{
Use: "create [name]",
Short: "Create a template from the current directory or as specified by flag",
Middleware: clibase.Chain(
clibase.RequireRangeArgs(0, 1),
r.InitClient(client),
),
Use: "create [name]",
Short: "Create a template from the current directory or as specified by flag",
Hidden: true,
Handler: func(inv *clibase.Invocation) error {
_, _ = fmt.Fprintln(inv.Stdout, "\n"+pretty.Sprint(cliui.DefaultStyles.Wrap,
pretty.Sprint(
cliui.DefaultStyles.Warn, "DEPRECATION WARNING: The `coder templates push` command should be used instead. This command will be removed in a future release. ")+"\n"))
time.Sleep(1 * time.Second)

organization, err := CurrentOrganization(inv, client)
if err != nil {
return err
}

name, err := uploadFlags.templateName(inv.Args)
if err != nil {
return err
}

job, _, err := createTemplateVersion(createTemplateVersionArgs{
inv: inv,
client: client,
name: name,
org: organization,
uploadFlags: uploadFlags,
provisionerTags: provisionerTags,
provisioner: provisioner,
variablesFile: variablesFile,
commandLineVariables: commandLineVariables,
versionName: versionName,
alwaysPrompt: false,
})
if err != nil {
return err
}

if !uploadFlags.stdin() {
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: "Confirm create?",
IsConfirm: true,
})
if err != nil {
return err
}
}

createReq := codersdk.CreateTemplateRequest{
Name: name,
VersionID: job.ID,
}

_, err = client.CreateTemplate(inv.Context(), organization.ID, createReq)
if err != nil {
return err
}

_, _ = fmt.Fprintln(inv.Stdout, "\n"+pretty.Sprint(cliui.DefaultStyles.Wrap,
"The "+pretty.Sprint(
cliui.DefaultStyles.Keyword, name)+" template has been created at "+
pretty.Sprint(cliui.DefaultStyles.DateTimeStamp, time.Now().Format(time.Stamp))+"! "+
"Developers can provision a workspace with this template using:")+"\n")

_, _ = fmt.Fprintln(inv.Stdout, " "+pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("coder create --template=%q [workspace name]", name)))
_, _ = fmt.Fprintln(inv.Stdout)

cliui.DefaultStyles.Error,
"ERROR: The `coder templates create` command has been removed."+
"Use the `coder templates push` command to create and update templates. "+
"Use the `coder templates edit` command to change template settings.")))
return nil
},
}
cmd.Options = clibase.OptionSet{
{
Flag: "variables-file",
Description: "Specify a file path with values for Terraform-managed variables.",
Value: clibase.StringOf(&variablesFile),
},
{
Flag: "variable",
Description: "Specify a set of values for Terraform-managed variables.",
Value: clibase.StringArrayOf(&commandLineVariables),
},
{
Flag: "var",
Description: "Alias of --variable.",
Value: clibase.StringArrayOf(&commandLineVariables),
},
{
Flag: "provisioner-tag",
Description: "Specify a set of tags to target provisioner daemons.",
Value: clibase.StringArrayOf(&provisionerTags),
},
{
Flag: "test.provisioner",
Description: "Customize the provisioner backend.",
Default: "terraform",
Value: clibase.StringOf(&provisioner),
Hidden: true,
},
{
Flag: "name",
Description: "Specify a name for the new template version. It will be automatically generated if not provided.",
Value: clibase.StringOf(&versionName),
},

cliui.SkipPromptOption(),
}
cmd.Options = append(cmd.Options, uploadFlags.options()...)
return cmd
}

type createValidTemplateVersionArgs struct {
Name string
Message string
Client *codersdk.Client
Organization codersdk.Organization
Provisioner codersdk.ProvisionerType
FileID uuid.UUID

// Template is only required if updating a template's active version.
Template *codersdk.Template
// ReuseParameters will attempt to reuse params from the Template field
// before prompting the user. Set to false to always prompt for param
// values.
ReuseParameters bool
ProvisionerTags map[string]string
UserVariableValues []codersdk.VariableValue
}

func createValidTemplateVersion(inv *clibase.Invocation, args createValidTemplateVersionArgs) (*codersdk.TemplateVersion, error) {
client := args.Client

req := codersdk.CreateTemplateVersionRequest{
Name: args.Name,
Message: args.Message,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
FileID: args.FileID,
Provisioner: args.Provisioner,
ProvisionerTags: args.ProvisionerTags,
UserVariableValues: args.UserVariableValues,
}
if args.Template != nil {
req.TemplateID = args.Template.ID
}
version, err := client.CreateTemplateVersion(inv.Context(), args.Organization.ID, req)
if err != nil {
return nil, err
}

err = cliui.ProvisionerJob(inv.Context(), inv.Stdout, cliui.ProvisionerJobOptions{
Fetch: func() (codersdk.ProvisionerJob, error) {
version, err := client.TemplateVersion(inv.Context(), version.ID)
return version.Job, err
},
Cancel: func() error {
return client.CancelTemplateVersion(inv.Context(), version.ID)
},
Logs: func() (<-chan codersdk.ProvisionerJobLog, io.Closer, error) {
return client.TemplateVersionLogsAfter(inv.Context(), version.ID, 0)
},
})
if err != nil {
var jobErr *cliui.ProvisionerJobError
if errors.As(err, &jobErr) && !codersdk.JobIsMissingParameterErrorCode(jobErr.Code) {
return nil, err
}
if err != nil {
return nil, err
}
}
version, err = client.TemplateVersion(inv.Context(), version.ID)
if err != nil {
return nil, err
}

if version.Job.Status != codersdk.ProvisionerJobSucceeded {
return nil, xerrors.New(version.Job.Error)
}

resources, err := client.TemplateVersionResources(inv.Context(), version.ID)
if err != nil {
return nil, err
}

// Only display the resources on the start transition, to avoid listing them more than once.
var startResources []codersdk.WorkspaceResource
for _, r := range resources {
if r.Transition == codersdk.WorkspaceTransitionStart {
startResources = append(startResources, r)
}
}
err = cliui.WorkspaceResources(inv.Stdout, startResources, cliui.WorkspaceResourcesOptions{
HideAgentState: true,
HideAccess: true,
Title: "Template Preview",
})
if err != nil {
return nil, xerrors.Errorf("preview template resources: %w", err)
}

return &version, nil
}

func ParseProvisionerTags(rawTags []string) (map[string]string, error) {
tags := map[string]string{}
for _, rawTag := range rawTags {
parts := strings.SplitN(rawTag, "=", 2)
if len(parts) < 2 {
return nil, xerrors.Errorf("invalid tag format for %q. must be key=value", rawTag)
}
tags[parts[0]] = parts[1]
}
return tags, nil
}
Loading