GitLabgitlab-org
cli
main
1package set
3import (
4 "fmt"
5 "strings"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 "gitlab.com/gitlab-org/cli/internal/cmdutils"
11 "gitlab.com/gitlab-org/cli/internal/commands/auth/authutils"
12 "gitlab.com/gitlab-org/cli/internal/config"
13 "gitlab.com/gitlab-org/cli/internal/config/confighelp"
14 "gitlab.com/gitlab-org/cli/internal/dbg"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/oauth2"
18)
20type options struct {
21 io *iostreams.IOStreams
22 config func() config.Config
24 hostname string
25 isGlobal bool
26 key string
27 value string
28}
30func NewCmdSet(f cmdutils.Factory) *cobra.Command {
31 opts := &options{
32 io: f.IO(),
33 config: f.Config,
34 }
36 cmd := &cobra.Command{
37 Use: "set <key> <value>",
38 Short: "Updates configuration with the value of a given key.",
39 Long: heredoc.Docf(`Use %[1]sglab config set --global%[1]s to write to the global configuration.
40 Specifying the %[1]s--host%[1]s flag also saves to the global configuration file.
42 Keys that glab manages for itself are always written to the global
43 configuration, even without %[1]s--global%[1]s.
44 `, "`"),
45 Example: heredoc.Doc(`
46glab config set editor vim
47glab config set token xxxxx --host gitlab.com
48glab config set check_update false --global`),
49 Args: cobra.ExactArgs(2),
50 ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
51 switch len(args) {
52 case 0:
53 return confighelp.KeyCompletions(true), cobra.ShellCompDirectiveNoFileComp
54 case 1:
55 return confighelp.ValueCompletions(args[0])
56 }
57 return nil, cobra.ShellCompDirectiveNoFileComp
58 },
59 Annotations: map[string]string{
60 mcpannotations.Exclude: "true",
61 },
62 RunE: func(cmd *cobra.Command, args []string) error {
63 opts.complete(args)
64 if err := opts.validate(); err != nil {
65 return err
66 }
67 return opts.run()
68 },
69 }
71 fl := cmd.Flags()
72 fl.StringVarP(&opts.hostname, "host", "", "", "Set per-host setting.")
73 fl.BoolVarP(&opts.isGlobal, "global", "g", false, "Write to global '~/.config/glab-cli/config.yml' file rather than the repository's '.git/glab-cli/config.yml' file.")
74 return cmd
75}
77func (o *options) complete(args []string) {
78 o.key = args[0]
79 o.value = args[1]
80 if config.ConfigKeyEquivalence(o.key) == "client_id" {
81 o.value = strings.TrimSpace(o.value)
82 }
83}
85func (o *options) validate() error {
86 if !config.IsKnownKey(o.key) {
87 return fmt.Errorf("%q is not a recognized glab config key, run `glab config` to see the supported keys", o.key)
88 }
89 if config.ConfigKeyEquivalence(o.key) == "client_id" && o.value != "" {
90 if err := oauth2.ValidateClientID(o.value); err != nil {
91 return fmt.Errorf("invalid value for %q: %w", o.key, err)
92 }
93 }
94 return nil
95}
97func (o *options) run() error {
98 cfg := o.config()
100 localCfg, _ := cfg.Local()
102 if err := o.clearStaleOAuth2Config(cfg); err != nil {
103 return err
104 }
106 // A key glab maintains for itself is read from the global file only, so a
107 // local write would land where nothing reads it and the old local value
108 // would keep winning. Route those to the global file instead.
109 routed := !o.isGlobal && o.hostname == "" && config.IsGlobalOnlyKey(o.key)
110 writeGlobal := o.isGlobal || o.hostname != "" || routed
111 if routed {
112 o.io.LogErrorf("%s %s is managed by glab and is always stored in the global configuration: %s\n",
113 o.io.Color().DotWarnIcon(), o.key, config.ConfigFile())
114 }
116 var err error
117 if writeGlobal {
118 err = cfg.Set(o.hostname, o.key, o.value)
119 } else {
120 err = localCfg.Set(o.key, o.value)
121 }
123 if err != nil {
124 // The value is deliberately left out of the message: keys such as
125 // `token` and `job_token` hold credentials, and this error reaches
126 // stderr, CI logs, and terminal scrollback. The user supplied the
127 // value, so the key alone is enough to locate the failure.
128 return fmt.Errorf("failed to set %q: %w", o.key, err)
129 }
131 if writeGlobal {
132 err = cfg.Write()
133 } else {
134 err = localCfg.Write()
135 }
137 if err != nil {
138 return fmt.Errorf("failed to write configuration to disk: %w", err)
139 }
140 return nil
141}
143// clearStaleOAuth2Config drops the OAuth session fields when a token is written
144// to an OAuth-authenticated host. Left in place, is_oauth2 sends the new token
145// as a Bearer credential: API calls still succeed, since GitLab accepts a
146// personal access token that way, but the credential helpers fail, so the
147// breakage surfaces far from this command. Persisted by the caller's Write.
148func (o *options) clearStaleOAuth2Config(cfg config.Config) error {
149 if o.hostname == "" || o.value == "" || config.ConfigKeyEquivalence(o.key) != "token" {
150 return nil
151 }
153 // searchEnvVars: false, so a stray GLAB_IS_OAUTH2 cannot trigger a rewrite of
154 // the config file. A read failure only costs the cleanup, which `glab auth
155 // status` also reports, so it must not fail the write the user asked for.
156 isOAuth2, _, err := cfg.GetWithSource(o.hostname, "is_oauth2", false)
157 if err != nil {
158 dbg.Debugf("could not read is_oauth2 for %s, leaving the OAuth configuration alone: %v", o.hostname, err)
159 return nil
160 }
161 if isOAuth2 != "true" {
162 return nil
163 }
165 if err := authutils.ClearOAuth2Fields(cfg, o.hostname); err != nil {
166 return fmt.Errorf("failed to clear the OAuth configuration for %q: %w", o.hostname, err)
167 }
169 o.io.LogErrorf("%s Cleared the OAuth configuration for %s, which authenticates with the token you set.\n", o.io.Color().WarnIcon(), o.hostname)
170 return nil
171}
Diagnostics8 git calls · 202 ms
CallStatusSizeTime0202 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 · hit3 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=HIT 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=2 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 · 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=2 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:0200222 B · 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=222 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=4892d06f6f71f04d3c94318a90a62d9487e287c2 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=222 git.pkt.payload.max=195 git.sideband.band1.bytes=195 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=195 git.pack.trailer=18940a1fb32891f80983c75dd5bb2c39d512d714Open pack map
fetchtree:0200141 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=141 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=5fdde2eba5db1cd641ff80fdd1d56e9727f74ec5 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=141 git.pkt.payload.max=114 git.sideband.band1.bytes=114 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=114 git.pack.trailer=fd3cd57456629891dd81421d72165b117cd056a2Open pack map
fetch2002.2 KiB188 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=188 git.response.bytes=2260 git.response.cache=MISS git.response.duration_ms=188 git.fetch.wants=e3bae0410fbf2dd15ad6d03ac2492463a88465ce 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=2260 git.pkt.payload.max=2233 git.sideband.band1.bytes=2233 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
- a5e2d16288b0cf3a
- Request
- f84b5493…
- 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