Skip to content

feat: switch organization context in coder organizations #12265

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 10 commits into from
Feb 26, 2024
Merged
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
feat: add coder organizations switch to change org context
  • Loading branch information
Emyrk committed Feb 26, 2024
commit 0c2cf56a915534ea4abc2aa74613111cc634bd5f
41 changes: 41 additions & 0 deletions cli/organization.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

import (
"errors"
"fmt"
"os"
"strings"

"github.com/coder/coder/v2/cli/clibase"
Expand All @@ -28,6 +30,45 @@ func (r *RootCmd) organizations() *clibase.Cmd {
return cmd
}

func (r *RootCmd) switchOrganization() *clibase.Cmd {
var (
client = new(codersdk.Client)
)

cmd := &clibase.Cmd{
Use: "switch <organization name | ID>",
Short: "Switch the organization used by the cli. Pass an empty string to reset to the default organization.",
Long: "Switch the organization used by the cli. Pass an empty string to reset to the default organization.\n" + formatExamples(
example{
Description: "Remove the current organization and defer to the default.",
Command: "coder organizations switch ''",
},
example{
Description: "Switch to a custom organization.",
Command: "coder organizations switch my-org",
},
),
Middleware: clibase.Chain(
r.InitClient(client),
),
Options: clibase.OptionSet{},
Handler: func(inv *clibase.Invocation) error {
conf := r.createConfig()
// If the user passes an empty string, we want to remove the organization
if inv.Args[0] == "" {
err := conf.Organization().Delete()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to unset organization: %w", err)
}
}

return nil
},
}

return cmd
}

func (r *RootCmd) currentOrganization() *clibase.Cmd {
var (
stringFormat func(orgs []codersdk.Organization) (string, error)
Expand Down