grr_cli/cli.rs
1//! grr — Google tools from the terminal, at maximum performance.
2//!
3//! One binary, one login, every service. The command surface has two
4//! layers:
5//!
6//! * a thin, hand-written set of account-level commands — `auth`, `api`,
7//! `mcp`, `transport`, `schema`, `completions`, `ask`, `skills` — kept
8//! as clap derive types;
9//! * the ENTIRE generated service command tree, built from the committed
10//! Discovery index into `commands/generated.rs`, because the index is
11//! the single source of truth for the CLI surface and it changes daily.
12//!
13//! Wiring: clap's derive cannot express a runtime-generated tree, and
14//! `external_subcommand` would forfeit typed flags and per-leaf `--help`.
15//! So [`root_command`] composes the derive's static commands with the
16//! generated service commands onto ONE `Command`, parses once with
17//! `get_matches()`, and dispatch matches the static names first —
18//! everything else is a generated service and goes through
19//! `gen_dispatch::dispatch_with_profile`, which shares the call path with
20//! `grr api`.
21
22use crate::core::prelude::*;
23use anyhow::Result;
24use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
25use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
26
27use crate::commands::{
28 api, ask, auth, build_auth, completions, gen_dispatch, generated, mcp, safety, skills,
29 transport,
30};
31use crate::logo;
32use crate::schema;
33use auth::AuthCommands;
34
35// The `--version` banner lives in `logo`: the semver, then the mascot
36// rendered as well as the terminal allows, then the tagline. clap
37// prefixes the whole string with `grr `, and the semver must stay on the
38// FIRST line — see that module for the three consumers that parse it.
39
40#[derive(Parser)]
41#[command(
42 name = "grr",
43 version = logo::version_banner(),
44 about = "Google tools from the terminal, at maximum performance"
45)]
46struct Cli {
47 #[command(subcommand)]
48 command: StaticCommands,
49 // NOTE: no global --format flag on purpose. A global `format` collides
50 // by ID with per-command `format` args of *different types*, which
51 // panics clap's downcast at runtime (0xC0000409). Every subcommand
52 // declares its own -f/--format.
53}
54
55#[derive(Subcommand)]
56enum StaticCommands {
57 /// Google account authentication (PKCE browser flow; --device for headless)
58 #[command(subcommand, subcommand_required = true)]
59 Auth(auth::AuthCommands),
60
61 /// Discovery-driven access to every Google Workspace method, including
62 /// the ones with no dedicated command. `grr api list` to browse.
63 #[command(subcommand)]
64 Api(api::ApiCommands),
65
66 /// Start a Model Context Protocol server over stdio, exposing every
67 /// grr method as a typed MCP tool. --readonly for a read-only server.
68 Mcp(mcp::McpArgs),
69
70 /// Show negotiated transport protocol and runtime features
71 Transport(transport::TransportArgs),
72
73 /// Dump the full command tree as JSON (machine-readable contract)
74 Schema(schema::SchemaArgs),
75
76 /// Print a shell completion script for the full command tree
77 /// (bash, elvish, powershell, zsh)
78 Completions(completions::CompletionsArgs),
79
80 /// Natural-language entry point: a System One model (Jev by default)
81 /// picks the method from the discovery catalog and fills its
82 /// parameters. Prints the plan; --run executes it.
83 Ask(ask::AskArgs),
84
85 /// Install the packaged agent skills into the user-level skills
86 /// directory (~/.agents/skills); list shows what is installed there
87 #[command(subcommand, subcommand_required = true)]
88 Skills(skills::SkillsCommands),
89}
90
91/// The complete parse tree: the eight static commands plus every generated
92/// service command. `grr --help`, `grr schema` and `grr completions` all
93/// read this, so it is the single definition of the CLI surface.
94///
95/// The safety flags are `global(true)`: clap merges global args across
96/// levels, so `--readonly` parses whether it appears before the subcommand
97/// (`grr --readonly gmail …`) or after it
98/// (`grr gmail users messages list --readonly`), and is readable from the
99/// root matches either way.
100pub(crate) fn root_command() -> clap::Command {
101 Cli::command()
102 .subcommands(generated::tree::commands())
103 .args(safety::global_args())
104}
105
106pub async fn run() -> Result<()> {
107 tracing_subscriber::registry()
108 .with(tracing_subscriber::EnvFilter::new(
109 // Default: info, but silence quinn_udp's harmless IPv6
110 // network-unreachable warnings on v6-less networks (v4 wins).
111 std::env::var("RUST_LOG").unwrap_or_else(|_| "info,quinn_udp=error".into()),
112 ))
113 // Logs go to stderr so stdout stays pure machine-readable output
114 // (`grr gmail profile | jq` must not receive log lines).
115 .with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
116 .init();
117
118 let matches = root_command().get_matches();
119
120 // Parse dispatch: the static commands first (they are few and
121 // fixed); any other matched name is a generated service, which clap
122 // has already validated against the registered tree.
123 let Some((name, sub)) = matches.subcommand() else {
124 // clap enforces `subcommand_required`; this arm exists for the
125 // compiler, not for users.
126 anyhow::bail!("a subcommand is required; run `grr --help`");
127 };
128
129 // Self-migrating installs: if the skills manifest records a different grr
130 // version, run the smart pass before the requested command. This is
131 // LAZY — `skills::maybe_auto_migrate` reads the manifest and compares the
132 // version before hashing a single file, and does nothing at all when no
133 // manifest exists. Skipped for `skills` itself (it manages its own
134 // install/list) and for `completions`, which shells commonly run on
135 // every startup (`source <(grr completions zsh)`) and so must not write
136 // files or print to the terminal; unreachable for `--help`/`--version`,
137 // which clap handles and exits from inside `get_matches`. stdout stays
138 // pure: the one-line summary goes to stderr.
139 if !matches!(name, "skills" | "completions")
140 && let Some(summary) = skills::maybe_auto_migrate()
141 {
142 eprintln!("{summary}");
143 }
144
145 match name {
146 // The schema dump is pure clap introspection: it must answer with
147 // zero configuration, before any client (or OAuth) exists.
148 "schema" => {
149 let args = schema::SchemaArgs::from_arg_matches(sub)?;
150 schema::handle_schema_cmd(root_command(), args)?;
151 }
152
153 // Completion scripts are clap introspection too, so they need no
154 // configuration and answer before it is loaded.
155 "completions" => {
156 let args = completions::CompletionsArgs::from_arg_matches(sub)?;
157 completions::handle_completions_cmd(root_command(), args)?;
158 }
159
160 // The skills are compiled into the binary (`include_str!`), so
161 // install/list need zero configuration, no OAuth client, and no
162 // network — like `schema`, they answer before the config is loaded.
163 "skills" => {
164 skills::handle_skills_cmd(skills::SkillsCommands::from_arg_matches(sub)?)?;
165 }
166
167 // `auth setup` writes the OAuth client, so it too must run before
168 // the config is loaded — otherwise a user with no client could
169 // never run it.
170 "auth" => {
171 let cmd = AuthCommands::from_arg_matches(sub)?;
172 if let AuthCommands::Setup(args) = &cmd {
173 return crate::commands::setup::handle_setup(args.clone()).await;
174 }
175 let config = ConfigLoader::load().await?;
176 let auth = build_auth(&config).await?;
177 auth::handle_auth_cmd(&auth, cmd).await?;
178 }
179
180 // The transport probe needs an auth handle (it issues a real
181 // authenticated request) but no typed service client.
182 "transport" => {
183 let config = ConfigLoader::load().await?;
184 let auth = build_auth(&config).await?;
185 transport::handle_transport_cmd(auth, transport::TransportArgs::from_arg_matches(sub)?)
186 .await?;
187 }
188
189 "api" => {
190 let cmd = api::ApiCommands::from_arg_matches(sub)?;
191 // `api list` / `describe` / `refresh` are pure introspection over
192 // the embedded index: they must work with zero configuration —
193 // a fresh CI runner has no OAuth client, and these commands
194 // would otherwise die at the client gate before doing anything.
195 // `call` is credential-OPTIONAL the same way: --dry-run is a
196 // plan-only operation, and a real send with no client reports
197 // the actionable help from the call path.
198 if matches!(cmd, api::ApiCommands::Call(_)) {
199 let safety_args = safety::SafetyArgs::from_arg_matches(&matches)?;
200 let config = ConfigLoader::load().await?;
201 let auth = build_auth(&config).await.ok();
202 api::handle_api_cmd(
203 auth.as_ref(),
204 cmd,
205 &safety::SafetyProfile::from_args(&safety_args),
206 )
207 .await?;
208 } else {
209 // Permissive profile: list/describe/refresh cannot write.
210 api::handle_api_cmd(None, cmd, &safety::SafetyProfile::PERMISSIVE).await?;
211 }
212 }
213
214 // The MCP server is self-contained: it loads the config, builds its
215 // own auth handle, and serves the JSON-RPC stream itself.
216 "mcp" => {
217 let args = mcp::McpArgs::from_arg_matches(sub)?;
218 mcp::run_mcp(args).await?;
219 }
220
221 // The ask flow plans by default (no credential needed); --run
222 // builds the auth handle and executes through the shared call
223 // path. The safety gate fires as soon as the method is resolved.
224 "ask" => {
225 let args = ask::AskArgs::from_arg_matches(sub)?;
226 let config = ConfigLoader::load().await?;
227 let auth = if args.run {
228 Some(build_auth(&config).await?)
229 } else {
230 None
231 };
232 let safety_args = safety::SafetyArgs::from_arg_matches(&matches)?;
233 ask::handle_ask(
234 &config,
235 auth.as_ref(),
236 args,
237 &safety::SafetyProfile::from_args(&safety_args),
238 )
239 .await?;
240 }
241
242 // A generated service. Dispatch walks the matched chain itself
243 // (the deepest subcommand's name IS the full dotted method id),
244 // so it gets the full root matches. The safety profile comes from
245 // the global --readonly/--deny-service/--deny-verb flags.
246 //
247 // The credential is OPTIONAL: --dry-run plans never consult it
248 // (call_method requires it only when actually sending), so a fresh
249 // CI runner — no OAuth client, no config — can exercise the whole
250 // tree offline. A real send with no client gets the actionable
251 // NO_CLIENT_HELP from the call path.
252 _ => {
253 let safety_args = safety::SafetyArgs::from_arg_matches(&matches)?;
254 let config = ConfigLoader::load().await?;
255 let auth = build_auth(&config).await.ok();
256 gen_dispatch::dispatch_with_profile(
257 &matches,
258 auth.as_ref(),
259 &safety::SafetyProfile::from_args(&safety_args),
260 )
261 .await?;
262 }
263 }
264
265 Ok(())
266}