GitLabgitlab-org

cli

main
ply8 git calls229 ms99 KiB5 cachedprotocol v2
140 lines3.9 KiBRaw
1package get
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"
19type options struct {
20 io *iostreams.IOStreams
21 apiClient func(repoHost string) (*api.Client, error)
22 baseRepo func() (glrepo.Interface, error)
24 account string
25 group string
26 outputFormat string
29func NewCmd(f cmdutils.Factory) *cobra.Command {
30 opts := &options{
31 io: f.IO(),
32 apiClient: f.ApiClient,
33 baseRepo: f.BaseRepo,
34 }
36 cmd := &cobra.Command{
37 Use: "get <id | username> [flags]",
38 Short: "Get a service account.",
39 Args: cobra.ExactArgs(1),
40 ValidArgsFunction: cobra.NoFileCompletions,
41 Long: heredoc.Docf(`
42 Get a single service account of a project or a group, by its ID or username.
44 A numeric argument is always treated as an ID. IDs and usernames come from %[1]sglab service-account list%[1]s.
46 Getting a service account by ID requires GitLab 19.4 or later.
47 `, "`"),
48 Example: heredoc.Doc(`
49 # Get service account 42 of the current project
50 glab service-account get 42
52 # Get a service account by its username
53 glab service-account get service_account_project_123_abc
55 # Get a service account of a specific project
56 glab service-account get 42 --repo user/my-repo
58 # Get a group's service account
59 glab service-account get 42 --group my-group
61 # Print the service account's email address
62 glab service-account get 42 --output json --jq '.email'`),
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", "", "Get a service account of a `group` instead of a project. Accepts a group ID or full path.")
80 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
81 cmd.MarkFlagsMutuallyExclusive("repo", "group")
83 return cmd
86func (o *options) complete(cmd *cobra.Command, args []string) error {
87 o.account = args[0]
89 group, err := cmdutils.GroupOverride(cmd)
90 if err != nil {
91 return err
92 }
93 o.group = group
95 return nil
98func (o *options) run(ctx context.Context) error {
99 client, scope, err := serviceaccountutils.NewClient(o.baseRepo, o.apiClient, o.group)
100 if err != nil {
101 return err
102 }
104 id, isID, err := serviceaccountutils.ParseID(o.account)
105 if err != nil {
106 return cmdutils.WrapError(err, "failed to get service account")
107 }
109 var raw any
110 var account serviceaccountutils.Account
111 switch {
112 // The list item carries the same fields as the single-account endpoint,
113 // which only exists from GitLab 19.4.
114 case !isID:
115 raw, account, err = serviceaccountutils.FindByUsername(ctx, client, scope, o.account)
116 if err != nil {
117 return cmdutils.WrapError(err, "failed to get service account")
118 }
119 case scope.Group != "":
120 a, _, err := client.Groups.GetServiceAccount(scope.Group, id, gitlab.WithContext(ctx))
121 if err != nil {
122 return cmdutils.WrapError(err, "failed to get service account")
123 }
124 raw, account = a, serviceaccountutils.FromGroup(a)
125 default:
126 a, _, err := client.Projects.GetProjectServiceAccount(scope.Project, id, gitlab.WithContext(ctx))
127 if err != nil {
128 return cmdutils.WrapError(err, "failed to get service account")
129 }
130 raw, account = a, serviceaccountutils.FromProject(a)
131 }
133 switch o.outputFormat {
134 case "json":
135 return o.io.PrintJSON(raw)
136 default:
137 serviceaccountutils.PrintDetails(o.io, account)
138 return nil
139 }
Diagnostics8 git calls · 229 ms
CallStatusSizeTime0229 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 KiB2 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=UPDATING
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
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=3
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=44
Open 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=4
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 · hit4 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=4
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: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=3
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=3488e558d4708096e879feba968e084c249dbe22
Open pack map
fetchtree:0200135 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=214
git.response.bytes=135
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=7fe485110b597246350bc7da56b52aa930443045
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=717f7347ad51127f9ca63f2909580355a72479bb
Open pack map
fetch2001.5 KiB214 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1521
git.response.cache=MISS
git.response.duration_ms=214
git.fetch.wants=30315ec9e5f6bdc9d739bd4adde0e43263d1bb1b
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=1521
git.pkt.payload.max=1494
git.sideband.band1.bytes=1494
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
a5e30930ea4c15db
Request
d613e39a…
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