Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (r *RootCmd) organizations() *clibase.Cmd {
Children: []*clibase.Cmd{
r.currentOrganization(),
r.switchOrganization(),
r.createOrganization(),
},
}

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

import (
"fmt"

"github.com/google/uuid"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/pretty"
)

func (r *RootCmd) createOrganization() *clibase.Cmd {
client := new(codersdk.Client)

cmd := &clibase.Cmd{
Use: "create <organization name>",
Short: "Create a new organization.",
// This action is currently irreversible, so it's hidden until we have a way to delete organizations.
Hidden: true,
Middleware: clibase.Chain(
r.InitClient(client),
clibase.RequireNArgs(1),
),
Options: clibase.OptionSet{
cliui.SkipPromptOption(),
},
Handler: func(inv *clibase.Invocation) error {
orgName := inv.Args[0]

// This check is not perfect since not all users can read all organizations.
// So ignore the error and if the org already exists, prevent the user
// from creating it.
existing, _ := client.OrganizationByName(inv.Context(), orgName)
if existing.ID != uuid.Nil {
return fmt.Errorf("organization %q already exists", orgName)
}

_, err := cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Are you sure you want to create an organization with the name %s?\n%s",
pretty.Sprint(cliui.DefaultStyles.Code, orgName),
pretty.Sprint(cliui.BoldFmt(), "This action is irreversible."),
),
IsConfirm: true,
Default: cliui.ConfirmNo,
})
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle cliui.Canceled? Just exit with code 1 and show no error (didn't test, maybe that's what happens).

Copy link
Member Author

@Emyrk Emyrk Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just confirmed if the context is cancelled, then it is exit code 1 with

$ coder orgs create test
> Are you sure you want to create an organization with the name  test ?
This action is irreversible. (yes/no) 
Encountered an error running "coder organizations create"
context canceled

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that we print context cancelled. Should just exit silently with 1.

return err
}

organization, err := client.CreateOrganization(inv.Context(), codersdk.CreateOrganizationRequest{
Name: orgName,
})
if err != nil {
return fmt.Errorf("failed to create organization: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Organization %s (%s) created.\n", organization.Name, organization.ID)
return nil
},
}

return cmd
}