GitLabgitlab-org
cli
main
1package list
3import (
4 "context"
5 "fmt"
6 "strings"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 "gitlab.com/gitlab-org/cli/internal/api"
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"
19)
21type options struct {
22 io *iostreams.IOStreams
23 gitlabClient func() (*gitlab.Client, error)
24 baseRepo func() (glrepo.Interface, error)
26 packageType string
27 listOptions *gitlab.ListProjectPackagesOptions
28}
30func NewCmd(f cmdutils.Factory) *cobra.Command {
31 opts := &options{
32 io: f.IO(),
33 gitlabClient: f.GitLabClient,
34 baseRepo: f.BaseRepo,
35 }
36 cmd := &cobra.Command{
37 Use: "list [flags]",
38 Short: `List packages in a project's package registry.`,
39 Long: heredoc.Docf(`
40 Packages of all types (generic, npm, maven, etc.) are returned. Use
41 %[1]s--package-type%[1]s to filter by type and %[1]s--name%[1]s to filter by name. Use
42 %[1]s--page%[1]s and %[1]s--per-page%[1]s to paginate the result.
44 By default, packages are listed for the current project. Use %[1]s--repo%[1]s
45 to target another project.
46 `, "`"),
47 Aliases: []string{"ls"},
48 Example: heredoc.Doc(`
49 # List all packages in the current project
50 glab packages list
52 # Use the 'ls' alias
53 glab packages ls
55 # Filter by package name
56 glab packages list --name my-package
58 # List a specific page with a custom page size
59 glab packages list --page 2 --per-page 10
61 # List packages from another project
62 glab packages list -R owner/repo
63 `),
64 Args: cobra.NoArgs,
65 ValidArgsFunction: cobra.NoFileCompletions,
66 Annotations: map[string]string{
67 mcpannotations.Safe: "true",
68 },
69 RunE: func(cmd *cobra.Command, args []string) error {
70 if err := opts.complete(cmd); err != nil {
71 return err
72 }
74 return opts.run(cmd.Context())
75 },
76 }
78 fl := cmd.Flags()
79 fl.IntP("page", "p", 1, "Page number.")
80 fl.IntP("per-page", "P", int(api.DefaultListLimit), "Number of items to list per page.")
81 fl.StringP("name", "n", "", "Filter packages by name (substring match).")
83 fl.Var(
84 cmdutils.NewEnumValue(helpers.PackageTypes, "", &opts.packageType),
85 "package-type",
86 fmt.Sprintf("Filter packages by type. One of: %s.", strings.Join(helpers.PackageTypes, ", ")),
87 )
89 cmdutils.AddJQFlag(cmd, f.IO())
90 return cmd
91}
93func (o *options) complete(cmd *cobra.Command) error {
94 o.listOptions = &gitlab.ListProjectPackagesOptions{
95 ListOptions: gitlab.ListOptions{
96 Page: 1,
97 PerPage: api.DefaultListLimit,
98 },
99 }
101 fl := cmd.Flags()
103 if fl.Changed("page") {
104 page, err := fl.GetInt("page")
105 if err != nil {
106 return err
107 }
108 o.listOptions.Page = int64(page)
109 }
111 if fl.Changed("per-page") {
112 perPage, err := fl.GetInt("per-page")
113 if err != nil {
114 return err
115 }
116 o.listOptions.PerPage = int64(perPage)
117 }
119 if fl.Changed("name") {
120 name, err := fl.GetString("name")
121 if err != nil {
122 return err
123 }
124 o.listOptions.PackageName = &name
125 }
127 if fl.Changed("package-type") {
128 o.listOptions.PackageType = &o.packageType
129 }
131 return nil
132}
134func (o *options) run(ctx context.Context) error {
135 client, err := o.gitlabClient()
136 if err != nil {
137 return err
138 }
140 repo, err := o.baseRepo()
141 if err != nil {
142 return err
143 }
145 packages, _, err := client.Packages.ListProjectPackages(repo.FullName(), o.listOptions, gitlab.WithContext(ctx))
146 if err != nil {
147 return fmt.Errorf("failed to list packages: %w", err)
148 }
150 return o.io.PrintJSON(packages)
151}
Diagnostics8 git calls · 238 ms
CallStatusSizeTime0238 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=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 · 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 · hit5 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=5 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=3 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=4 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=4126ef8ff4c560b52ccdd91c9d0a185a77d21c2dOpen pack map
fetchtree:0200133 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=223 git.response.bytes=133 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=b4375f454e137754558871ed6798d9aeaa29abf7 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=133 git.pkt.payload.max=106 git.sideband.band1.bytes=106 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=106 git.pack.trailer=e8a8faa6dc0a0f41e3220fe7927dc7dbaff886a9Open pack map
fetch2001.4 KiB223 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1477 git.response.cache=MISS git.response.duration_ms=223 git.fetch.wants=313c5e5418f5f14eee747274705e32f92e1cb512 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=1477 git.pkt.payload.max=1450 git.sideband.band1.bytes=1450 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
- a5e2cbec7f1e7929
- Request
- 16f45719…
- 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