|
| 1 | +package team |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "syscall" |
| 9 | + |
| 10 | + "github.com/alecthomas/kong" |
| 11 | + "github.com/buildkite/cli/v3/internal/cli" |
| 12 | + bkIO "github.com/buildkite/cli/v3/internal/io" |
| 13 | + "github.com/buildkite/cli/v3/internal/team" |
| 14 | + "github.com/buildkite/cli/v3/pkg/cmd/factory" |
| 15 | + "github.com/buildkite/cli/v3/pkg/cmd/validation" |
| 16 | + "github.com/buildkite/cli/v3/pkg/output" |
| 17 | + buildkite "github.com/buildkite/go-buildkite/v4" |
| 18 | +) |
| 19 | + |
| 20 | +type CreateCmd struct { |
| 21 | + Name string `arg:"" help:"Name of the team" name:"name"` |
| 22 | + Description string `help:"Description of the team" optional:""` |
| 23 | + Privacy string `help:"Privacy setting for the team: visible or secret" optional:"" default:"visible" enum:"visible,secret"` |
| 24 | + Default bool `help:"Whether this is the default team for new members" optional:"" name:"default"` |
| 25 | + DefaultMemberRole string `help:"Default role for new members: member or maintainer" optional:"" name:"default-member-role" default:"member" enum:"member,maintainer"` |
| 26 | + MembersCanCreatePipelines bool `help:"Whether members can create pipelines" optional:"" name:"members-can-create-pipelines"` |
| 27 | + output.OutputFlags |
| 28 | +} |
| 29 | + |
| 30 | +func (c *CreateCmd) Help() string { |
| 31 | + return ` |
| 32 | +Create a new team in the organization. |
| 33 | +
|
| 34 | +Examples: |
| 35 | + # Create a team with default settings |
| 36 | + $ bk team create my-team |
| 37 | +
|
| 38 | + # Create a private team with a description |
| 39 | + $ bk team create my-team --description "My team" --privacy secret |
| 40 | +
|
| 41 | + # Create a default team where members can create pipelines |
| 42 | + $ bk team create my-team --default --members-can-create-pipelines |
| 43 | +` |
| 44 | +} |
| 45 | + |
| 46 | +func (c *CreateCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error { |
| 47 | + f, err := factory.New(factory.WithDebug(globals.EnableDebug())) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + f.SkipConfirm = globals.SkipConfirmation() |
| 53 | + f.NoInput = globals.DisableInput() |
| 54 | + f.Quiet = globals.IsQuiet() |
| 55 | + f.NoPager = f.NoPager || globals.DisablePager() |
| 56 | + |
| 57 | + if err := validation.ValidateConfiguration(f.Config, kongCtx.Command()); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + format := output.ResolveFormat(c.Output, f.Config.OutputFormat()) |
| 62 | + |
| 63 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 64 | + defer stop() |
| 65 | + |
| 66 | + input := buildkite.CreateTeam{ |
| 67 | + Name: c.Name, |
| 68 | + Description: c.Description, |
| 69 | + Privacy: c.Privacy, |
| 70 | + IsDefaultTeam: c.Default, |
| 71 | + DefaultMemberRole: c.DefaultMemberRole, |
| 72 | + MembersCanCreatePipelines: c.MembersCanCreatePipelines, |
| 73 | + } |
| 74 | + |
| 75 | + var t buildkite.Team |
| 76 | + spinErr := bkIO.SpinWhile(f, "Creating team", func() { |
| 77 | + t, _, err = f.RestAPIClient.Teams.CreateTeam(ctx, f.Config.OrganizationSlug(), input) |
| 78 | + }) |
| 79 | + if spinErr != nil { |
| 80 | + return spinErr |
| 81 | + } |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("error creating team: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + teamView := output.Viewable[buildkite.Team]{ |
| 87 | + Data: t, |
| 88 | + Render: team.RenderTeamText, |
| 89 | + } |
| 90 | + |
| 91 | + if format != output.FormatText { |
| 92 | + return output.Write(os.Stdout, teamView, format) |
| 93 | + } |
| 94 | + |
| 95 | + fmt.Fprintf(os.Stdout, "Team %s created successfully\n\n", t.Name) |
| 96 | + return output.Write(os.Stdout, teamView, format) |
| 97 | +} |
0 commit comments