GitLabgitlab-org
cli
main
1package list
3import (
4 "context"
6 "github.com/MakeNowJust/heredoc/v2"
7 "github.com/spf13/cobra"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/api"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/commands/service-account/serviceaccountutils"
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)
20type options struct {
21 io *iostreams.IOStreams
22 apiClient func(repoHost string) (*api.Client, error)
23 baseRepo func() (glrepo.Interface, error)
25 account string
26 active bool
27 group string
28 outputFormat string
29}
31func NewCmd(f cmdutils.Factory) *cobra.Command {
32 opts := &options{
33 io: f.IO(),
34 apiClient: f.ApiClient,
35 baseRepo: f.BaseRepo,
36 }
38 cmd := &cobra.Command{
39 Use: "list <account-id | username> [flags]",
40 Short: "List the tokens of a service account.",
41 Args: cobra.ExactArgs(1),
42 ValidArgsFunction: cobra.NoFileCompletions,
43 Long: heredoc.Doc(`
44 List the personal access tokens of a service account of a project or a group.
46 - A numeric account argument is always treated as an ID.
47 - Token values are never shown. GitLab returns a token's value only when the token is created or rotated.
49 Listing the tokens of a group service account requires GitLab 17.11 or later.
50 `),
51 Example: heredoc.Doc(`
52 # List the tokens of service account 42 of the current project
53 glab service-account token list 42
55 # List only the active tokens, finding the account by its username
56 glab service-account token list release-bot --active
58 # List the tokens of a group's service account
59 glab service-account token list 42 --group my-group
61 # Print the names of the active tokens
62 glab service-account token list 42 --active --output json --jq '.[].name'`),
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, args); err != nil {
68 return err
69 }
71 return opts.run(cmd.Context())
72 },
73 }
75 cmdutils.EnableRepoOverride(cmd, f)
77 fl := cmd.Flags()
78 fl.StringVarP(&opts.group, "group", "g", "", "List the tokens of a service account of a `group` instead of a project. Accepts a group ID or full path.")
79 fl.BoolVarP(&opts.active, "active", "a", false, "List only the active tokens.")
81 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
82 cmd.MarkFlagsMutuallyExclusive("repo", "group")
84 return cmd
85}
87func (o *options) complete(cmd *cobra.Command, args []string) error {
88 o.account = args[0]
90 group, err := cmdutils.GroupOverride(cmd)
91 if err != nil {
92 return err
93 }
94 o.group = group
96 return nil
97}
99func (o *options) run(ctx context.Context) error {
100 client, scope, err := serviceaccountutils.NewClient(o.baseRepo, o.apiClient, o.group)
101 if err != nil {
102 return err
103 }
105 accountID, err := serviceaccountutils.ResolveID(ctx, client, scope, o.account)
106 if err != nil {
107 return cmdutils.WrapError(err, "failed to list service account tokens")
108 }
110 tokens, err := serviceaccountutils.ListTokens(ctx, client, scope, accountID, o.active)
111 if err != nil {
112 return cmdutils.WrapError(err, "failed to list service account tokens")
113 }
115 if o.outputFormat == "json" {
116 return o.io.PrintJSON(tokens)
117 }
119 o.printTable(tokens, accountID)
120 return nil
121}
123func (o *options) printTable(tokens []*gitlab.PersonalAccessToken, accountID int64) {
124 if len(tokens) == 0 {
125 o.io.LogInfof("No tokens found for service account %d.\n", accountID)
126 return
127 }
129 c := o.io.Color()
130 table := tableprinter.NewTablePrinter()
131 table.AddRow(c.Bold("ID"), c.Bold("Name"), c.Bold("Scopes"), c.Bold("Active"), c.Bold("Expires At"), c.Bold("Last Used At"))
132 for _, t := range tokens {
133 table.AddRow(
134 t.ID,
135 t.Name,
136 serviceaccountutils.FormatScopes(t.Scopes),
137 t.Active,
138 serviceaccountutils.FormatDate(t.ExpiresAt),
139 serviceaccountutils.FormatTime(t.LastUsedAt),
140 )
141 }
142 o.io.LogInfof("%s", table.Render())
143}
Diagnostics9 git calls · 231 ms
CallStatusSizeTime0231 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 · hit3 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=3 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
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
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:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=3 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:0200322 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=322 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=ba2caccf6c55211c6b29b98f40c2358479d91b1f 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=322 git.pkt.payload.max=295 git.sideband.band1.bytes=295 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=295 git.pack.trailer=3488e558d4708096e879feba968e084c249dbe22Open pack map
fetchtree:0200223 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=223 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=41d72d07155f99e3acc572f8fe5ad26c802e4ddc 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=223 git.pkt.payload.max=196 git.sideband.band1.bytes=196 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=196 git.pack.trailer=d9c58aac50f427eb21501a81fe28c81c7c49ae44Open pack map
fetchtree:0200133 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=133 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=fef2397ba5f10896709e0e77f067b17e2bde060b 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=133 git.pkt.payload.max=106 git.sideband.band1.bytes=106 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=106 git.pack.trailer=81b37a8b84ced73eebbc562db73f4e42213075adOpen pack map
fetch2001.6 KiB212 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=212 git.response.bytes=1623 git.response.cache=MISS git.response.duration_ms=212 git.fetch.wants=b638431f47cb92584201db84654af1fd19bb206c 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=1623 git.pkt.payload.max=1596 git.sideband.band1.bytes=1596 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
- a5e3292f681a4628
- Request
- fb69d88e…
- 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