()
| 12 | ) |
| 13 | |
| 14 | func (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 | } |
no test coverage detected