Skip to content

Commit dcc5023

Browse files
committed
fixup cli
1 parent 031da75 commit dcc5023

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

cli/organizationroles.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
153153
return err
154154
}
155155

156+
createNewRole := false
156157
var customRole codersdk.Role
157158
if jsonInput {
158159
// JSON Upload mode
@@ -179,12 +180,13 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
179180
return xerrors.Errorf("missing role name argument, usage: \"coder organizations roles edit <role_name>\"")
180181
}
181182

182-
interactiveRole, err := interactiveOrgRoleEdit(inv, org.ID, client)
183+
interactiveRole, newRole, err := interactiveOrgRoleEdit(inv, org.ID, client)
183184
if err != nil {
184185
return xerrors.Errorf("editing role: %w", err)
185186
}
186187

187188
customRole = *interactiveRole
189+
createNewRole = newRole
188190

189191
preview := fmt.Sprintf("permissions: %d site, %d org, %d user",
190192
len(customRole.SitePermissions), len(customRole.OrganizationPermissions), len(customRole.UserPermissions))
@@ -203,7 +205,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
203205
// Do not actually post
204206
updated = customRole
205207
} 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+
}
207214
if err != nil {
208215
return xerrors.Errorf("patch role: %w", err)
209216
}
@@ -223,11 +230,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
223230
return cmd
224231
}
225232

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
227235
ctx := inv.Context()
228236
roles, err := client.ListOrganizationRoles(ctx, orgID)
229237
if err != nil {
230-
return nil, xerrors.Errorf("listing roles: %w", err)
238+
return nil, newRole, xerrors.Errorf("listing roles: %w", err)
231239
}
232240

233241
// Make sure the role actually exists first
@@ -246,22 +254,23 @@ func interactiveOrgRoleEdit(inv *serpent.Invocation, orgID uuid.UUID, client *co
246254
IsConfirm: true,
247255
})
248256
if err != nil {
249-
return nil, xerrors.Errorf("abort: %w", err)
257+
return nil, newRole, xerrors.Errorf("abort: %w", err)
250258
}
251259

252260
originalRole.Role = codersdk.Role{
253261
Name: inv.Args[0],
254262
OrganizationID: orgID.String(),
255263
}
264+
newRole = true
256265
}
257266

258267
// Some checks since interactive mode is limited in what it currently sees
259268
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")
261270
}
262271

263272
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")
265274
}
266275

267276
role := &originalRole.Role
@@ -283,13 +292,13 @@ customRoleLoop:
283292
Options: append(permissionPreviews(role, allowedResources), done, abort),
284293
})
285294
if err != nil {
286-
return role, xerrors.Errorf("selecting resource: %w", err)
295+
return role, newRole, xerrors.Errorf("selecting resource: %w", err)
287296
}
288297
switch selected {
289298
case done:
290299
break customRoleLoop
291300
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)
293302
default:
294303
strs := strings.Split(selected, "::")
295304
resource := strings.TrimSpace(strs[0])
@@ -300,7 +309,7 @@ customRoleLoop:
300309
Defaults: defaultActions(role, resource),
301310
})
302311
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)
304313
}
305314
applyOrgResourceActions(role, resource, actions)
306315
// back to resources!
@@ -309,7 +318,7 @@ customRoleLoop:
309318
// This println is required because the prompt ends us on the same line as some text.
310319
_, _ = fmt.Println()
311320

312-
return role, nil
321+
return role, newRole, nil
313322
}
314323

315324
func applyOrgResourceActions(role *codersdk.Role, resource string, actions []string) {

site/src/api/typesGenerated.ts

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)