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
67 changes: 67 additions & 0 deletions pkg/cmd/org/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org

import (
"fmt"
"time"

"github.com/brevdev/brev-cli/pkg/cmd/cmderrors"
"github.com/brevdev/brev-cli/pkg/cmdcontext"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/spf13/cobra"
)

func NewCmdOrgCreate(t *terminal.Terminal, orgcmdStore OrgCmdStore) *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{"housekeeping": ""},
Use: "create",
Short: "Create a new organization",
Long: "Create a new organization with the specified name",
Example: `
brev org create my-new-org
`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := cmdcontext.InvokeParentPersistentPreRun(cmd, args)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
Args: cmderrors.TransformToValidationError(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
err := createOrg(args[0], orgcmdStore, t)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}
return cmd
}

func createOrg(orgName string, createStore OrgCmdStore, t *terminal.Terminal) error {
if len(orgName) == 0 {
return breverrors.NewValidationError("organization name cannot be empty")
}

startTime := time.Now()

t.Vprintf("Creating organization %s...\n", t.Green(orgName))

org, err := createStore.CreateOrganization(store.CreateOrganizationRequest{
Name: orgName,
})
if err != nil {
return breverrors.WrapAndTrace(err)
}

duration := time.Since(startTime)
fmt.Print("\n")
t.Vprint(t.Green(fmt.Sprintf("✓ Successfully created organization %s (ID: %s) in %v\n", org.Name, org.ID, duration.Round(time.Millisecond))))
Comment thread
cloin marked this conversation as resolved.
fmt.Print("\n")
t.Vprint(t.Yellow("Switch to this organization:\n"))
t.Vprint(t.Yellow(fmt.Sprintf("\tbrev org set %s\n", org.Name)))

return nil
}
2 changes: 2 additions & 0 deletions pkg/cmd/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type OrgCmdStore interface {
GetServerSockFile() string
CreateInviteLink(organizationID string) (string, error)
GetCurrentWorkspaceID() (string, error)
CreateOrganization(req store.CreateOrganizationRequest) (*entity.Organization, error)
}

func NewCmdOrg(t *terminal.Terminal, orgcmdStore OrgCmdStore, noorgcmdStore OrgCmdStore) *cobra.Command {
Expand Down Expand Up @@ -64,6 +65,7 @@ func NewCmdOrg(t *terminal.Terminal, orgcmdStore OrgCmdStore, noorgcmdStore OrgC

cmd.AddCommand(NewCmdOrgSet(t, orgcmdStore, noorgcmdStore))
cmd.AddCommand(NewCmdOrgLs(t, orgcmdStore))
cmd.AddCommand(NewCmdOrgCreate(t, orgcmdStore))
cmd.AddCommand(invite.NewCmdInvite(t, orgcmdStore, noorgcmdStore))

return cmd
Expand Down
Loading