Skip to content

Commit 6a59ee1

Browse files
committed
chore: add cli command to fetch group sync settings as json
1 parent 45420b9 commit 6a59ee1

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

cli/organization.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (r *RootCmd) organizations() *serpent.Command {
2626
r.createOrganization(),
2727
r.organizationMembers(orgContext),
2828
r.organizationRoles(orgContext),
29+
r.organizationSettings(orgContext),
2930
},
3031
}
3132

cli/organizationsettings.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/coder/coder/v2/codersdk"
10+
"github.com/coder/serpent"
11+
)
12+
13+
func (r *RootCmd) organizationSettings(orgContext *OrganizationContext) *serpent.Command {
14+
cmd := &serpent.Command{
15+
Use: "settings",
16+
Short: "Manage organization settings.",
17+
Aliases: []string{"setting"},
18+
Handler: func(inv *serpent.Invocation) error {
19+
return inv.Command.HelpHandler(inv)
20+
},
21+
Hidden: true,
22+
Children: []*serpent.Command{
23+
r.printOrganizationSetting(orgContext),
24+
},
25+
}
26+
return cmd
27+
}
28+
29+
func (r *RootCmd) printOrganizationSetting(orgContext *OrganizationContext) *serpent.Command {
30+
client := new(codersdk.Client)
31+
cmd := &serpent.Command{
32+
Use: "show <groupsync | rolesync>",
33+
Short: "Outputs specified organization setting.",
34+
Long: FormatExamples(
35+
Example{
36+
Description: "Output group sync settings.",
37+
Command: "coder organization settings show groupsync",
38+
},
39+
),
40+
Options: []serpent.Option{},
41+
Middleware: serpent.Chain(
42+
serpent.RequireNArgs(1),
43+
r.InitClient(client),
44+
),
45+
Handler: func(inv *serpent.Invocation) error {
46+
ctx := inv.Context()
47+
org, err := orgContext.Selected(inv, client)
48+
if err != nil {
49+
return err
50+
}
51+
52+
var setting any
53+
switch strings.ToLower(inv.Args[0]) {
54+
case "groupsync", "group-sync":
55+
setting, err = client.GroupIDPSyncSettings(ctx, org.ID.String())
56+
case "rolesync", "role-sync":
57+
// TODO: Implement role sync settings.
58+
return fmt.Errorf("role sync settings are not implemented")
59+
default:
60+
_, _ = fmt.Fprintln(inv.Stderr, "Valid organization settings are: 'groupsync', 'rolesync'")
61+
return fmt.Errorf("unknown organization setting %s", inv.Args[0])
62+
}
63+
64+
if err != nil {
65+
return fmt.Errorf("failed to get organization setting %s: %w", inv.Args[0], err)
66+
}
67+
68+
settingJson, err := json.Marshal(setting)
69+
if err != nil {
70+
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
71+
}
72+
73+
var dst bytes.Buffer
74+
err = json.Indent(&dst, settingJson, "", "\t")
75+
if err != nil {
76+
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
77+
}
78+
79+
_, err = fmt.Fprintln(inv.Stdout, dst.String())
80+
return err
81+
},
82+
}
83+
return cmd
84+
}

cli/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"golang.org/x/mod/semver"
3030
"golang.org/x/xerrors"
3131

32+
"github.com/coder/coder/v2/coderd/database/db2sdk"
3233
"github.com/coder/pretty"
3334

3435
"github.com/coder/coder/v2/buildinfo"
@@ -657,7 +658,10 @@ func (o *OrganizationContext) Selected(inv *serpent.Invocation, client *codersdk
657658
}
658659

659660
// No org selected, and we are more than 1? Return an error.
660-
return codersdk.Organization{}, xerrors.Errorf("Must select an organization with --org=<org_name>.")
661+
validOrgs := db2sdk.List(orgs, func(org codersdk.Organization) string {
662+
return fmt.Sprintf("%q", org.Name)
663+
})
664+
return codersdk.Organization{}, xerrors.Errorf("Must select an organization with --org=<org_name>. Choose from: %s", strings.Join(validOrgs, ", "))
661665
}
662666

663667
func splitNamedWorkspace(identifier string) (owner string, workspaceName string, err error) {

0 commit comments

Comments
 (0)