@@ -29,7 +29,9 @@ func (r *RootCmd) create() *serpent.Command {
29
29
parameterFlags workspaceParameterFlags
30
30
autoUpdates string
31
31
copyParametersFrom string
32
- orgContext = NewOrganizationContext ()
32
+ // Organization context is only required if more than 1 template
33
+ // shares the same name across multiple organizations.
34
+ orgContext = NewOrganizationContext ()
33
35
)
34
36
client := new (codersdk.Client )
35
37
cmd := & serpent.Command {
@@ -44,11 +46,7 @@ func (r *RootCmd) create() *serpent.Command {
44
46
),
45
47
Middleware : serpent .Chain (r .InitClient (client )),
46
48
Handler : func (inv * serpent.Invocation ) error {
47
- organization , err := orgContext .Selected (inv , client )
48
- if err != nil {
49
- return err
50
- }
51
-
49
+ var err error
52
50
workspaceOwner := codersdk .Me
53
51
if len (inv .Args ) >= 1 {
54
52
workspaceOwner , workspaceName , err = splitNamedWorkspace (inv .Args [0 ])
@@ -99,7 +97,7 @@ func (r *RootCmd) create() *serpent.Command {
99
97
if templateName == "" {
100
98
_ , _ = fmt .Fprintln (inv .Stdout , pretty .Sprint (cliui .DefaultStyles .Wrap , "Select a template below to preview the provisioned infrastructure:" ))
101
99
102
- templates , err := client .TemplatesByOrganization (inv .Context (), organization . ID )
100
+ templates , err := client .Templates (inv .Context (), codersdk. TemplateFilter {} )
103
101
if err != nil {
104
102
return err
105
103
}
@@ -145,10 +143,34 @@ func (r *RootCmd) create() *serpent.Command {
145
143
}
146
144
templateVersionID = sourceWorkspace .LatestBuild .TemplateVersionID
147
145
} else {
148
- template , err = client .TemplateByName (inv .Context (), organization .ID , templateName )
146
+ templates , err := client .Templates (inv .Context (), codersdk.TemplateFilter {
147
+ ExactName : templateName ,
148
+ })
149
149
if err != nil {
150
150
return xerrors .Errorf ("get template by name: %w" , err )
151
151
}
152
+ if len (templates ) == 0 {
153
+ return xerrors .Errorf ("no template found with the name %q" , templateName )
154
+ }
155
+
156
+ if len (templates ) > 1 {
157
+ selectedOrg , err := orgContext .Selected (inv , client )
158
+ if err != nil {
159
+ return xerrors .Errorf ("multiple templates found with the name %q, use `--org=<organization_name>` to specify which template by that name to use" , templateName )
160
+ }
161
+
162
+ index := slices .IndexFunc (templates , func (i codersdk.Template ) bool {
163
+ return i .OrganizationID == selectedOrg .ID
164
+ })
165
+ if index == - 1 {
166
+ return xerrors .Errorf ("no templates found with the name %q in the organization %q" , templateName , selectedOrg .Name )
167
+ }
168
+
169
+ // remake the list with the only template selected
170
+ templates = []codersdk.Template {templates [index ]}
171
+ }
172
+
173
+ template = templates [0 ]
152
174
templateVersionID = template .ActiveVersionID
153
175
}
154
176
@@ -207,7 +229,7 @@ func (r *RootCmd) create() *serpent.Command {
207
229
ttlMillis = ptr .Ref (stopAfter .Milliseconds ())
208
230
}
209
231
210
- workspace , err := client .CreateWorkspace (inv .Context (), organization . ID , workspaceOwner , codersdk.CreateWorkspaceRequest {
232
+ workspace , err := client .CreateWorkspace (inv .Context (), template . OrganizationID , workspaceOwner , codersdk.CreateWorkspaceRequest {
211
233
TemplateVersionID : templateVersionID ,
212
234
Name : workspaceName ,
213
235
AutostartSchedule : schedSpec ,
0 commit comments