GitLabgitlab-org
cli
main
1package delete
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"
20)
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}
31func NewCmdDelete(f cmdutils.Factory) *cobra.Command {
32 opts := &options{
33 io: f.IO(),
34 apiClient: f.ApiClient,
35 }
36 cmd := &cobra.Command{
37 Use: "delete [<key-id>]",
38 Short: "Deletes a single SSH key specified by the ID.",
39 Long: heredoc.Docf(`Pass the key ID as an argument, or run the command without arguments
40 to select a key interactively. Find key IDs by running %[1]sglab ssh-key list --show-id%[1]s.
42 This action is permanent and cannot be undone.
43 `, "`"),
44 Example: heredoc.Doc(`
45 # Delete SSH key with ID as argument
46 glab ssh-key delete 7750633
48 # Interactive
49 glab ssh-key delete
51 # Interactive, with pagination
52 glab ssh-key delete -P 50 -p 2`),
53 Args: cobra.MaximumNArgs(1),
54 ValidArgsFunction: func(cmd *cobra.Command, args []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
55 if len(args) != 0 {
56 return nil, cobra.ShellCompDirectiveNoFileComp
57 }
58 return sshkeyutils.SSHKeyIDCompletions(cmd.Context(), f)
59 },
60 Annotations: map[string]string{
61 mcpannotations.Destructive: "true",
62 },
63 RunE: func(cmd *cobra.Command, args []string) error {
64 if err := opts.complete(cmd.Context(), args); err != nil {
65 return err
66 }
67 return opts.run()
68 },
69 }
71 cmd.Flags().IntVarP(&opts.page, "page", "p", 1, "Page number.")
72 cmd.Flags().IntVarP(&opts.perPage, "per-page", "P", 30, "Number of items to list per page.")
74 return cmd
75}
77func (o *options) complete(ctx context.Context, args []string) error {
78 if len(args) == 0 {
79 keyID, err := keySelectPrompt(ctx, o)
80 if err != nil {
81 return err
82 }
83 o.keyID = keyID
84 }
86 if len(args) == 1 {
87 o.keyID = int64(utils.StringToInt(args[0]))
88 }
90 return nil
91}
93func (o *options) run() error {
94 c, err := o.apiClient("")
95 if err != nil {
96 return err
97 }
98 client := c.Lab()
100 _, err = client.Users.DeleteSSHKey(o.keyID)
101 if err != nil {
102 return cmdutils.WrapError(err, "deleting SSH key.")
103 }
105 if o.io.IsOutputTTY() {
106 cs := o.io.Color()
107 o.io.LogInfof("%s SSH key deleted.\n", cs.GreenCheck())
108 } else {
109 o.io.LogInfo("SSH key deleted.\n")
110 }
112 return nil
113}
115func keySelectPrompt(ctx context.Context, opts *options) (int64, error) {
116 if !opts.io.PromptEnabled() {
117 return 0, cmdutils.FlagError{Err: errors.New("the <key-id> argument is required when prompts are disabled")}
118 }
120 sshKeyListOptions := &gitlab.ListSSHKeysOptions{
121 ListOptions: gitlab.ListOptions{
122 PerPage: int64(opts.perPage),
123 Page: int64(opts.page),
124 },
125 }
127 c, err := opts.apiClient("")
128 if err != nil {
129 return 0, err
130 }
131 client := c.Lab()
133 keys, resp, err := client.Users.ListSSHKeys(sshKeyListOptions)
134 if err != nil {
135 return 0, cmdutils.WrapError(err, "Retrieving list of SSH keys.")
136 }
137 if len(keys) == 0 {
138 return 0, cmdutils.WrapError(errors.New("no keys found"), "Retrieving list of SSH keys.")
139 }
141 options := make([]huh.Option[int64], 0, len(keys))
142 for _, key := range keys {
143 options = append(options, huh.NewOption(key.Title, key.ID))
144 }
146 var selectedID int64
147 selector := huh.NewSelect[int64]().
148 Title(fmt.Sprintf(
149 "Select key to delete - Showing %d/%d keys - page %d/%d",
150 len(keys),
151 resp.TotalItems,
152 opts.page,
153 resp.TotalPages,
154 )).
155 Options(options...).
156 Value(&selectedID)
158 err = opts.io.Run(ctx, selector)
159 if err != nil {
160 return 0, cmdutils.WrapError(err, "prompting for SSH key to delete.")
161 }
163 return selectedID, nil
164}
Diagnostics8 git calls · 317 ms
CallStatusSizeTime0317 ms
discover200185 B5 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB5 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=5 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 · 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=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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 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 · hit5 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=1857 git.response.cache=HIT git.response.duration_ms=5 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:0200289 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=289 git.response.cache=HIT git.response.duration_ms=4 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=d2a3ec46393f5a5492eae3096186e74c059a8aafOpen pack map
fetchtree:0200138 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=295 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=13515d04f5763fac4ae8f4bd881dd087aa1e9368 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=138 git.pkt.payload.max=111 git.sideband.band1.bytes=111 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=111 git.pack.trailer=ccaf8c5a098822b834228ad82df07783df6d712bOpen pack map
fetch2001.6 KiB295 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1689 git.response.cache=MISS git.response.duration_ms=295 git.fetch.wants=fdb18a14453174d1e0604ad0774910d929120981 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=1689 git.pkt.payload.max=1662 git.sideband.band1.bytes=1662 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
- a5e284b7081c4af4
- Request
- 030548ec…
- 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