GitLabgitlab-org

cli

main
ply8 git calls190 ms99 KiB6 cachedprotocol v2
207 lines5.0 KiBRaw
1package list
3import (
4 "context"
5 "strings"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/glrepo"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/tableprinter"
18 "gitlab.com/gitlab-org/cli/internal/utils"
21type options struct {
22 io *iostreams.IOStreams
23 apiClient func(repoHost string) (*api.Client, error)
24 baseRepo func() (glrepo.Interface, error)
26 page int64
27 perPage int64
28 outputFormat string
29 group string
30 instance bool
33func NewCmd(f cmdutils.Factory) *cobra.Command {
34 opts := &options{
35 io: f.IO(),
36 apiClient: f.ApiClient,
37 baseRepo: f.BaseRepo,
38 }
40 cmd := &cobra.Command{
41 Use: "list [flags]",
42 Short: "List runners.",
43 Long: heredoc.Doc(`
44 List runners for a project (default), group, or instance.
46 Instance scope requires administrator access.
47 `),
48 Aliases: []string{"ls"},
49 Args: cobra.NoArgs,
50 ValidArgsFunction: cobra.NoFileCompletions,
51 Example: heredoc.Doc(`
52 # List runners for the current project
53 glab runner list
55 # List runners for a specific project
56 glab runner list -R owner/repo
58 # List runners for a group
59 glab runner list --group mygroup
61 # List runners as JSON
62 glab runner list --output json`),
63 Annotations: map[string]string{
64 mcpannotations.Safe: "true",
65 },
66 RunE: func(cmd *cobra.Command, args []string) error {
67 if err := opts.complete(cmd); err != nil {
68 return err
69 }
70 return opts.run(cmd.Context())
71 },
72 }
74 cmdutils.EnableRepoOverride(cmd, f)
75 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
76 fl := cmd.Flags()
77 fl.Int64VarP(&opts.page, "page", "p", 1, "Page number.")
78 fl.Int64VarP(&opts.perPage, "per-page", "P", api.DefaultListLimit, "Number of items to list per page.")
79 fl.StringVarP(&opts.group, "group", "g", "", "List runners for a group. Ignored if -R/--repo is set.")
80 fl.BoolVarP(&opts.instance, "instance", "i", false, "List all runners available to the user (instance scope).")
81 cmd.MarkFlagsMutuallyExclusive("instance", "group", "repo")
83 return cmd
86func (o *options) complete(cmd *cobra.Command) error {
87 group, err := cmdutils.GroupOverride(cmd)
88 if err != nil {
89 return err
90 }
91 o.group = group
92 return nil
95func (o *options) run(ctx context.Context) error {
96 repo, repoErr := o.baseRepo()
97 var repoHost string
98 if repoErr == nil {
99 repoHost = repo.RepoHost()
100 }
101 apiClient, err := o.apiClient(repoHost)
102 if err != nil {
103 return err
104 }
105 client := apiClient.Lab()
107 listOpts := &gitlab.ListRunnersOptions{
108 ListOptions: gitlab.ListOptions{
109 Page: o.page,
110 PerPage: o.perPage,
111 },
112 }
114 var runners []*gitlab.Runner
115 var resp *gitlab.Response
116 var scopeLabel string
118 listOptsBase := gitlab.ListOptions{
119 Page: o.page,
120 PerPage: o.perPage,
121 }
123 switch {
124 case o.instance:
125 runners, resp, err = client.Runners.ListRunners(listOpts, gitlab.WithContext(ctx))
126 if err != nil {
127 return err
128 }
129 scopeLabel = "instance"
130 case o.group != "":
131 runners, resp, err = client.Runners.ListGroupsRunners(o.group, &gitlab.ListGroupsRunnersOptions{
132 ListOptions: listOptsBase,
133 }, gitlab.WithContext(ctx))
134 if err != nil {
135 return err
136 }
137 scopeLabel = o.group
138 default:
139 if repoErr != nil {
140 return repoErr
141 }
142 runners, resp, err = client.Runners.ListProjectRunners(repo.FullName(), &gitlab.ListProjectRunnersOptions{
143 ListOptions: listOptsBase,
144 }, gitlab.WithContext(ctx))
145 if err != nil {
146 return err
147 }
148 scopeLabel = repo.FullName()
149 }
151 switch o.outputFormat {
152 case "json":
153 return o.io.PrintJSON(runners)
154 default:
155 return o.printTable(runners, resp, scopeLabel)
156 }
159func (o *options) printTable(runners []*gitlab.Runner, resp *gitlab.Response, scopeLabel string) error {
160 title := utils.NewListTitle("runner")
161 title.RepoName = scopeLabel
162 title.Page = int(o.page)
163 title.CurrentPageTotal = len(runners)
164 if resp != nil {
165 title.Total = int(resp.TotalItems)
166 }
168 if err := o.io.StartPager(); err != nil {
169 return err
170 }
171 defer o.io.StopPager()
173 o.io.LogInfof("%s\n%s\n", title.Describe(), displayRunners(o.io, runners))
174 return nil
177func displayRunners(io *iostreams.IOStreams, runners []*gitlab.Runner) string {
178 c := io.Color()
179 table := tableprinter.NewTablePrinter()
180 table.AddRow(c.Bold("ID"), c.Bold("Description"), c.Bold("Status"), c.Bold("Paused"))
181 for _, r := range runners {
182 table.AddRow(
183 r.ID,
184 r.Description,
185 formatStatus(c, r.Status),
186 r.Paused,
187 )
188 }
189 return table.Render()
192func formatStatus(c *iostreams.ColorPalette, status string) string {
193 switch strings.ToLower(status) {
194 case "online":
195 return c.Green(status)
196 case "offline":
197 return c.Gray(status)
198 case "stale":
199 return c.Magenta(status)
200 case "never_contacted":
201 return c.Yellow(status)
202 case "paused":
203 return c.Yellow(status)
204 default:
205 return status
206 }
Diagnostics8 git calls · 190 ms
CallStatusSizeTime0190 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200314 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=314
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=9182edcf49c51a2d4d26f5f18ad76a93a377699f
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=314
git.pkt.payload.max=287
git.sideband.band1.bytes=287
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=287
git.pack.trailer=d25ce511fa222aedcda012c18364dcb1f012062b
Open pack map
fetchtree:0200135 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=177
git.response.bytes=135
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=4c29033a778ddfc5e7c3564b6cd14f3c734a6f68
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=135
git.pkt.payload.max=108
git.sideband.band1.bytes=108
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=108
git.pack.trailer=4804568d56624ee73c06fd30ea4875d8099250bb
Open pack map
fetch2001.8 KiB177 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1867
git.response.cache=MISS
git.response.duration_ms=177
git.fetch.wants=3d6751e23c90bfaff00ef3cd0f266ce0c3c1acd2
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1867
git.pkt.payload.max=1840
git.sideband.band1.bytes=1840
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2c439ce80aedd
Request
7b99b870…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format