MCPcopy Index your code
hub / github.com/coder/coder / createOrganization

Method createOrganization

cli/organizationmanage.go:14–58  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12)
13
14func (r *RootCmd) createOrganization() *serpent.Command {
15 cmd := &serpent.Command{
16 Use: "create <organization name>",
17 Short: "Create a new organization.",
18 Middleware: serpent.Chain(
19 serpent.RequireNArgs(1),
20 ),
21 Options: serpent.OptionSet{
22 cliui.SkipPromptOption(),
23 },
24 Handler: func(inv *serpent.Invocation) error {
25 client, err := r.InitClient(inv)
26 if err != nil {
27 return err
28 }
29
30 orgName := inv.Args[0]
31
32 err = codersdk.NameValid(orgName)
33 if err != nil {
34 return xerrors.Errorf("organization name %q is invalid: %w", orgName, err)
35 }
36
37 // This check is not perfect since not all users can read all organizations.
38 // So ignore the error and if the org already exists, prevent the user
39 // from creating it.
40 existing, _ := client.OrganizationByName(inv.Context(), orgName)
41 if existing.ID != uuid.Nil {
42 return xerrors.Errorf("organization %q already exists", orgName)
43 }
44
45 organization, err := client.CreateOrganization(inv.Context(), codersdk.CreateOrganizationRequest{
46 Name: orgName,
47 })
48 if err != nil {
49 return xerrors.Errorf("failed to create organization: %w", err)
50 }
51
52 _, _ = fmt.Fprintf(inv.Stdout, "Organization %s (%s) created.\n", organization.Name, organization.ID)
53 return nil
54 },
55 }
56
57 return cmd
58}

Callers 4

organizationsMethod · 0.95
createOrganizationFunction · 0.80
createOrganizationFunction · 0.80

Calls 7

InitClientMethod · 0.95
SkipPromptOptionFunction · 0.92
NameValidFunction · 0.92
OrganizationByNameMethod · 0.80
CreateOrganizationMethod · 0.80
ContextMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected