# Add Shell Completion to a Go CLI from a Linear Issue

An agent makes a Cobra CLI's file argument complete Markdown files only, documents bash, zsh and fish setup, and tests the generated scripts.

[Run this on Rush](https://cloud.getrush.ai/run/add-shell-completion/go-cli/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `main.go`, `glow_test.go`, `man_cmd.go`, the README and `utils/utils.go` in charmbracelet/glow before changing anything. Cobra's default `completion` command already printed bash, zsh and fish scripts. The gap was `rootCmd`'s `ValidArgsFunction`, which returned `ShellCompDirectiveDefault`, so tab completion offered every file.

It adds `completeMarkdownFiles` in `main.go`, returning glow's Markdown extensions with `ShellCompDirectiveFilterFileExt`, and `ShellCompDirectiveNoFileComp` once an argument is given. The extensions come from a new `utils.MarkdownExtensions()` that reads the list `IsMarkdownFile` already uses, not a second copy. Three tests in `glow_test.go` check the offered extensions, the second argument and the `completion bash|zsh|fish` output, and the README gains a Shell Completion section.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add Shell Completion to a Go CLI. Document and test Cobra's completion command for bash, zsh and fish, and make file arguments complete Markdown files only.\" Do the work in this repository, add tests for it, run go test ./..., and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on charmbracelet/glow at 8b0600f: 4 files changed, 119 insertions(+), 7 deletions(-) in `README.md`, `glow_test.go`, `main.go` and `utils/utils.go`. `go test ./...` passed on the untouched clone and again after the change, with `ok  charm.land/glow/v3 1.664s` for the main package under `-count=1`, and `go vet ./...` exited 0. The run worked from the issue text quoted in the request; there was no Linear issue to link.

For the reviewer: glow treats a file with no extension as Markdown, but the extension filter cannot express that, so such files are no longer suggested. Nobody pressed Tab in a live shell; the tests check the directive and the generated scripts.

## Run it on every Linear issue labeled agent

An automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` runs the `linear-label-to-pr` recipe's prompt when someone in your Rush org labels an issue. An issue that asks for PowerShell too, or for a different file filter, reaches the agent with those words.

## Why run it on Rush?

The obvious change was to add a completion command, and it already existed. The run checked that first, so the diff fixes the real gap, the unfiltered file argument. Its first completion test also failed because Cobra binds the command's output to `os.Stdout` at init; the run read Cobra's `completions.go`, then built the binary in the test instead.

## Frequently asked questions

### Why does a test build the binary?

Cobra's default completion command keeps the `os.Stdout` it saw at startup, so swapping stdout in a test captures nothing. The test runs `go build` and calls `completion bash`, `zsh` and `fish` on the result, which adds about 1.5 seconds and needs the Go toolchain at test time.

### Did gofmt change unrelated lines?

Only alignment in the `rootCmd` struct, after `gofmt` ran on `main.go`. `gofmt -l .` already flagged `console_windows.go` on the untouched clone, and the run left that file alone.

## More Go CLI jobs

- [Add Snapshot Tests to a Go CLI's Rendered Output](https://getrush.ai/run/add-snapshot-tests/go-cli/)
- [Fix golangci-lint Findings in a Go CLI and Make Lint Block Merges](https://getrush.ai/run/fix-lint-warnings/go-cli/)
- [Fix Go CLI Errors from Sentry Alerts](https://getrush.ai/run/fix-sentry-errors/go-cli/)

## The same job on other stacks

- [Add Shell Completions to a Rust CLI](https://getrush.ai/run/add-shell-completion/rust-cli/)
- [Add Shell Completion to a Textual App's CLI](https://getrush.ai/run/add-shell-completion/textual/)
- [Add Shell Completion to a Click CLI](https://getrush.ai/run/add-shell-completion/click/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
