GitLabgitlab-org

cli

main
ply8 git calls236 ms99 KiB6 cachedprotocol v2
167 lines4.0 KiBRaw
1package get
3import (
4 "context"
5 "errors"
6 "fmt"
8 "charm.land/huh/v2"
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/api"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/commands/ssh-key/sshkeyutils"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19 "gitlab.com/gitlab-org/cli/internal/utils"
22type options struct {
23 apiClient func(repoHost string) (*api.Client, error)
24 io *iostreams.IOStreams
26 keyID int64
27 perPage int
28 page int
29 outputFormat string
32func NewCmdGet(f cmdutils.Factory) *cobra.Command {
33 opts := &options{
34 io: f.IO(),
35 apiClient: f.ApiClient,
36 }
37 cmd := &cobra.Command{
38 Use: "get [<key-id>]",
39 Short: "Returns a single SSH key specified by the ID.",
40 Long: heredoc.Docf(`Pass the key ID as an argument, or run the command without arguments
41 to select a key interactively. Find key IDs by running %[1]sglab ssh-key list --show-id%[1]s.
43 By default, the command prints the public key. Use %[1]s--output json%[1]s to return
44 the full key object.
45 `, "`"),
46 Example: heredoc.Doc(`
47 # Get ssh key with ID as argument
48 glab ssh-key get 7750633
50 # Interactive
51 glab ssh-key get
53 # Interactive, with pagination
54 glab ssh-key get -P 50 -p 2`),
55 Args: cobra.MaximumNArgs(1),
56 ValidArgsFunction: func(cmd *cobra.Command, args []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
57 if len(args) != 0 {
58 return nil, cobra.ShellCompDirectiveNoFileComp
59 }
60 return sshkeyutils.SSHKeyIDCompletions(cmd.Context(), f)
61 },
62 Annotations: map[string]string{
63 mcpannotations.Safe: "true",
64 },
65 RunE: func(cmd *cobra.Command, args []string) error {
66 if err := opts.complete(cmd.Context(), args); err != nil {
67 return err
68 }
70 return opts.run()
71 },
72 }
74 cmd.Flags().IntVarP(&opts.page, "page", "p", 1, "Page number.")
75 cmd.Flags().IntVarP(&opts.perPage, "per-page", "P", 20, "Number of items to list per page.")
76 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
78 return cmd
81func (o *options) complete(ctx context.Context, args []string) error {
82 if len(args) == 0 {
83 keyID, err := keySelectPrompt(ctx, o)
84 if err != nil {
85 return err
86 }
87 o.keyID = keyID
88 }
90 if len(args) == 1 {
91 o.keyID = int64(utils.StringToInt(args[0]))
92 }
94 return nil
97func (o *options) run() error {
98 c, err := o.apiClient("")
99 if err != nil {
100 return err
101 }
102 client := c.Lab()
104 key, _, err := client.Users.GetSSHKey(o.keyID)
105 if err != nil {
106 return cmdutils.WrapError(err, "getting SSH key.")
107 }
109 if o.outputFormat == "json" {
110 return o.io.PrintJSON(key)
111 }
113 o.io.LogInfo(key.Key)
115 return nil
118func keySelectPrompt(ctx context.Context, opts *options) (int64, error) {
119 if !opts.io.PromptEnabled() {
120 return 0, cmdutils.FlagError{Err: errors.New("the <key-id> argument is required when prompts are disabled")}
121 }
123 sshKeyListOptions := &gitlab.ListSSHKeysOptions{
124 ListOptions: gitlab.ListOptions{
125 PerPage: int64(opts.perPage),
126 Page: int64(opts.page),
127 },
128 }
130 c, err := opts.apiClient("")
131 if err != nil {
132 return 0, err
133 }
134 client := c.Lab()
136 keys, response, err := client.Users.ListSSHKeys(sshKeyListOptions)
137 if err != nil {
138 return 0, cmdutils.WrapError(err, "Retrieving list of SSH keys to prompt with.")
139 }
140 if len(keys) == 0 {
141 return 0, cmdutils.WrapError(errors.New("no keys were found"), "Retrieving list of SSH keys.")
142 }
144 options := make([]huh.Option[int64], 0, len(keys))
145 for _, key := range keys {
146 options = append(options, huh.NewOption(key.Title, key.ID))
147 }
149 var selectedID int64
150 selector := huh.NewSelect[int64]().
151 Title(fmt.Sprintf(
152 "Select key - Showing %d/%d keys - page %d/%d",
153 len(keys),
154 response.TotalItems,
155 opts.page,
156 response.TotalPages,
157 )).
158 Options(options...).
159 Value(&selectedID)
161 err = opts.io.Run(ctx, selector)
162 if err != nil {
163 return 0, cmdutils.WrapError(err, "prompting for SSH key to delete.")
164 }
166 return selectedID, nil
Diagnostics8 git calls · 236 ms
CallStatusSizeTime0236 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=3
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 · 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:0200289 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=289
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=1dd4ebf804b44e69b594244d57246af8b61f1ed3
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=289
git.pkt.payload.max=262
git.sideband.band1.bytes=262
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=262
git.pack.trailer=d2a3ec46393f5a5492eae3096186e74c059a8aaf
Open 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=bfada6d29eea3c4cda52d75e0786e05950aa9b79
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=1ed43afa126e6a4c927eb4b89b2cd96be60f484c
Open pack map
fetch2001.7 KiB224 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=224
git.response.bytes=1730
git.response.cache=MISS
git.response.duration_ms=224
git.fetch.wants=59f4389d59f9049fb12bcbe953fc8c0a848e34c0
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=1730
git.pkt.payload.max=1703
git.sideband.band1.bytes=1703
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
a5e2c6df7cde4afc
Request
ba58759e…
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