|
| 1 | +# AGENTS.md — CLI |
| 2 | + |
| 3 | +This file provides guidance to AI coding assistants when working with the `tools` CLI. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`tools` is a Go CLI that wraps the GlueOps Tools API. It authenticates via Dex device code flow through oauth2-proxy and self-updates when the API version changes. |
| 8 | + |
| 9 | +All Go builds use Docker (`golang:1.24-alpine`, pinned by digest in the Makefile) — no local Go toolchain is required. |
| 10 | + |
| 11 | +## Build |
| 12 | + |
| 13 | +```bash |
| 14 | +cd cli |
| 15 | + |
| 16 | +# Build for current platform |
| 17 | +make build |
| 18 | + |
| 19 | +# Build for all release platforms (linux/darwin × amd64/arm64) |
| 20 | +make build-all |
| 21 | + |
| 22 | +# Regenerate OpenAPI client after API changes |
| 23 | +make generate |
| 24 | + |
| 25 | +# Clean build artifacts |
| 26 | +make clean |
| 27 | +``` |
| 28 | + |
| 29 | +## Regenerating the API Client |
| 30 | + |
| 31 | +When the API changes (new endpoints, schema changes, updated descriptions/examples), run: |
| 32 | + |
| 33 | +```bash |
| 34 | +cd cli |
| 35 | +make generate |
| 36 | +``` |
| 37 | + |
| 38 | +This does three things: |
| 39 | +1. Builds the tools-api Docker image and exports the OpenAPI spec to `openapi.json` |
| 40 | +2. Runs `oapi-codegen` (pinned to v2.6.0) via Docker to regenerate `api/generated.go` |
| 41 | +3. Copies `openapi.json` to `internal/spec/openapi.json` for embedding |
| 42 | + |
| 43 | +The generated client (`api/generated.go`) and both copies of `openapi.json` are committed to the repo. |
| 44 | + |
| 45 | +## Architecture |
| 46 | + |
| 47 | +### File Structure |
| 48 | + |
| 49 | +``` |
| 50 | +cli/ |
| 51 | +├── main.go # Entry point |
| 52 | +├── go.mod / go.sum # Go module (github.com/GlueOps/tools-api/cli) |
| 53 | +├── Makefile # Docker-based build targets |
| 54 | +├── openapi.json # Exported OpenAPI spec from FastAPI |
| 55 | +├── oapi-codegen.yaml # oapi-codegen config (generates types + client) |
| 56 | +├── api/ |
| 57 | +│ └── generated.go # Auto-generated typed client — DO NOT EDIT |
| 58 | +├── cmd/ |
| 59 | +│ ├── root.go # Root command, persistent flags, auth/update pre-run |
| 60 | +│ ├── client.go # Authenticated API client helper + response handler (pretty-prints JSON) |
| 61 | +│ ├── version.go # tools version |
| 62 | +│ ├── login.go # tools login (device code flow) |
| 63 | +│ ├── logout.go # tools logout |
| 64 | +│ ├── storage_buckets.go # tools storage-buckets create |
| 65 | +│ ├── aws.go # tools aws setup-credentials, aws nuke-account |
| 66 | +│ ├── nuke.go # tools nuke captain-domain-data |
| 67 | +│ ├── github.go # tools github reset-org, github workflow-status |
| 68 | +│ ├── chisel.go # tools chisel create, chisel delete |
| 69 | +│ ├── opsgenie.go # tools opsgenie create |
| 70 | +│ └── captain_manifests.go # tools captain-manifests generate |
| 71 | +└── internal/ |
| 72 | + ├── auth/ |
| 73 | + │ ├── device_flow.go # Dex device code flow (issuer: dex.toolshosted.com) |
| 74 | + │ └── token.go # Token storage/refresh (~/.config/glueops/tools-cli/tokens.json) |
| 75 | + ├── config/ |
| 76 | + │ └── config.go # Config dir management (~/.config/glueops/tools-cli/) |
| 77 | + ├── spec/ |
| 78 | + │ ├── spec.go # Embedded OpenAPI spec parser (examples, summaries, descriptions) |
| 79 | + │ └── openapi.json # Embedded copy of OpenAPI spec (go:embed) |
| 80 | + ├── updater/ |
| 81 | + │ └── updater.go # Self-update from GitHub releases when API version changes |
| 82 | + └── version/ |
| 83 | + └── version.go # Build-time injected version vars (ldflags) |
| 84 | +``` |
| 85 | + |
| 86 | +### Key Design Decisions |
| 87 | + |
| 88 | +- **OpenAPI as single source of truth** — CLI flag descriptions, command summaries, and long descriptions are all read from the embedded `openapi.json` at compile time via `internal/spec`. When API docstrings or schema examples change, `make generate` + rebuild picks them up automatically without editing Go code. |
| 89 | +- **Auto-generated API client** — `api/generated.go` is produced by `oapi-codegen` from the OpenAPI spec. Each command file in `cmd/` constructs requests using generated types and calls generated client methods. |
| 90 | +- **Auth via PersistentPreRunE** — `root.go` checks for a valid token before every command except `login`, `logout`, `version`, `completion`, `help`, and the root command itself (so `tools --help` works without login). Expired tokens are automatically refreshed. |
| 91 | +- **Self-update** — On every invocation, the CLI checks `GET /version` on the API. If the version differs (and isn't a placeholder like `UNKNOWN` or `dev`), it downloads the matching binary from GitHub releases and replaces itself. |
| 92 | +- **Config directory** — `~/.config/glueops/tools-cli/` stores `tokens.json`. |
| 93 | + |
| 94 | +### Adding a New Command |
| 95 | + |
| 96 | +1. Add the endpoint to `app/main.py` and schema to `app/schemas/schemas.py` |
| 97 | +2. Run `cd cli && make generate` to update the client and spec |
| 98 | +3. Create `cli/cmd/<command>.go`: |
| 99 | + - Use `spec.Summary()` and `spec.Description()` for `Short`/`Long` |
| 100 | + - Use `spec.FlagDesc()` for flag descriptions |
| 101 | + - Use `newClient()` from `client.go` to get an authenticated client |
| 102 | + - Use `handleResponse()` to print the response |
| 103 | +4. Register the command with `rootCmd` in `init()` |
| 104 | + |
| 105 | +### Key Dependencies |
| 106 | + |
| 107 | +- **`github.com/spf13/cobra`** — CLI framework |
| 108 | +- **`github.com/oapi-codegen/runtime`** — Runtime helpers for the generated client |
| 109 | + |
| 110 | +### Auth Details |
| 111 | + |
| 112 | +- Dex issuer: `https://dex.toolshosted.com` |
| 113 | +- Client ID: `tools-cli` |
| 114 | +- Scopes: `openid email profile offline_access` |
| 115 | +- Default API URL: `https://tools.toolshosted.rocks` (overridable via `--api-url`) |
| 116 | + |
| 117 | +## CI/CD |
| 118 | + |
| 119 | +`.github/workflows/cli_release.yaml` builds CLI binaries on every push: |
| 120 | +- Cross-compiles for linux/amd64, linux/arm64, darwin/amd64, darwin/arm64 via Docker |
| 121 | +- Uploads binaries as workflow artifacts |
| 122 | +- Creates a GitHub Release tagged with `github.ref_name` (rolling `main` release for branch pushes, versioned releases for tag pushes) |
| 123 | +- Version is injected via ldflags from the git ref |
0 commit comments