GitLabgitlab-org

cli

main
ply8 git calls309 ms100 KiB5 cachedprotocol v2
234 lines7.1 KiBRaw
1package delete
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
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/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/commands/packages/helpers"
16 "gitlab.com/gitlab-org/cli/internal/glrepo"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
21type options struct {
22 forceDelete bool
23 packageID int64
24 packageName string
25 packageVersion string
26 packageType string
28 io *iostreams.IOStreams
29 gitlabClient func() (*gitlab.Client, error)
30 baseRepo func() (glrepo.Interface, error)
33func NewCmd(f cmdutils.Factory) *cobra.Command {
34 return newCmd(f, "delete [<id> | --id <id> | --name <name>] [flags]", []string{"del"}, "")
37// NewRmCmd returns the deprecated `rm` spelling of delete, kept so existing
38// scripts keep working. Cobra prints the deprecation notice on every
39// invocation; the command is hidden from help (including fang's renderer,
40// which unlike cobra does not hide Deprecated commands), completion, and
41// generated docs, and excluded from MCP tool registration, so the alias is
42// no longer advertised anywhere.
43//
44// Cobra has no way to deprecate an alias, so this twin command is how the
45// `rm` spelling keeps working while `delete` advertises `del` alone.
46func NewRmCmd(f cmdutils.Factory) *cobra.Command {
47 return newCmd(f, "rm [<id> | --id <id> | --name <name>] [flags]", nil, "use `glab packages del` instead.")
50func newCmd(f cmdutils.Factory, use string, aliases []string, deprecated string) *cobra.Command {
51 opts := &options{
52 io: f.IO(),
53 gitlabClient: f.GitLabClient,
54 baseRepo: f.BaseRepo,
55 }
56 annotations := map[string]string{
57 mcpannotations.Destructive: "true",
58 }
59 if deprecated != "" {
60 // MCP tool registration walks every command regardless of
61 // Hidden/Deprecated, so a deprecated spelling must opt out explicitly
62 // or it surfaces as a duplicate tool next to the real command.
63 annotations[mcpannotations.Exclude] = "true"
64 }
65 cmd := &cobra.Command{
66 Use: use,
67 Short: `Delete a package from a project's package registry.`,
68 Long: heredoc.Docf(`
69 Identify the package by its numeric ID (as a positional argument or with
70 %[1]s--id%[1]s), or by %[1]s--name%[1]s and %[1]s--version%[1]s. When using %[1]s--name%[1]s,
71 %[1]s--package-type%[1]s defaults to %[1]sgeneric%[1]s.
73 The command asks for confirmation before deleting; use %[1]s-y%[1]s to skip
74 the prompt in scripts.
76 By default, the package is removed from the current project. Use %[1]s--repo%[1]s
77 to target another project.
78 `, "`"),
79 Aliases: aliases,
80 Deprecated: deprecated,
81 Hidden: deprecated != "",
82 Args: cobra.MaximumNArgs(1),
83 Example: heredoc.Doc(`
84 # Delete a package by ID
85 glab packages delete 1
86 glab packages delete --id 1
88 # Delete a generic package by name and version
89 glab packages delete --name example-generic --version 1.0.0
91 # Delete a package by name, version and type
92 glab packages delete --name example-maven --version 1.0.0 --package-type maven
94 # Skip the confirmation prompt
95 glab packages delete 1 -y
96 glab packages delete --name example-generic --version 1.0.0 --yes
97 `),
98 Annotations: annotations,
99 RunE: func(cmd *cobra.Command, args []string) error {
100 if err := opts.complete(cmd, args); err != nil {
101 return err
102 }
104 if err := opts.validate(); err != nil {
105 return err
106 }
108 return opts.run(cmd.Context())
109 },
110 }
112 cmd.Flags().BoolVarP(&opts.forceDelete, "yes", "y", false, "Skip the confirmation prompt.")
113 cmd.Flags().Int64("id", 0, "ID of the package to remove.")
114 cmd.Flags().String("name", "", "Name of the package to remove.")
115 cmd.Flags().String("version", "", "Version of the package.")
117 cmd.Flags().Var(
118 cmdutils.NewEnumValue(helpers.PackageTypes, helpers.DefaultPackageType, &opts.packageType),
119 "package-type",
120 fmt.Sprintf("Type of the package to remove, used with --name. One of: %s.", strings.Join(helpers.PackageTypes, ", ")),
121 )
123 cmd.MarkFlagsMutuallyExclusive("id", "name")
124 cmd.MarkFlagsRequiredTogether("name", "version")
126 return cmd
129func (o *options) complete(cmd *cobra.Command, args []string) error {
130 // A positional ID and the --id/--name flags select the package in conflicting
131 // ways. Rejecting the combination avoids silently ignoring the argument and
132 // removing a different package than the one named on the command line.
133 if len(args) > 0 && (cmd.Flags().Changed("id") || cmd.Flags().Changed("name")) {
134 return &cmdutils.FlagError{
135 Err: fmt.Errorf("the package ID argument %q cannot be combined with --id or --name", args[0]),
136 }
137 }
139 name, err := cmd.Flags().GetString("name")
140 if err != nil {
141 return fmt.Errorf("unable to get name flag: %w", err)
142 }
144 version, err := cmd.Flags().GetString("version")
145 if err != nil {
146 return fmt.Errorf("unable to get version flag: %w", err)
147 }
149 if name != "" {
150 o.packageName = name
151 o.packageVersion = version
153 return nil
154 }
156 if cmd.Flags().Changed("package-type") {
157 return &cmdutils.FlagError{Err: fmt.Errorf("--package-type can only be used with --name")}
158 }
160 if cmd.Flags().Changed("id") {
161 o.packageID, err = cmd.Flags().GetInt64("id")
162 if err != nil {
163 return fmt.Errorf("unable to get id flag: %w", err)
164 }
165 } else {
166 if len(args) == 0 {
167 return &cmdutils.FlagError{Err: fmt.Errorf("provide a package ID argument, or the --id or --name flag")}
168 }
169 o.packageID, err = strconv.ParseInt(args[0], 10, 64)
170 if err != nil {
171 return fmt.Errorf("package ID must be an integer: %s", args[0])
172 }
173 }
175 return nil
178func (o *options) validate() error {
179 if !o.forceDelete && !o.io.PromptEnabled() {
180 return &cmdutils.FlagError{Err: fmt.Errorf("--yes or -y flag is required when not running interactively")}
181 }
183 return nil
186func (o *options) run(ctx context.Context) error {
187 client, err := o.gitlabClient()
188 if err != nil {
189 return err
190 }
192 repo, err := o.baseRepo()
193 if err != nil {
194 return err
195 }
197 target := fmt.Sprintf("package %d", o.packageID)
198 if o.packageName != "" {
199 pkg, err := helpers.GetPackageByName(ctx, client, o.packageName, o.packageType, o.packageVersion, repo.FullName())
200 if err != nil {
201 return err
202 }
204 o.packageID = pkg.ID
205 target = fmt.Sprintf("package %d (%s %s, type %s)", pkg.ID, pkg.Name, pkg.Version, pkg.PackageType)
206 }
208 confirmed := o.forceDelete
209 if !confirmed && o.io.PromptEnabled() {
210 o.io.LogInfof("This action will permanently delete %s immediately.\n\n", target)
211 if err := o.io.Confirm(ctx, &confirmed, fmt.Sprintf("Are you ABSOLUTELY SURE you wish to delete this %s?", target)); err != nil {
212 return cmdutils.WrapError(err, "could not prompt")
213 }
214 }
216 if !confirmed {
217 return cmdutils.CancelError()
218 }
220 color := o.io.Color()
221 o.io.LogInfof("%s Deleting package %s=%s %s=%d\n",
222 color.ProgressIcon(),
223 color.Blue("repo"), repo.FullName(),
224 color.Blue("packageID"), o.packageID)
226 _, err = client.Packages.DeleteProjectPackage(repo.FullName(), o.packageID, gitlab.WithContext(ctx))
227 if err != nil {
228 return fmt.Errorf("failed to delete package: %w", err)
229 }
231 o.io.LogInfof(color.Bold("%s Package %d deleted.\n"), color.RedCheck(), o.packageID)
233 return nil
Diagnostics8 git calls · 309 ms
CallStatusSizeTime0309 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 KiB3 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=UPDATING
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 · 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: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=3
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: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=4445ce4c6a6710742ed094287b245091ae79204f
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=4126ef8ff4c560b52ccdd91c9d0a185a77d21c2d
Open pack map
fetchtree:0200168 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=168
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=9d92f1843d9a5bf444d8a7076b49e4947aad98b4
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=168
git.pkt.payload.max=141
git.sideband.band1.bytes=141
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=141
git.pack.trailer=f11d7cf506552a78e18692d286e9d7854ebb7fb6
Open pack map
fetch2002.7 KiB294 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=294
git.response.bytes=2737
git.response.cache=MISS
git.response.duration_ms=294
git.fetch.wants=fdce95c2fbee9c6a1d97c011fd0d9246acca32a4
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=2737
git.pkt.payload.max=2710
git.sideband.band1.bytes=2710
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
a5e296356f8e2407
Request
99e989a5…
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