GitLabgitlab-org
cli
main
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/deploy-token/display"
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 tokenID int64
26 group string
27 outputFormat string
28}
30func NewCmd(f cmdutils.Factory) *cobra.Command {
31 opts := &options{
32 io: f.IO(),
33 apiClient: f.ApiClient,
34 baseRepo: f.BaseRepo,
35 }
37 cmd := &cobra.Command{
38 Use: "get <token-id> [flags]",
39 Short: "Get a deploy token.",
40 Args: cobra.ExactArgs(1),
41 ValidArgsFunction: cobra.NoFileCompletions,
42 Long: heredoc.Docf(`
43 Get a single deploy token of a project or a group, by its ID.
45 The output never contains the token value. A token value is shown only once, when the token is created.
47 Token IDs come from %[1]sglab deploy-token list%[1]s.
48 `, "`"),
49 Example: heredoc.Doc(`
50 # Get deploy token 42 of the current project
51 glab deploy-token get 42
53 # Get a deploy token of a specific project
54 glab deploy-token get 42 --repo user/my-repo
56 # Get a group's deploy token
57 glab deploy-token get 42 --group group/sub-group
59 # Get a deploy token as JSON
60 glab deploy-token get 42 --output json`),
61 Annotations: map[string]string{
62 mcpannotations.Safe: "true",
63 },
64 RunE: func(cmd *cobra.Command, args []string) error {
65 if err := opts.complete(cmd, args); err != nil {
66 return err
67 }
69 return opts.run(cmd.Context())
70 },
71 }
73 cmdutils.EnableRepoOverride(cmd, f)
75 fl := cmd.Flags()
76 fl.StringVarP(&opts.group, "group", "g", "", "Get a deploy token of the group <group> instead of a project.")
78 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
79 cmd.MarkFlagsMutuallyExclusive("repo", "group")
81 return cmd
82}
84func (o *options) complete(cmd *cobra.Command, args []string) error {
85 tokenID, err := cmdutils.ParseID(args[0], "deploy token ID")
86 if err != nil {
87 return err
88 }
89 o.tokenID = tokenID
91 group, err := cmdutils.GroupOverride(cmd)
92 if err != nil {
93 return err
94 }
95 o.group = group
97 return nil
98}
100func (o *options) run(ctx context.Context) error {
101 // The group scope works outside a repository, so a missing base repo is only
102 // fatal for the project scope. Fail before building a client that would be
103 // discarded, so the caller sees why the repository was needed.
104 repo, repoErr := o.baseRepo()
105 if o.group == "" && repoErr != nil {
106 return repoErr
107 }
109 var repoHost string
110 if repoErr == nil {
111 repoHost = repo.RepoHost()
112 }
114 apiClient, err := o.apiClient(repoHost)
115 if err != nil {
116 return err
117 }
118 client := apiClient.Lab()
120 var token *gitlab.DeployToken
121 switch {
122 case o.group != "":
123 token, _, err = client.DeployTokens.GetGroupDeployToken(o.group, o.tokenID, gitlab.WithContext(ctx))
124 default:
125 token, _, err = client.DeployTokens.GetProjectDeployToken(repo.FullName(), o.tokenID, gitlab.WithContext(ctx))
126 }
127 if err != nil {
128 return cmdutils.WrapError(err, "failed to get deploy token")
129 }
131 // JSON output prints the response as-is, so the token value is cleared here
132 // instead of relying on the endpoint never returning it.
133 token.Token = ""
135 switch o.outputFormat {
136 case "json":
137 return o.io.PrintJSON(token)
138 default:
139 return o.printDetails(token)
140 }
141}
143func (o *options) printDetails(token *gitlab.DeployToken) error {
144 c := o.io.Color()
145 table := tableprinter.NewTablePrinter()
146 table.AddRow(c.Bold("ID"), token.ID)
147 table.AddRow(c.Bold("Name"), token.Name)
148 table.AddRow(c.Bold("Username"), token.Username)
149 table.AddRow(c.Bold("Scopes"), display.Scopes(token.Scopes))
150 table.AddRow(c.Bold("Expires At"), display.ExpiresAt(token.ExpiresAt))
151 table.AddRow(c.Bold("Revoked"), token.Revoked)
152 table.AddRow(c.Bold("Expired"), token.Expired)
153 o.io.LogInfof("%s", table.Render())
155 return nil
156}
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=ls-refs 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 · 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=3 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
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 · hit0 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=0 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=0 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:0200263 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=263 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=aff3dd5052d75f0afe2383713d669f993c82b2f9 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=263 git.pkt.payload.max=236 git.sideband.band1.bytes=236 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=236 git.pack.trailer=4076aed8a003d312c41b730611843784ab33181fOpen pack map
fetchtree:0200134 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=134 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=39d484dcf7951935ac324634c52e6f75cdf36a31 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=9d747441ee037484d9a04a619fe68ebec4f0273dOpen pack map
fetch2001.7 KiB219 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=219 git.response.bytes=1712 git.response.cache=MISS git.response.duration_ms=219 git.fetch.wants=d02bad990d072d1cbbf1545e1d64a940f9a6c5bb 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=1712 git.pkt.payload.max=1685 git.sideband.band1.bytes=1685 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
- a5e2c7771e884afc
- Request
- 3dc94248…
- 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