Skip to content

Commit 341f59d

Browse files
committed
[FEAT]: Add team command group
1 parent 33f1e65 commit 341f59d

9 files changed

Lines changed: 1079 additions & 0 deletions

File tree

cmd/team/create.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

cmd/team/delete.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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/pkg/cmd/factory"
14+
"github.com/buildkite/cli/v3/pkg/cmd/validation"
15+
)
16+
17+
type DeleteCmd struct {
18+
TeamUUID string `arg:"" help:"UUID of the team to delete" name:"team-uuid"`
19+
}
20+
21+
func (c *DeleteCmd) Help() string {
22+
return `
23+
Delete a team from the organization.
24+
25+
You will be prompted to confirm deletion unless --yes is set.
26+
27+
Examples:
28+
# Delete a team (with confirmation prompt)
29+
$ bk team delete my-team-uuid
30+
31+
# Delete a team without confirmation
32+
$ bk team delete my-team-uuid --yes
33+
`
34+
}
35+
36+
func (c *DeleteCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
37+
f, err := factory.New(factory.WithDebug(globals.EnableDebug()))
38+
if err != nil {
39+
return err
40+
}
41+
42+
f.SkipConfirm = globals.SkipConfirmation()
43+
f.NoInput = globals.DisableInput()
44+
f.Quiet = globals.IsQuiet()
45+
46+
if err := validation.ValidateConfiguration(f.Config, kongCtx.Command()); err != nil {
47+
return err
48+
}
49+
50+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
51+
defer stop()
52+
53+
confirmed, err := bkIO.Confirm(f, fmt.Sprintf("Are you sure you want to delete team %s?", c.TeamUUID))
54+
if err != nil {
55+
return err
56+
}
57+
if !confirmed {
58+
fmt.Fprintln(os.Stderr, "Deletion cancelled.")
59+
return nil
60+
}
61+
62+
spinErr := bkIO.SpinWhile(f, "Deleting team", func() {
63+
_, err = f.RestAPIClient.Teams.DeleteTeam(ctx, f.Config.OrganizationSlug(), c.TeamUUID)
64+
})
65+
if spinErr != nil {
66+
return spinErr
67+
}
68+
if err != nil {
69+
return fmt.Errorf("error deleting team: %v", err)
70+
}
71+
72+
fmt.Fprintln(os.Stderr, "Team deleted successfully.")
73+
return nil
74+
}

cmd/team/list.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package team
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
"github.com/alecthomas/kong"
12+
"github.com/buildkite/cli/v3/internal/cli"
13+
bkIO "github.com/buildkite/cli/v3/internal/io"
14+
"github.com/buildkite/cli/v3/internal/team"
15+
"github.com/buildkite/cli/v3/pkg/cmd/factory"
16+
"github.com/buildkite/cli/v3/pkg/cmd/validation"
17+
"github.com/buildkite/cli/v3/pkg/output"
18+
buildkite "github.com/buildkite/go-buildkite/v4"
19+
)
20+
21+
type ListCmd struct {
22+
output.OutputFlags
23+
}
24+
25+
func (c *ListCmd) Help() string {
26+
return `
27+
List the teams for an organization.
28+
29+
Examples:
30+
# List all teams
31+
$ bk team list
32+
33+
# List teams in JSON format
34+
$ bk team list -o json
35+
`
36+
}
37+
38+
func (c *ListCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
39+
f, err := factory.New(factory.WithDebug(globals.EnableDebug()))
40+
if err != nil {
41+
return err
42+
}
43+
44+
f.SkipConfirm = globals.SkipConfirmation()
45+
f.NoInput = globals.DisableInput()
46+
f.Quiet = globals.IsQuiet()
47+
f.NoPager = f.NoPager || globals.DisablePager()
48+
49+
if err := validation.ValidateConfiguration(f.Config, kongCtx.Command()); err != nil {
50+
return err
51+
}
52+
53+
format := output.ResolveFormat(c.Output, f.Config.OutputFormat())
54+
55+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
56+
defer stop()
57+
58+
teams, err := listTeams(ctx, f)
59+
if err != nil {
60+
return err
61+
}
62+
63+
if format != output.FormatText {
64+
return output.Write(os.Stdout, teams, format)
65+
}
66+
67+
summary := team.TeamViewTable(teams...)
68+
69+
writer, cleanup := bkIO.Pager(f.NoPager, f.Config.Pager())
70+
defer func() { _ = cleanup() }()
71+
72+
fmt.Fprintf(writer, "%v\n", summary)
73+
74+
return nil
75+
}
76+
77+
func listTeams(ctx context.Context, f *factory.Factory) ([]buildkite.Team, error) {
78+
var teams []buildkite.Team
79+
var err error
80+
81+
spinErr := bkIO.SpinWhile(f, "Loading teams information", func() {
82+
teams, _, err = f.RestAPIClient.Teams.List(ctx, f.Config.OrganizationSlug(), nil)
83+
})
84+
if spinErr != nil {
85+
return nil, spinErr
86+
}
87+
if err != nil {
88+
return nil, fmt.Errorf("error fetching team list: %v", err)
89+
}
90+
91+
if len(teams) < 1 {
92+
return nil, errors.New("no teams found in organization")
93+
}
94+
95+
return teams, nil
96+
}

0 commit comments

Comments
 (0)