GitLabgitlab-org

cli

main
ply8 git calls361 ms100 KiB5 cachedprotocol v2
243 lines7.5 KiBRaw
1package update
3import (
4 "context"
5 "errors"
6 "fmt"
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/variable/variableutils"
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 apiClient func(repoHost string) (*api.Client, error)
23 io *iostreams.IOStreams
24 baseRepo func() (glrepo.Interface, error)
26 key string
27 value string
28 typ string
29 scope string
30 protected bool
31 masked bool
32 raw bool
33 group string
34 description string
36 // Attributes that were not passed on the command line are left out of the
37 // request, so the API keeps their current values.
38 typeChanged bool
39 protectedChanged bool
40 maskedChanged bool
41 rawChanged bool
42 descriptionChanged bool
45func NewCmdUpdate(f cmdutils.Factory, runE func(opts *options) error) *cobra.Command {
46 opts := &options{
47 io: f.IO(),
48 apiClient: f.ApiClient,
49 baseRepo: f.BaseRepo,
50 }
52 cmd := &cobra.Command{
53 Use: "update <key> <value>",
54 Short: "Update an existing variable for a project or group.",
55 Long: heredoc.Docf(`
56 You can pass the variable value from standard input. Use %[1]s--group%[1]s
57 to update a variable for a group instead of the current project.
59 Use %[1]s--scope%[1]s to select the environment scope of the project or
60 group variable. The default scope is %[1]s*%[1]s. Environment scopes for
61 group variables require GitLab Premium or Ultimate.
63 Only the attributes you pass are changed. For example, %[1]s--protected%[1]s,
64 %[1]s--masked%[1]s, %[1]s--raw%[1]s, %[1]s--type%[1]s and %[1]s--description%[1]s keep their
65 current values unless you set them. To turn a boolean attribute off, use
66 %[1]s--protected=false%[1]s.
67 `, "`"),
68 Args: cobra.RangeArgs(1, 2),
69 Example: heredoc.Doc(`
70 glab variable update WITH_ARG "some value"
71 glab variable update FROM_FLAG -v "some value"
72 glab variable update FROM_ENV_WITH_ARG "${ENV_VAR}"
73 glab variable update FROM_ENV_WITH_FLAG -v"${ENV_VAR}"
74 glab variable update FROM_FILE < secret.txt
75 cat file.txt | glab variable update SERVER_TOKEN
76 cat token.txt | glab variable update GROUP_TOKEN -g mygroup
78 # Update a group variable in the production environment.
79 glab variable update GROUP_TOKEN -g mygroup --scope prod --value "some value"`),
80 Annotations: map[string]string{
81 mcpannotations.Destructive: "true",
82 },
83 RunE: func(cmd *cobra.Command, args []string) error {
84 opts.complete(args)
86 if err := opts.validate(cmd, args); err != nil {
87 return err
88 }
90 if runE != nil {
91 return runE(opts)
92 }
94 return opts.run(cmd.Context())
95 },
96 }
98 cmd.Flags().StringVarP(&opts.value, "value", "v", "", "The value of a variable.")
99 cmd.Flags().StringVarP(&opts.typ, "type", "t", "env_var", "The type of a variable: env_var, file.")
100 cmd.Flags().StringVarP(&opts.scope, "scope", "s", "*", "The environment_scope of the variable. Values: all (*), or specific environments.")
101 cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Set variable for a group.")
102 cmd.Flags().BoolVarP(&opts.masked, "masked", "m", false, "Whether the variable is masked.")
103 cmd.Flags().BoolVarP(&opts.raw, "raw", "r", false, "Whether the variable is treated as a raw string.")
104 cmd.Flags().BoolVarP(&opts.protected, "protected", "p", false, "Whether the variable is protected.")
105 cmd.Flags().StringVarP(&opts.description, "description", "d", "", "Set description of a variable.")
106 return cmd
109func (o *options) complete(args []string) {
110 o.key = args[0]
113func (o *options) validate(cmd *cobra.Command, args []string) error {
114 o.typeChanged = cmd.Flags().Changed("type")
115 o.protectedChanged = cmd.Flags().Changed("protected")
116 o.maskedChanged = cmd.Flags().Changed("masked")
117 o.rawChanged = cmd.Flags().Changed("raw")
118 o.descriptionChanged = cmd.Flags().Changed("description")
120 if !variableutils.IsValidKey(o.key) {
121 return cmdutils.FlagError{Err: fmt.Errorf("invalid key provided: %s", variableutils.ValidKeyMsg)}
122 }
124 if o.value != "" && len(args) == 2 {
125 return cmdutils.FlagError{Err: errors.New("specify value either by the second positional argument or the --value flag")}
126 }
128 value, err := variableutils.GetValue(o.value, o.io, args)
129 if err != nil {
130 return err
131 }
132 o.value = value
134 if cmd.Flags().Changed("type") {
135 if o.typ != "env_var" && o.typ != "file" {
136 return cmdutils.FlagError{Err: fmt.Errorf("invalid type %q: --type must be one of `env_var` or `file`", o.typ)}
137 }
138 }
140 return nil
143func (o *options) run(ctx context.Context) error {
144 c := o.io.Color()
146 // NOTE: this command can not only be used for projects,
147 // so we have to manually check for the base repo, if it doesn't exist,
148 // we bootstrap the client with the default hostname.
149 var repoHost string
150 if baseRepo, err := o.baseRepo(); err == nil {
151 repoHost = baseRepo.RepoHost()
152 }
153 apiClient, err := o.apiClient(repoHost)
154 if err != nil {
155 return err
156 }
157 client := apiClient.Lab()
159 if o.group != "" {
160 // GitLab can update another scope when the requested group variable is absent.
161 // Confirm the variable belongs to the exact scope before changing it.
162 variable, _, err := client.GroupVariables.GetVariable(o.group, o.key, &gitlab.GetGroupVariableOptions{
163 Filter: &gitlab.VariableFilter{EnvironmentScope: o.scope},
164 }, gitlab.WithContext(ctx))
165 if err != nil {
166 return err
167 }
168 if variable == nil || variable.EnvironmentScope != o.scope {
169 return fmt.Errorf("group variable %q does not exist in scope %q", o.key, o.scope)
170 }
172 // update group-level variable
173 _, _, err = client.GroupVariables.UpdateVariable(o.group, o.key, o.groupUpdateOptions(), gitlab.WithContext(ctx))
174 if err != nil {
175 return err
176 }
178 o.io.LogInfof("%s Updated variable %s for group %s with scope %s.\n", c.GreenCheck(), o.key, o.group, o.scope)
179 return nil
180 }
182 // update project-level variable
183 baseRepo, err := o.baseRepo()
184 if err != nil {
185 return err
186 }
188 _, _, err = client.ProjectVariables.UpdateVariable(baseRepo.FullName(), o.key, o.projectUpdateOptions(), gitlab.WithContext(ctx))
189 if err != nil {
190 return err
191 }
193 o.io.LogInfof("%s Updated variable %s for project %s with scope %s.\n", c.GreenCheck(), o.key, baseRepo.FullName(), o.scope)
194 return nil
197func (o *options) groupUpdateOptions() *gitlab.UpdateGroupVariableOptions {
198 opts := &gitlab.UpdateGroupVariableOptions{
199 Value: new(o.value),
200 EnvironmentScope: new(o.scope),
201 Filter: &gitlab.VariableFilter{EnvironmentScope: o.scope},
202 }
203 if o.typeChanged {
204 opts.VariableType = new(gitlab.VariableTypeValue(o.typ))
205 }
206 if o.maskedChanged {
207 opts.Masked = new(o.masked)
208 }
209 if o.protectedChanged {
210 opts.Protected = new(o.protected)
211 }
212 if o.rawChanged {
213 opts.Raw = new(o.raw)
214 }
215 if o.descriptionChanged {
216 opts.Description = new(o.description)
217 }
218 return opts
221func (o *options) projectUpdateOptions() *gitlab.UpdateProjectVariableOptions {
222 opts := &gitlab.UpdateProjectVariableOptions{
223 Value: new(o.value),
224 EnvironmentScope: new(o.scope),
225 Filter: &gitlab.VariableFilter{EnvironmentScope: o.scope},
226 }
227 if o.typeChanged {
228 opts.VariableType = new(gitlab.VariableTypeValue(o.typ))
229 }
230 if o.maskedChanged {
231 opts.Masked = new(o.masked)
232 }
233 if o.protectedChanged {
234 opts.Protected = new(o.protected)
235 }
236 if o.rawChanged {
237 opts.Raw = new(o.raw)
238 }
239 if o.descriptionChanged {
240 opts.Description = new(o.description)
241 }
242 return opts
Diagnostics8 git calls · 361 ms
CallStatusSizeTime0361 ms
discover200185 B4 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 KiB2 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=UPDATING
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 · hit2 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=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 · 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: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=4
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 · hit4 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=375
git.response.cache=HIT
git.response.duration_ms=4
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:0200138 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=345
git.response.bytes=138
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=82e9c2bc0610e79e6ed8b3383ab3bdfa6ef24385
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=afabf17e523d1096228b6cb0eaca23458d7c608f
Open pack map
fetch2002.5 KiB345 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2533
git.response.cache=MISS
git.response.duration_ms=345
git.fetch.wants=d18a82d7f7fd67dc5ee0f6f105ee925ff40d5af2
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=2533
git.pkt.payload.max=2506
git.sideband.band1.bytes=2506
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
a5e2066d1bddc69c
Request
af39943d…
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