@@ -153,6 +153,7 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
153
153
return err
154
154
}
155
155
156
+ createNewRole := false
156
157
var customRole codersdk.Role
157
158
if jsonInput {
158
159
// JSON Upload mode
@@ -179,12 +180,13 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
179
180
return xerrors .Errorf ("missing role name argument, usage:\" coder organizations roles edit <role_name>\" " )
180
181
}
181
182
182
- interactiveRole ,err := interactiveOrgRoleEdit (inv ,org .ID ,client )
183
+ interactiveRole ,newRole , err := interactiveOrgRoleEdit (inv ,org .ID ,client )
183
184
if err != nil {
184
185
return xerrors .Errorf ("editing role: %w" ,err )
185
186
}
186
187
187
188
customRole = * interactiveRole
189
+ createNewRole = newRole
188
190
189
191
preview := fmt .Sprintf ("permissions: %d site, %d org, %d user" ,
190
192
len (customRole .SitePermissions ),len (customRole .OrganizationPermissions ),len (customRole .UserPermissions ))
@@ -203,7 +205,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
203
205
// Do not actually post
204
206
updated = customRole
205
207
}else {
206
- updated ,err = client .PatchOrganizationRole (ctx ,customRole )
208
+ switch createNewRole {
209
+ case true :
210
+ updated ,err = client .CreateOrganizationRole (ctx ,customRole )
211
+ default :
212
+ updated ,err = client .UpdateOrganizationRole (ctx ,customRole )
213
+ }
207
214
if err != nil {
208
215
return xerrors .Errorf ("patch role: %w" ,err )
209
216
}
@@ -223,11 +230,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
223
230
return cmd
224
231
}
225
232
226
- func interactiveOrgRoleEdit (inv * serpent.Invocation ,orgID uuid.UUID ,client * codersdk.Client ) (* codersdk.Role ,error ) {
233
+ func interactiveOrgRoleEdit (inv * serpent.Invocation ,orgID uuid.UUID ,client * codersdk.Client ) (* codersdk.Role ,bool ,error ) {
234
+ newRole := false
227
235
ctx := inv .Context ()
228
236
roles ,err := client .ListOrganizationRoles (ctx ,orgID )
229
237
if err != nil {
230
- return nil ,xerrors .Errorf ("listing roles: %w" ,err )
238
+ return nil ,newRole , xerrors .Errorf ("listing roles: %w" ,err )
231
239
}
232
240
233
241
// Make sure the role actually exists first
@@ -246,22 +254,23 @@ func interactiveOrgRoleEdit(inv *serpent.Invocation, orgID uuid.UUID, client *co
246
254
IsConfirm :true ,
247
255
})
248
256
if err != nil {
249
- return nil ,xerrors .Errorf ("abort: %w" ,err )
257
+ return nil ,newRole , xerrors .Errorf ("abort: %w" ,err )
250
258
}
251
259
252
260
originalRole .Role = codersdk.Role {
253
261
Name :inv .Args [0 ],
254
262
OrganizationID :orgID .String (),
255
263
}
264
+ newRole = true
256
265
}
257
266
258
267
// Some checks since interactive mode is limited in what it currently sees
259
268
if len (originalRole .SitePermissions )> 0 {
260
- return nil ,xerrors .Errorf ("unable to edit role in interactive mode, it contains site wide permissions" )
269
+ return nil ,newRole , xerrors .Errorf ("unable to edit role in interactive mode, it contains site wide permissions" )
261
270
}
262
271
263
272
if len (originalRole .UserPermissions )> 0 {
264
- return nil ,xerrors .Errorf ("unable to edit role in interactive mode, it contains user permissions" )
273
+ return nil ,newRole , xerrors .Errorf ("unable to edit role in interactive mode, it contains user permissions" )
265
274
}
266
275
267
276
role := & originalRole .Role
@@ -283,13 +292,13 @@ customRoleLoop:
283
292
Options :append (permissionPreviews (role ,allowedResources ),done ,abort ),
284
293
})
285
294
if err != nil {
286
- return role ,xerrors .Errorf ("selecting resource: %w" ,err )
295
+ return role ,newRole , xerrors .Errorf ("selecting resource: %w" ,err )
287
296
}
288
297
switch selected {
289
298
case done :
290
299
break customRoleLoop
291
300
case abort :
292
- return role ,xerrors .Errorf ("edit role %q aborted" ,role .Name )
301
+ return role ,newRole , xerrors .Errorf ("edit role %q aborted" ,role .Name )
293
302
default :
294
303
strs := strings .Split (selected ,"::" )
295
304
resource := strings .TrimSpace (strs [0 ])
@@ -300,7 +309,7 @@ customRoleLoop:
300
309
Defaults :defaultActions (role ,resource ),
301
310
})
302
311
if err != nil {
303
- return role ,xerrors .Errorf ("selecting actions for resource %q: %w" ,resource ,err )
312
+ return role ,newRole , xerrors .Errorf ("selecting actions for resource %q: %w" ,resource ,err )
304
313
}
305
314
applyOrgResourceActions (role ,resource ,actions )
306
315
// back to resources!
@@ -309,7 +318,7 @@ customRoleLoop:
309
318
// This println is required because the prompt ends us on the same line as some text.
310
319
_ ,_ = fmt .Println ()
311
320
312
- return role ,nil
321
+ return role ,newRole , nil
313
322
}
314
323
315
324
func applyOrgResourceActions (role * codersdk.Role ,resource string ,actions []string ) {