GitLabgitlab-org
cli
main
1package cli
3import (
4 "context"
5 "errors"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 "gitlab.com/gitlab-org/cli/internal/binarymgr"
11 "gitlab.com/gitlab-org/cli/internal/binarymgr/binaries"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 duoCLIUpdateCmd "gitlab.com/gitlab-org/cli/internal/commands/duo/cli/update"
14 "gitlab.com/gitlab-org/cli/internal/config"
15 "gitlab.com/gitlab-org/cli/internal/dbg"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
18 "gitlab.com/gitlab-org/cli/internal/utils"
19)
21// AppendBinaryStatusFooter registers a help function on cmd that renders glab's
22// standard help and then appends a section describing how to reach the GitLab
23// Duo CLI's own help, depending on whether the binary is installed.
24func AppendBinaryStatusFooter(cmd *cobra.Command, f cmdutils.Factory) {
25 cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
26 cmd.Parent().HelpFunc()(c, args)
27 // Subcommands inherit this help func, but the footer only describes cmd.
28 if c != cmd {
29 return
30 }
32 io := f.IO()
33 io.LogInfo(io.Color().Bold("GITLAB DUO CLI"))
35 path, version, installed := binaryStatus(f.Config())
36 if installed {
37 installedText := path
38 if version != "" {
39 installedText = version + " (" + path + ")"
40 }
41 io.LogInfo(utils.Indent(heredoc.Docf(`
42 Installed: %s
43 Run 'glab duo cli help' for the GitLab Duo CLI commands and flags.`, installedText), " "))
44 return
45 }
47 io.LogInfo(utils.Indent(heredoc.Doc(`
48 The GitLab Duo CLI binary is not installed yet. To get started:
50 1. glab auth login # authenticate, once
51 2. glab duo cli --install # download the GitLab Duo CLI binary
52 3. glab duo cli # start an interactive session
54 Or run 'glab duo cli' and follow the prompts.
55 After it is installed, 'glab duo cli help' shows the GitLab Duo CLI
56 commands and flags.`), " "))
57 })
58}
60func binaryStatus(cfg config.Config) (string, string, bool) {
61 status, err := binarymgr.InstalledBinary(cfg, binaries.DuoCLI())
62 if err != nil {
63 dbg.Debugf("binaryStatus: %v", err)
64 }
65 return status.Path, status.Version, status.Installed
66}
68// NewCmd creates the `glab duo cli` command.
69func NewCmd(f cmdutils.Factory) *cobra.Command {
70 spec := binaries.DuoCLI()
71 cmd := &cobra.Command{
72 Use: "cli [command]",
73 Short: "Run the GitLab Duo CLI.",
74 Long: heredoc.Docf(`
75 Run the GitLab Duo CLI (%[1]sduo%[1]s) through %[1]sglab%[1]s.
77 The GitLab Duo CLI brings the GitLab Duo Agent Platform to your terminal. Ask GitLab Duo questions about your codebase and use it to autonomously perform actions on your behalf.
79 The GitLab Duo CLI runs in two modes:
81 - Interactive (default): %[1]sglab duo cli%[1]s opens a session for multiple prompts, with build and plan modes.
82 - Headless: %[1]sglab duo cli run --goal "<prompt>"%[1]s runs a single prompt and exits. For use in runners, scripts, and automated workflows.
84 When you use the GitLab Duo CLI through %[1]sglab%[1]s, %[1]sglab%[1]s handles authentication for you. You authenticate only once.
86 Prerequisites:
88 - Use GitLab 19.2 or later.
89 - Run %[1]sglab auth login%[1]s to authenticate.
90 - Meet the [prerequisites for GitLab Duo Agent Platform](https://docs.gitlab.com/user/duo_agent_platform/#prerequisites).
91 - Set a [default GitLab Duo namespace](https://docs.gitlab.com/user/profile/preferences/#namespace-resolution-in-your-local-environment), or run the command in a project that has GitLab Duo access.
92 - For GitLab Self-Managed and GitLab Dedicated on 19.2 or later, turn on [GitLab Duo CLI access](https://docs.gitlab.com/user/gitlab_duo_cli/#manage-gitlab-duo-cli-access). It is on by default.
94 If you are on GitLab 18.11 to 19.1, you can use the GitLab Duo CLI by turning on [beta and experimental features](https://docs.gitlab.com/user/duo_agent_platform/turn_on_off/#turn-on-beta-and-experimental-features).
96 Configuration options:
98 - %[1]sduo_cli_auto_run%[1]s: Skip the run confirmation prompt.
99 - %[1]sduo_cli_auto_download%[1]s: Skip the download confirmation prompt.
101 Except for the %[1]supdate%[1]s command, %[1]sglab%[1]s passes all other arguments and flags through to the GitLab Duo CLI binary. To see the GitLab Duo CLI commands and flags, run %[1]sglab duo cli help%[1]s.
103 For more information, see the [GitLab Duo CLI documentation](https://docs.gitlab.com/user/gitlab_duo_cli/).
104 `, "`"),
105 Annotations: map[string]string{
106 "help:environment": heredoc.Docf(`
107 - %[1]sGLAB_DUO_CLI_BINARY_PATH%[1]s: Use a local binary instead of the managed one.
108 Skips download, version checks, and updates. Can also be set through the
109 %[1]sduo_cli_binary_path%[1]s configuration key.
110 `, "`"),
111 mcpannotations.Exclude: "true",
112 },
113 Example: heredoc.Doc(`
114 # Start an interactive GitLab Duo CLI session
115 glab duo cli
117 # Use the GitLab Duo CLI in headless mode with a single prompt
118 glab duo cli run --goal "Fix the failing tests in this project"
120 # Pass any command or flag through to the GitLab Duo CLI binary (for example: model, version, run)
121 glab duo cli <command>
122 glab duo cli --model claude_sonnet_4_6
124 # Show GitLab CLI help content
125 glab duo cli --help
127 # Show GitLab Duo CLI help content, including commands and flags
128 glab duo cli help
130 # Run without prompts (for use in scripts and non-interactive environments)
131 glab duo cli --yes
133 # Install the GitLab Duo CLI binary
134 glab duo cli --install
136 # Install the GitLab Duo CLI binary without prompts
137 glab duo cli --install --yes
139 # Check for and install updates
140 glab duo cli update`),
141 DisableFlagParsing: true,
142 Args: cobra.ArbitraryArgs,
143 RunE: func(cmd *cobra.Command, args []string) error {
144 runner := newRunner(f.IO(), f.Config(), spec)
146 // DisableFlagParsing is on, so split glab-owned flags from
147 // pass-through args manually. --help/-h shows glab's help only
148 // when nothing has been collected for pass-through yet.
149 var remaining []string
150 for _, arg := range args {
151 switch arg {
152 case "--update":
153 runner.Update = true
154 case "--install":
155 runner.Install = true
156 case "--yes", "-y":
157 runner.Yes = true
158 case "--help", "-h":
159 if len(remaining) == 0 {
160 return cmd.Help()
161 }
162 remaining = append(remaining, arg)
163 default:
164 remaining = append(remaining, arg)
165 }
166 }
167 runner.Args = remaining
169 if runner.Install && runner.Update {
170 return errors.New("the --install and --update flags are mutually exclusive")
171 }
173 warnIfSnapConfined(f.IO(), runner.Install, runner.Update)
175 if runner.Install {
176 return runner.HandleInstall(cmd.Context())
177 }
178 return runner.Run(cmd.Context())
179 },
180 }
182 // Registered for documentation only — DisableFlagParsing means Cobra
183 // never parses these; the RunE switch above handles them manually.
184 fl := cmd.Flags()
185 fl.BoolP("yes", "y", false, "Skip confirmation prompts.")
186 fl.Bool("install", false, "Install the GitLab Duo CLI binary without running it.")
187 fl.Bool("update", false, "Check for and install updates to the binary. Same as the update command.")
189 cmd.AddCommand(duoCLIUpdateCmd.NewCmd(f))
190 AppendBinaryStatusFooter(cmd, f)
192 return cmd
193}
195// warnIfSnapConfined warns when `glab duo cli` runs under snap confinement:
196// the sandbox blocks the credential-helper callback the Duo binary needs, so
197// authentication fails even after a successful `glab auth login`. --install
198// and --update skip this warning since they don't hit the callback.
199func warnIfSnapConfined(io *iostreams.IOStreams, install, update bool) {
200 if install || update {
201 return
202 }
203 if !config.SnapConfined() {
204 return
205 }
206 msg := heredoc.Docf(`
207 %[1]s glab is running under snap confinement.
209 The GitLab Duo CLI authenticates by spawning %[2]sglab auth credential-helper%[2]s, but
210 snap's sandbox blocks that callback. Authentication is likely to fail with
211 "no credentials found" even after a successful %[2]sglab auth login%[2]s.
213 To use %[2]sglab duo cli%[2]s, install glab from a non-snap source:
214 - Homebrew: %[2]sbrew install glab%[2]s
215 - mise: %[2]smise use -g glab@latest%[2]s
216 - Native binary: https://gitlab.com/gitlab-org/cli/-/releases
218 Or set the GITLAB_TOKEN environment variable (%[2]sGITLAB_TOKEN=glpat-xyz glab duo cli%[2]s).
219 For GitLab Self-Managed, also set GITLAB_URL (%[2]sGITLAB_URL=https://gitlab.example.com GITLAB_TOKEN=glpat-xyz glab duo cli%[2]s).
221 `, io.Color().DotWarnIcon(), "`")
222 io.LogError(msg)
223}
225// newRunner builds a binarymgr.Runner for the Duo CLI. The Executor is
226// platform-specific: syscall.Exec on Unix (in execute_unix.go), subprocess
227// on Windows (in execute_windows.go). Each declares executeDuoCLI(io, ...).
228func newRunner(io *iostreams.IOStreams, cfg config.Config, spec binarymgr.Spec) *binarymgr.Runner {
229 return &binarymgr.Runner{
230 IO: io,
231 Cfg: cfg,
232 Spec: spec,
233 Manager: binarymgr.NewManager(io, spec),
234 Executor: func(ctx context.Context, binaryPath string, args []string) error {
235 return executeDuoCLI(ctx, io, binaryPath, args)
236 },
237 }
238}
Diagnostics8 git calls · 210 ms
CallStatusSizeTime0210 ms
discover200185 B3 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=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:0200191 B · 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=191 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=c3602e112f33dfaff92a8559e2993279d5c2f1ac 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=191 git.pkt.payload.max=164 git.sideband.band1.bytes=164 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=164 git.pack.trailer=43874cd66e8aba9ccffcac699b3b9f515e08cf67Open pack map
fetchtree:0200238 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=195 git.response.bytes=238 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c0d5012d80c7130de331a7d6bbd8da4757868e8d 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=238 git.pkt.payload.max=211 git.sideband.band1.bytes=211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=211 git.pack.trailer=3ab8f4d29d6b378c7200388b9a42af0739e36766Open pack map
fetch2003.4 KiB195 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3480 git.response.cache=MISS git.response.duration_ms=195 git.fetch.wants=de2f9b917c1aa0f3a6e4053ec049f6a22ba45fdc 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=3480 git.pkt.payload.max=3453 git.sideband.band1.bytes=3453 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
- a5e23a907ea04bc2
- Request
- b43b5ec2…
- 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