GitLabgitlab-org
cli
main
1package set
3import (
4 "errors"
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/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/commands/variable/variableutils"
15 "gitlab.com/gitlab-org/cli/internal/glrepo"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
18)
20type options struct {
21 apiClient func(repoHost string) (*api.Client, error)
22 io *iostreams.IOStreams
23 baseRepo func() (glrepo.Interface, error)
25 key string
26 value string
27 typ string
28 scope string
29 protected bool
30 masked bool
31 hidden bool
32 raw bool
33 group string
34 description string
35}
37func NewCmdSet(f cmdutils.Factory, runE func(opts *options) error) *cobra.Command {
38 opts := &options{
39 io: f.IO(),
40 apiClient: f.ApiClient,
41 baseRepo: f.BaseRepo,
42 }
44 cmd := &cobra.Command{
45 Use: "set <key> <value>",
46 Short: "Create a new variable for a project or group.",
47 Long: heredoc.Docf(`
48 You can pass the variable value from standard input. Use %[1]s--group%[1]s
49 to set a variable for a group instead of the current project.
50 `, "`"),
51 Aliases: []string{"new", "create"},
52 Args: cobra.RangeArgs(1, 2),
53 Example: heredoc.Doc(`
54 glab variable set WITH_ARG "some value"
55 glab variable set WITH_DESC "some value" --description "some description"
56 glab variable set FROM_FLAG -v "some value"
57 glab variable set FROM_ENV_WITH_ARG "${ENV_VAR}"
58 glab variable set FROM_ENV_WITH_FLAG -v"${ENV_VAR}"
59 glab variable set FROM_FILE < secret.txt
60 cat file.txt | glab variable set SERVER_TOKEN
61 cat token.txt | glab variable set GROUP_TOKEN -g mygroup --scope=prod`),
62 Annotations: map[string]string{
63 mcpannotations.Destructive: "true",
64 },
65 RunE: func(cmd *cobra.Command, args []string) error {
66 opts.complete(args)
68 if err := opts.validate(cmd, args); err != nil {
69 return err
70 }
72 if runE != nil {
73 return runE(opts)
74 }
75 return opts.run()
76 },
77 }
79 cmd.Flags().StringVarP(&opts.value, "value", "v", "", "The value of a variable.")
80 cmd.Flags().StringVarP(&opts.typ, "type", "t", "env_var", "The type of a variable: env_var, file.")
81 cmd.Flags().StringVarP(&opts.scope, "scope", "s", "*", "The environment_scope of the variable. Values: all (*), or specific environments.")
82 cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Set variable for a group.")
83 cmd.Flags().BoolVarP(&opts.masked, "masked", "m", false, "Whether the variable is masked.")
84 cmd.Flags().BoolVarP(&opts.hidden, "hidden", "", false, "Whether the variable is hidden.")
85 cmd.Flags().BoolVarP(&opts.raw, "raw", "r", false, "Whether the variable is treated as a raw string.")
86 cmd.Flags().BoolVarP(&opts.protected, "protected", "p", false, "Whether the variable is protected.")
87 cmd.Flags().StringVarP(&opts.description, "description", "d", "", "Set description of a variable.")
88 return cmd
89}
91func (o *options) complete(args []string) {
92 o.key = args[0]
93}
95func (o *options) validate(cmd *cobra.Command, args []string) error {
96 if !variableutils.IsValidKey(o.key) {
97 return cmdutils.FlagError{Err: fmt.Errorf("invalid key provided: %s", variableutils.ValidKeyMsg)}
98 }
100 if o.value != "" && len(args) == 2 {
101 return cmdutils.FlagError{Err: errors.New("specify value either by the second positional argument or the --value flag")}
102 }
104 value, err := variableutils.GetValue(o.value, o.io, args)
105 if err != nil {
106 return err
107 }
108 o.value = value
110 if cmd.Flags().Changed("type") {
111 if o.typ != "env_var" && o.typ != "file" {
112 return cmdutils.FlagError{Err: fmt.Errorf("invalid type %q: --type must be one of `env_var` or `file`", o.typ)}
113 }
114 }
116 return nil
117}
119func (o *options) run() error {
120 c := o.io.Color()
122 // NOTE: this command can not only be used for projects,
123 // so we have to manually check for the base repo, if it doesn't exist,
124 // we bootstrap the client with the default hostname.
125 var repoHost string
126 if baseRepo, err := o.baseRepo(); err == nil {
127 repoHost = baseRepo.RepoHost()
128 }
129 apiClient, err := o.apiClient(repoHost)
130 if err != nil {
131 return err
132 }
133 client := apiClient.Lab()
135 if o.group != "" {
136 // creating group-level variable
137 createVarOpts := &gitlab.CreateGroupVariableOptions{
138 Key: new(o.key),
139 Value: new(o.value),
140 EnvironmentScope: new(o.scope),
141 Masked: new(o.masked),
142 Protected: new(o.protected),
143 VariableType: new(gitlab.VariableTypeValue(o.typ)),
144 Raw: new(o.raw),
145 Description: new(o.description),
146 }
147 if o.hidden {
148 createVarOpts.MaskedAndHidden = new(true)
149 }
151 _, _, err := client.GroupVariables.CreateVariable(o.group, createVarOpts)
152 if err != nil {
153 return err
154 }
156 o.io.LogInfof("%s Created variable %s for group %s.\n", c.GreenCheck(), o.key, o.group)
157 return nil
158 }
160 // creating project-level variable
161 baseRepo, err := o.baseRepo()
162 if err != nil {
163 return err
164 }
165 createVarOpts := &gitlab.CreateProjectVariableOptions{
166 Key: new(o.key),
167 Value: new(o.value),
168 EnvironmentScope: new(o.scope),
169 Masked: new(o.masked),
170 Protected: new(o.protected),
171 VariableType: new(gitlab.VariableTypeValue(o.typ)),
172 Raw: new(o.raw),
173 Description: new(o.description),
174 }
175 if o.hidden {
176 createVarOpts.MaskedAndHidden = new(true)
177 }
178 _, _, err = client.ProjectVariables.CreateVariable(baseRepo.FullName(), createVarOpts)
179 if err != nil {
180 return err
181 }
183 o.io.LogInfof("%s Created variable %s for %s with scope %s.\n", c.GreenCheck(), o.key, baseRepo.FullName(), o.scope)
184 return nil
185}
Diagnostics8 git calls · 212 ms
CallStatusSizeTime0212 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 KiB4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=4 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=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=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=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:0200375 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=375 git.response.cache=HIT git.response.duration_ms=3 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=8c3972a32424b06b4c572b8aa869b28eca201985Open pack map
fetchtree:0200134 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=194 git.response.bytes=134 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=9636692405552af7a4ff3c20e1588b5499dea105 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=23fbc292075ac7563d781cd93a1c3a9896ec306bOpen pack map
fetch2002.0 KiB194 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2049 git.response.cache=MISS git.response.duration_ms=194 git.fetch.wants=fa672ddf1a19465f9d9401a9b17e3672b28171b5 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=2049 git.pkt.payload.max=2022 git.sideband.band1.bytes=2022 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
- a5e2148288e6984a
- Request
- 4e01c013…
- 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