|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/alecthomas/kong" |
| 10 | + "github.com/buildkite/cli/v3/internal/agent" |
| 11 | + "github.com/buildkite/cli/v3/internal/cli" |
| 12 | + "github.com/buildkite/cli/v3/internal/version" |
| 13 | + "github.com/buildkite/cli/v3/pkg/cmd/factory" |
| 14 | + "github.com/buildkite/cli/v3/pkg/cmd/validation" |
| 15 | + "github.com/buildkite/cli/v3/pkg/output" |
| 16 | + buildkite "github.com/buildkite/go-buildkite/v4" |
| 17 | + tea "github.com/charmbracelet/bubbletea" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + stateRunning = "running" |
| 22 | + stateIdle = "idle" |
| 23 | + statePaused = "paused" |
| 24 | +) |
| 25 | + |
| 26 | +var validStates = []string{stateRunning, stateIdle, statePaused} |
| 27 | + |
| 28 | +type ListCmd struct { |
| 29 | + Name string `help:"Filter agents by their name"` |
| 30 | + Version string `help:"Filter agents by their version"` |
| 31 | + Hostname string `help:"Filter agents by their hostname"` |
| 32 | + State string `help:"Filter agents by state (running, idle, paused)"` |
| 33 | + Tags []string `help:"Filter agents by tags"` |
| 34 | + PerPage int `help:"Number of agents per page" default:"30"` |
| 35 | + Limit int `help:"Maximum number of agents to return" default:"100"` |
| 36 | + Output string `help:"Output format. One of: json, yaml, text" short:"o" default:"${output_default_format}"` |
| 37 | +} |
| 38 | + |
| 39 | +func (c *ListCmd) Help() string { |
| 40 | + return `By default, shows up to 100 agents. Use filters to narrow results, or increase the number of agents displayed with --limit. |
| 41 | +
|
| 42 | +Examples: |
| 43 | + # List all agents |
| 44 | + $ bk agent list |
| 45 | +
|
| 46 | + # List agents with JSON output |
| 47 | + $ bk agent list --output json |
| 48 | +
|
| 49 | + # List only running agents (currently executing jobs) |
| 50 | + $ bk agent list --state running |
| 51 | +
|
| 52 | + # List only idle agents (connected but not running jobs) |
| 53 | + $ bk agent list --state idle |
| 54 | +
|
| 55 | + # List only paused agents |
| 56 | + $ bk agent list --state paused |
| 57 | +
|
| 58 | + # Filter agents by hostname |
| 59 | + $ bk agent list --hostname my-server-01 |
| 60 | +
|
| 61 | + # Combine state and hostname filters |
| 62 | + $ bk agent list --state idle --hostname my-server-01 |
| 63 | +
|
| 64 | + # Filter agents by tags |
| 65 | + $ bk agent list --tags queue=default |
| 66 | +
|
| 67 | + # Filter agents by multiple tags (all must match) |
| 68 | + $ bk agent list --tags queue=default --tags os=linux |
| 69 | +
|
| 70 | + # Multiple filters with output format |
| 71 | + $ bk agent list --state running --version 3.107.2 --output json` |
| 72 | +} |
| 73 | + |
| 74 | +func (c *ListCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error { |
| 75 | + f, err := factory.New(version.Version) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + f.SkipConfirm = globals.SkipConfirmation() |
| 81 | + f.NoInput = globals.DisableInput() |
| 82 | + f.Quiet = globals.IsQuiet() |
| 83 | + |
| 84 | + if err := validation.ValidateConfiguration(f.Config, kongCtx.Command()); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + ctx := context.Background() |
| 89 | + |
| 90 | + if err := validateState(c.State); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + format := output.Format(c.Output) |
| 95 | + |
| 96 | + // Skip TUI when using non-text format (JSON/YAML) |
| 97 | + if format != output.FormatText { |
| 98 | + agents := []buildkite.Agent{} |
| 99 | + page := 1 |
| 100 | + |
| 101 | + for len(agents) < c.Limit && page < 50 { |
| 102 | + opts := buildkite.AgentListOptions{ |
| 103 | + Name: c.Name, |
| 104 | + Hostname: c.Hostname, |
| 105 | + Version: c.Version, |
| 106 | + ListOptions: buildkite.ListOptions{ |
| 107 | + Page: page, |
| 108 | + PerPage: c.PerPage, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + pageAgents, _, err := f.RestAPIClient.Agents.List(ctx, f.Config.OrganizationSlug(), &opts) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + if len(pageAgents) == 0 { |
| 118 | + break |
| 119 | + } |
| 120 | + |
| 121 | + filtered := filterAgents(pageAgents, c.State, c.Tags) |
| 122 | + agents = append(agents, filtered...) |
| 123 | + page++ |
| 124 | + } |
| 125 | + |
| 126 | + if len(agents) > c.Limit { |
| 127 | + agents = agents[:c.Limit] |
| 128 | + } |
| 129 | + |
| 130 | + return output.Write(os.Stdout, agents, format) |
| 131 | + } |
| 132 | + |
| 133 | + loader := func(page int) tea.Cmd { |
| 134 | + return func() tea.Msg { |
| 135 | + opts := buildkite.AgentListOptions{ |
| 136 | + Name: c.Name, |
| 137 | + Hostname: c.Hostname, |
| 138 | + Version: c.Version, |
| 139 | + ListOptions: buildkite.ListOptions{ |
| 140 | + Page: page, |
| 141 | + PerPage: c.PerPage, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + agents, resp, err := f.RestAPIClient.Agents.List(ctx, f.Config.OrganizationSlug(), &opts) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + filtered := filterAgents(agents, c.State, c.Tags) |
| 151 | + |
| 152 | + items := make([]agent.AgentListItem, len(filtered)) |
| 153 | + for i, a := range filtered { |
| 154 | + a := a |
| 155 | + items[i] = agent.AgentListItem{Agent: a} |
| 156 | + } |
| 157 | + |
| 158 | + return agent.NewAgentItemsMsg(items, resp.LastPage) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + model := agent.NewAgentList(loader, 1, c.PerPage, f.Quiet) |
| 163 | + |
| 164 | + p := tea.NewProgram(model, tea.WithAltScreen()) |
| 165 | + _, err = p.Run() |
| 166 | + return err |
| 167 | +} |
| 168 | + |
| 169 | +func validateState(state string) error { |
| 170 | + if state == "" { |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + normalized := strings.ToLower(state) |
| 175 | + for _, valid := range validStates { |
| 176 | + if normalized == valid { |
| 177 | + return nil |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return fmt.Errorf("invalid state %q: must be one of %s, %s, or %s", state, stateRunning, stateIdle, statePaused) |
| 182 | +} |
| 183 | + |
| 184 | +func filterAgents(agents []buildkite.Agent, state string, tags []string) []buildkite.Agent { |
| 185 | + filtered := make([]buildkite.Agent, 0, len(agents)) |
| 186 | + for _, a := range agents { |
| 187 | + if matchesState(a, state) && matchesTags(a, tags) { |
| 188 | + filtered = append(filtered, a) |
| 189 | + } |
| 190 | + } |
| 191 | + return filtered |
| 192 | +} |
| 193 | + |
| 194 | +func matchesState(a buildkite.Agent, state string) bool { |
| 195 | + if state == "" { |
| 196 | + return true |
| 197 | + } |
| 198 | + |
| 199 | + normalized := strings.ToLower(state) |
| 200 | + switch normalized { |
| 201 | + case stateRunning: |
| 202 | + return a.Job != nil |
| 203 | + case stateIdle: |
| 204 | + return a.Job == nil && (a.Paused == nil || !*a.Paused) |
| 205 | + case statePaused: |
| 206 | + return a.Paused != nil && *a.Paused |
| 207 | + default: |
| 208 | + return false |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func matchesTags(a buildkite.Agent, tags []string) bool { |
| 213 | + if len(tags) == 0 { |
| 214 | + return true |
| 215 | + } |
| 216 | + |
| 217 | + for _, tag := range tags { |
| 218 | + if !hasTag(a.Metadata, tag) { |
| 219 | + return false |
| 220 | + } |
| 221 | + } |
| 222 | + return true |
| 223 | +} |
| 224 | + |
| 225 | +func hasTag(metadata []string, tag string) bool { |
| 226 | + for _, meta := range metadata { |
| 227 | + if meta == tag { |
| 228 | + return true |
| 229 | + } |
| 230 | + } |
| 231 | + return false |
| 232 | +} |
0 commit comments