Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: add cli command to fetch group sync settings as json #14694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
chore: add cli command to fetch group sync settings as json
  • Loading branch information
Emyrk committed Sep 17, 2024
commit 6a59ee11636821a83e3b143d24cd5e5ce4ea5be6
1 change: 1 addition & 0 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r *RootCmd) organizations() *serpent.Command {
r.createOrganization(),
r.organizationMembers(orgContext),
r.organizationRoles(orgContext),
r.organizationSettings(orgContext),
},
}

Expand Down
84 changes: 84 additions & 0 deletions cli/organizationsettings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cli

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)

func (r *RootCmd) organizationSettings(orgContext *OrganizationContext) *serpent.Command {
cmd := &serpent.Command{
Use: "settings",
Short: "Manage organization settings.",
Aliases: []string{"setting"},
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
Hidden: true,
Children: []*serpent.Command{
r.printOrganizationSetting(orgContext),
},
}
return cmd
}

func (r *RootCmd) printOrganizationSetting(orgContext *OrganizationContext) *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "show <groupsync | rolesync>",
Short: "Outputs specified organization setting.",
Long: FormatExamples(
Example{
Description: "Output group sync settings.",
Command: "coder organization settings show groupsync",
},
),
Options: []serpent.Option{},
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
org, err := orgContext.Selected(inv, client)
if err != nil {
return err
}

var setting any
switch strings.ToLower(inv.Args[0]) {
case "groupsync", "group-sync":
setting, err = client.GroupIDPSyncSettings(ctx, org.ID.String())
case "rolesync", "role-sync":
// TODO: Implement role sync settings.
return fmt.Errorf("role sync settings are not implemented")
default:
_, _ = fmt.Fprintln(inv.Stderr, "Valid organization settings are: 'groupsync', 'rolesync'")
return fmt.Errorf("unknown organization setting %s", inv.Args[0])
}

if err != nil {
return fmt.Errorf("failed to get organization setting %s: %w", inv.Args[0], err)
}

settingJson, err := json.Marshal(setting)
if err != nil {
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
}

var dst bytes.Buffer
err = json.Indent(&dst, settingJson, "", "\t")
if err != nil {
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
}

_, err = fmt.Fprintln(inv.Stdout, dst.String())
return err
},
}
return cmd
}
6 changes: 5 additions & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/mod/semver"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/pretty"

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

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

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