GitLabgitlab-org

cli

main
ply8 git calls211 ms99 KiB5 cachedprotocol v2
189 lines5.3 KiBRaw
1package list
3import (
4 "github.com/MakeNowJust/heredoc/v2"
5 "github.com/spf13/cobra"
7 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
9 "gitlab.com/gitlab-org/cli/internal/api"
10 "gitlab.com/gitlab-org/cli/internal/cmdutils"
11 "gitlab.com/gitlab-org/cli/internal/glrepo"
12 "gitlab.com/gitlab-org/cli/internal/iostreams"
13 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
14 "gitlab.com/gitlab-org/cli/internal/tableprinter"
17type options struct {
18 apiClient func(repoHost string) (*api.Client, error)
19 io *iostreams.IOStreams
20 baseRepo func() (glrepo.Interface, error)
21 page int
22 perPage int
24 group string
25 outputFormat string
26 instance bool
29func NewCmdList(f cmdutils.Factory, runE func(opts *options) error) *cobra.Command {
30 opts := &options{
31 io: f.IO(),
32 apiClient: f.ApiClient,
33 baseRepo: f.BaseRepo,
34 }
36 cmd := &cobra.Command{
37 Use: "list",
38 Short: "List variables for a project or group.",
39 Long: heredoc.Docf(`
40 Defaults to the current project. Use %[1]s--group%[1]s to list variables
41 for a group, or %[1]s--instance%[1]s to list instance variables.
42 `, "`"),
43 Aliases: []string{"ls"},
44 Args: cobra.NoArgs,
45 ValidArgsFunction: cobra.NoFileCompletions,
46 Example: heredoc.Doc(`
47 glab variable list
48 glab variable list -i
49 glab variable list --per-page 100 --page 1
50 glab variable list --group gitlab-org
51 glab variable list --group gitlab-org --per-page 100`),
52 Annotations: map[string]string{
53 mcpannotations.Exclude: "true",
54 },
55 RunE: func(cmd *cobra.Command, args []string) error {
56 if err := opts.complete(cmd); err != nil {
57 return err
58 }
60 if runE != nil {
61 return runE(opts)
62 }
64 return opts.run()
65 },
66 }
68 cmdutils.EnableRepoOverride(cmd, f)
69 cmd.PersistentFlags().StringP("group", "g", "", "Select a group or subgroup. Ignored if a repository argument is set.")
70 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
71 cmd.Flags().IntVarP(&opts.perPage, "per-page", "P", 20, "Number of items to list per page.")
72 cmd.Flags().IntVarP(&opts.page, "page", "p", 1, "Page number.")
73 cmd.Flags().BoolVarP(&opts.instance, "instance", "i", false, "Display instance variables.")
75 return cmd
78func (o *options) complete(cmd *cobra.Command) error {
79 group, err := cmdutils.GroupOverride(cmd)
80 if err != nil {
81 return err
82 }
83 o.group = group
85 return nil
88func (o *options) run() error {
89 color := o.io.Color()
91 // NOTE: this command can not only be used for projects,
92 // so we have to manually check for the base repo, if it doesn't exist,
93 // we bootstrap the client with the default hostname.
94 var repoHost string
95 if baseRepo, err := o.baseRepo(); err == nil {
96 repoHost = baseRepo.RepoHost()
97 }
98 apiClient, err := o.apiClient(repoHost)
99 if err != nil {
100 return err
101 }
102 client := apiClient.Lab()
104 table := tableprinter.NewTablePrinter()
106 switch {
107 case o.group != "":
108 if o.outputFormat != "json" {
109 o.io.LogInfof("Listing variables for the %s group:\n\n", color.Bold(o.group))
110 }
111 listOpts := &gitlab.ListGroupVariablesOptions{
112 ListOptions: gitlab.ListOptions{
113 Page: int64(o.page),
114 PerPage: int64(o.perPage),
115 },
116 }
117 variables, _, err := client.GroupVariables.ListVariables(o.group, listOpts)
118 if err != nil {
119 return err
120 }
121 if o.outputFormat == "json" {
122 if err := o.io.PrintJSON(variables); err != nil {
123 return err
124 }
125 } else {
126 table.AddRow("KEY", "PROTECTED", "MASKED", "HIDDEN", "EXPANDED", "SCOPE", "DESCRIPTION")
127 for _, variable := range variables {
128 table.AddRow(variable.Key, variable.Protected, variable.Masked, variable.Hidden, !variable.Raw, variable.EnvironmentScope, variable.Description)
129 }
130 }
131 case o.instance:
132 if o.outputFormat != "json" {
133 o.io.LogInfo("Listing variables for the instance\n\n")
134 }
135 listOpts := &gitlab.ListInstanceVariablesOptions{
136 ListOptions: gitlab.ListOptions{
137 Page: int64(o.page),
138 PerPage: int64(o.perPage),
139 },
140 }
141 variables, _, err := client.InstanceVariables.ListVariables(listOpts)
142 if err != nil {
143 return err
144 }
145 if o.outputFormat == "json" {
146 if err := o.io.PrintJSON(variables); err != nil {
147 return err
148 }
149 } else {
150 table.AddRow("KEY", "PROTECTED", "MASKED", "EXPANDED", "SCOPE", "DESCRIPTION")
151 for _, variable := range variables {
152 table.AddRow(variable.Key, variable.Protected, variable.Masked, !variable.Raw, "", variable.Description)
153 }
154 }
155 default:
156 repo, err := o.baseRepo()
157 if err != nil {
158 return err
159 }
160 if o.outputFormat != "json" {
161 o.io.LogInfof("Listing variables from the %s project:\n\n", color.Bold(repo.FullName()))
162 }
163 listOpts := &gitlab.ListProjectVariablesOptions{
164 ListOptions: gitlab.ListOptions{
165 Page: int64(o.page),
166 PerPage: int64(o.perPage),
167 },
168 }
169 variables, _, err := client.ProjectVariables.ListVariables(repo.FullName(), listOpts)
170 if err != nil {
171 return err
172 }
173 if o.outputFormat == "json" {
174 if err := o.io.PrintJSON(variables); err != nil {
175 return err
176 }
177 } else {
178 table.AddRow("KEY", "PROTECTED", "MASKED", "HIDDEN", "EXPANDED", "SCOPE", "DESCRIPTION")
179 for _, variable := range variables {
180 table.AddRow(variable.Key, variable.Protected, variable.Masked, variable.Hidden, !variable.Raw, variable.EnvironmentScope, variable.Description)
181 }
182 }
183 }
185 if o.outputFormat != "json" {
186 o.io.LogInfof("%s", table.String())
187 }
188 return nil
Diagnostics8 git calls · 211 ms
CallStatusSizeTime0211 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=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 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
fetchtree:0 · deepen 1020032 KiB · hit2 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=2
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
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=3
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 · 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=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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200375 B · hit2 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=375
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=210e8ccdc9766a94adae919c6922ea88d77a8fb1
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=375
git.pkt.payload.max=348
git.sideband.band1.bytes=348
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=348
git.pack.trailer=8c3972a32424b06b4c572b8aa869b28eca201985
Open pack map
fetchtree:0200134 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=193
git.response.bytes=134
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=aadd074ebc9eb2c2240541bf5e43203ba102611e
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=de9d71bf05a219b4867c40a383040dd7177df2eb
Open pack map
fetch2001.7 KiB193 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1719
git.response.cache=MISS
git.response.duration_ms=193
git.fetch.wants=114ea6fda537ed95f3f26411abe2400f61fb92fc
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=1719
git.pkt.payload.max=1692
git.sideband.band1.bytes=1692
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
a5e294d36900224d
Request
2ceffbd4…
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