GitLabgitlab-org
cli
main
1package list
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "time"
9 "github.com/MakeNowJust/heredoc/v2"
10 "github.com/spf13/cobra"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/glrepo"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
18 "gitlab.com/gitlab-org/cli/internal/tableprinter"
19)
21type options struct {
22 io *iostreams.IOStreams
23 gitlabClient func() (*gitlab.Client, error)
24 baseRepoFunc func() (glrepo.Interface, error)
26 agentID int64
27 outputFormat string
28}
30func NewCmd(f cmdutils.Factory) *cobra.Command {
31 opts := options{
32 io: f.IO(),
33 gitlabClient: f.GitLabClient,
34 baseRepoFunc: f.BaseRepo,
35 }
37 cmd := &cobra.Command{
38 Use: "list <agent-id> [flags]",
39 Short: `List tokens for a GitLab Agent for Kubernetes.`,
40 Long: heredoc.Doc(`
41 Lists all tokens for the specified agent. Requires the numeric agent ID.
42 `),
43 Example: heredoc.Doc(`
44 # List tokens for agent 123
45 glab cluster agent token list 123
47 # List tokens in JSON format
48 glab cluster agent token list 123 --output json`),
49 Args: cobra.ExactArgs(1),
50 Annotations: map[string]string{
51 mcpannotations.Safe: "true",
52 },
53 RunE: func(cmd *cobra.Command, args []string) error {
54 if err := opts.complete(args); err != nil {
55 return err
56 }
58 return opts.run(cmd.Context())
59 },
60 }
62 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
64 return cmd
65}
67func (o *options) complete(args []string) error {
68 agentID, err := strconv.ParseInt(args[0], 10, 64)
69 if err != nil {
70 return fmt.Errorf("agent ID must be a valid integer, got %q", args[0])
71 }
72 o.agentID = agentID
74 return nil
75}
77func (o *options) run(ctx context.Context) error {
78 client, err := o.gitlabClient()
79 if err != nil {
80 return err
81 }
82 baseRepo, err := o.baseRepoFunc()
83 if err != nil {
84 return err
85 }
87 tokens, _, err := client.ClusterAgents.ListAgentTokens(baseRepo.FullName(), o.agentID, nil, gitlab.WithContext(ctx))
88 if err != nil {
89 return fmt.Errorf("unable to retrieve agent tokens: %w", err)
90 }
92 if o.outputFormat == "json" {
93 return o.io.PrintJSON(tokens)
94 }
96 c := o.io.Color()
97 bold := c.Bold
99 table := tableprinter.NewTablePrinter()
100 table.AddRow(bold("ID"), bold("Name"), bold("Status"), bold("Created At"), bold("Created By"), bold("Last Used At"), bold("Description"))
101 var username string
102 // NOTE: there can only ever be two tokens registered for an agent at once, therefore, it's safe to assume that
103 // we only ever get a maximum of two items back from the API, despite it's slice return type.
104 var cachedUserID int64
105 for _, token := range tokens {
106 var lastUsedAt string
107 switch token.LastUsedAt {
108 case nil:
109 lastUsedAt = c.Gray("never")
110 default:
111 lastUsedAt = token.LastUsedAt.Format(time.RFC3339)
112 }
114 if cachedUserID != token.CreatedByUserID {
115 user, _, err := client.Users.GetUser(token.CreatedByUserID, &gitlab.GetUserOptions{}, gitlab.WithContext(ctx))
116 if err != nil {
117 username = fmt.Sprintf("%d", token.CreatedByUserID)
118 } else {
119 username = user.Username
120 }
121 cachedUserID = token.CreatedByUserID
122 }
124 table.AddRow(token.ID, token.Name, token.Status, token.CreatedAt.Format(time.RFC3339), username, lastUsedAt, token.Description)
125 }
126 o.io.LogInfof("%s", table.Render())
128 return nil
129}
Diagnostics10 git calls · 322 ms
CallStatusSizeTime0322 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=2 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 · 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=32926 git.response.cache=HIT git.response.duration_ms=2 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=44Open 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200200 B · 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=200 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4 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=200 git.pkt.payload.max=173 git.sideband.band1.bytes=173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=173 git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9Open pack map
fetchtree:0200405 B · 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=405 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531 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=405 git.pkt.payload.max=378 git.sideband.band1.bytes=378 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=378 git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934Open pack map
fetchtree:0200167 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=167 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=7dfd1c9bf93d8973834f70c611fab6bb6c6258a5 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=167 git.pkt.payload.max=140 git.sideband.band1.bytes=140 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=140 git.pack.trailer=8b63cee433b4118712f1ed3590553e906f29e021Open pack map
fetchtree:0200134 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=134 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=e459914cb4e2e9e3447771739e12c8d7de47d97b 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=77dbe9638eb886ae0e4dccdf90191fdcc46fe838Open pack map
fetch2001.4 KiB303 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=303 git.response.bytes=1484 git.response.cache=MISS git.response.duration_ms=303 git.fetch.wants=77179f1c8bfad85d8903e4f61819bfa4997c1fd6 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=1484 git.pkt.payload.max=1457 git.sideband.band1.bytes=1457 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
- a5eb14bb0b0bcf75
- Request
- 81e6fc4b…
- 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