GitLabgitlab-org
cli
main
1package list
3import (
4 "context"
5 "fmt"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/commands/container_registry/registryutils"
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/utils"
18)
20type options struct {
21 group string
22 includeTags bool
23 includeTagDetails bool
24 includeTagsCount bool
25 page int
26 perPage int
27 outputFormat string
29 io *iostreams.IOStreams
30 gitlabClient func() (*gitlab.Client, error)
31 baseRepo func() (glrepo.Interface, error)
32}
34func NewCmd(f cmdutils.Factory) *cobra.Command {
35 opts := &options{
36 io: f.IO(),
37 gitlabClient: f.GitLabClient,
38 baseRepo: f.BaseRepo,
39 }
41 cmd := &cobra.Command{
42 Use: "list [flags]",
43 Short: "List container registry repositories.",
44 Long: heredoc.Docf(`
45 By default, repositories are listed for the current project. Use %[1]s--repo%[1]s
46 to target another project, or %[1]s--group%[1]s to list repositories for a group.
47 `, "`"),
48 Aliases: []string{"ls"},
49 Args: cobra.NoArgs,
50 ValidArgsFunction: cobra.NoFileCompletions,
51 Example: heredoc.Doc(`
52 # List container registry repositories for the current project
53 glab container-registry repository list
55 # List container registry repositories for another project
56 glab container-registry repository list -R gitlab-org/cli
58 # List container registry repositories for a group
59 glab container-registry repository list --group gitlab-org`),
60 Annotations: map[string]string{
61 mcpannotations.Safe: "true",
62 },
63 RunE: func(cmd *cobra.Command, args []string) error {
64 if err := opts.complete(cmd); err != nil {
65 return err
66 }
68 if err := opts.validate(); err != nil {
69 return err
70 }
72 return opts.run(cmd.Context())
73 },
74 }
76 cmdutils.EnableRepoOverride(cmd, f)
77 cmd.PersistentFlags().StringP("group", "g", "", "List container registry repositories for a group.")
79 fl := cmd.Flags()
80 fl.BoolVar(&opts.includeTags, "include-tags", false, "Include tags in the response. Project repositories only.")
81 fl.BoolVar(&opts.includeTagDetails, "include-tag-details", false, "Fetch digest, size, and creation time for included tags. Makes one API call per tag. Project JSON output only. Implies --include-tags.")
82 fl.BoolVar(&opts.includeTagsCount, "include-tags-count", true, "Include the number of tags in the response. Project repositories only.")
83 fl.IntVarP(&opts.page, "page", "p", 1, "Page number.")
84 fl.IntVarP(&opts.perPage, "per-page", "P", 30, "Number of items to list per page.")
85 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
87 return cmd
88}
90func (o *options) complete(cmd *cobra.Command) error {
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) validate() error {
101 if o.group != "" && o.includeTagDetails {
102 return &cmdutils.FlagError{Err: fmt.Errorf("--include-tag-details is only available for project repositories")}
103 }
104 if o.includeTagDetails && o.outputFormat != "json" {
105 return &cmdutils.FlagError{Err: fmt.Errorf("--include-tag-details requires --output json")}
106 }
108 return nil
109}
111func (o *options) run(ctx context.Context) error {
112 client, err := o.gitlabClient()
113 if err != nil {
114 return err
115 }
117 listOpts := gitlab.ListOptions{
118 Page: int64(o.page),
119 PerPage: int64(o.perPage),
120 }
122 var repoName string
123 var repositories []*gitlab.RegistryRepository
124 var resp *gitlab.Response
126 if o.group != "" {
127 repoName = o.group
128 repositories, resp, err = client.ContainerRegistry.ListGroupRegistryRepositories(
129 o.group,
130 &gitlab.ListGroupRegistryRepositoriesOptions{ListOptions: listOpts},
131 )
132 } else {
133 repo, repoErr := o.baseRepo()
134 if repoErr != nil {
135 return repoErr
136 }
137 repoName = repo.FullName()
138 opts := &gitlab.ListProjectRegistryRepositoriesOptions{
139 ListOptions: listOpts,
140 }
141 if o.includeTags || o.includeTagDetails {
142 opts.Tags = new(true)
143 }
144 if o.includeTagsCount {
145 opts.TagsCount = new(true)
146 }
147 repositories, resp, err = client.ContainerRegistry.ListProjectRegistryRepositories(
148 repo.FullName(),
149 opts,
150 )
151 }
152 if err != nil {
153 if o.group == "" {
154 return cmdutils.WrapError(err, fmt.Sprintf("failed to list container registry repositories from %s; ensure %s exists and has container registry enabled, or specify the owning project with -R <project>.", repoName, repoName))
155 }
156 return err
157 }
159 if o.outputFormat == "json" {
160 showTagsCount := o.group == "" && o.includeTagsCount
161 if o.includeTagDetails {
162 if err := o.fetchRepositoryTagDetails(ctx, client, repoName, repositories); err != nil {
163 return err
164 }
165 }
167 return o.io.PrintJSON(registryutils.NewRepositoryJSONList(repositories, o.includeTagDetails, showTagsCount))
168 }
170 title := utils.NewListTitle("container registry repository")
171 title.RepoName = repoName
172 title.Page = o.page
173 title.CurrentPageTotal = len(repositories)
174 title.EmptyMessage = fmt.Sprintf("No container registry repositories available on %s.", repoName)
175 if resp != nil {
176 title.Total = int(resp.TotalItems)
177 }
179 o.io.LogInfof("%s\n", title.Describe())
180 if len(repositories) > 0 {
181 o.io.LogInfof("%s\n", registryutils.DisplayRepositories(o.io, repositories, o.group == "" && o.includeTagsCount))
182 }
184 return nil
185}
187func (o *options) fetchRepositoryTagDetails(ctx context.Context, client *gitlab.Client, repoName string, repositories []*gitlab.RegistryRepository) error {
188 for _, repository := range repositories {
189 detailedTags := make([]*gitlab.RegistryRepositoryTag, 0, len(repository.Tags))
190 for _, tag := range repository.Tags {
191 if err := ctx.Err(); err != nil {
192 return err
193 }
194 detailedTag, _, err := client.ContainerRegistry.GetRegistryRepositoryTagDetail(
195 repoName,
196 repository.ID,
197 tag.Name,
198 )
199 if err != nil {
200 return cmdutils.WrapError(err, fmt.Sprintf("failed to fetch container registry tag %q from repository %d.", tag.Name, repository.ID))
201 }
202 detailedTags = append(detailedTags, detailedTag)
203 }
204 repository.Tags = detailedTags
205 }
207 return nil
208}
Diagnostics9 git calls · 43 ms
CallStatusSizeTime043 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 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 · 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=32926 git.response.cache=HIT git.response.duration_ms=4 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=4 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=4 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:0200243 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=243 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=a517df7bd3c1e8ee5cd02bc33418664d7dab246f 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=243 git.pkt.payload.max=216 git.sideband.band1.bytes=216 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=216 git.pack.trailer=50fe37a7b0ca9579b3ab7da0b785d0006b143110Open pack map
fetchtree:0200232 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=232 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=30b8a2dae1c295021612c17ef58cff022e6e1e20 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=232 git.pkt.payload.max=205 git.sideband.band1.bytes=205 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=205 git.pack.trailer=ccde92ca11c0d35d0efab2d7a5815798e7ee4f3fOpen pack map
fetchtree:0200135 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=135 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=d8f0a91cbefe0365725132fbe71e21d54dc35611 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=135 git.pkt.payload.max=108 git.sideband.band1.bytes=108 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=108 git.pack.trailer=34cb9131ea495de5116757226fe337233cfecaffOpen pack map
fetch2002.1 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=23 git.response.bytes=2120 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=1ed80659946ae6695cd155a10cd7e4d65a76fec5 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=2120 git.pkt.payload.max=2093 git.sideband.band1.bytes=2093 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
- a5e2ea60ca4f4bc2
- Request
- 51aedbd2…
- 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