-
Notifications
You must be signed in to change notification settings - Fork 117
/
create.go
76 lines (64 loc) · 1.68 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package org
import (
"fmt"
"strings"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)
func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
var name, description string
createCmd := &cobra.Command{
Use: "create [<org-name>]",
Short: "Create organization",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := ch.Client()
if err != nil {
return err
}
if len(args) > 0 {
name = args[0]
}
if len(args) == 0 && ch.Interactive {
err = cmdutil.SetFlagsByInputPrompts(*cmd, "name")
if err != nil {
return err
}
}
res, err := client.CreateOrganization(cmd.Context(), &adminv1.CreateOrganizationRequest{
Name: name,
Description: description,
})
if err != nil {
if !isNameExistsErr(err) {
return err
}
fmt.Printf("Org name %q already exists\n", name)
return nil
}
// Switching to the created org
err = dotrill.SetDefaultOrg(res.Organization.Name)
if err != nil {
return err
}
ch.PrintfSuccess("Created organization\n")
ch.PrintOrgs([]*adminv1.Organization{res.Organization}, "")
return nil
},
}
createCmd.Flags().SortFlags = false
createCmd.Flags().StringVar(&name, "name", "", "Organization Name")
createCmd.Flags().StringVar(&description, "description", "", "Description")
return createCmd
}
func isNameExistsErr(err error) bool {
if strings.Contains(err.Error(), "already exists") {
return true
}
if strings.Contains(err.Error(), "violates unique constraint") {
return true
}
return false
}