nomoreide_cli/lib.rs
1//! The `nomoreide` command, as a library.
2//!
3//! This is a library so that the `nomoreide` crate — the name people type
4//! at `cargo install` — can be a real front door rather than a duplicate.
5//! It has nothing to offer as an API beyond [`run`]; the modules below stay
6//! private, and the binary in this crate is itself a three-line caller.
7//!
8//! The `nomoreide` binary's front door — the Rust half of `src/index.ts`.
9//!
10//! Dispatch order matters and mirrors the reference exactly. In particular
11//! `start` is overloaded: with no service name it is the MCP server (the shape
12//! an agent launches), and with one it is a service start. An agent config
13//! that says `nomoreide start` has to keep working.
14
15mod agents;
16mod commands;
17mod daemon_cli;
18mod database;
19mod flags;
20mod git;
21mod linear;
22mod profile;
23mod remote;
24mod setup;
25mod tui;
26
27use std::process::ExitCode;
28
29use nomoreide_daemon_client::{resolve_daemon_port, RuntimePaths};
30
31/// Run the command line, returning the process exit code.
32///
33/// Async rather than blocking, and deliberately not wrapped in a runtime:
34/// each binary supplies its own `#[tokio::main]`, so neither has to know
35/// that the other exists.
36pub async fn run() -> ExitCode {
37 let args: Vec<String> = std::env::args().skip(1).collect();
38 // No deprecation notice here. The reference prints one on an interactive
39 // stderr telling the reader to install *this* binary; having installed it,
40 // there is nothing left to say.
41 let command = args.first().map(String::as_str).unwrap_or("mcp");
42 let paths = RuntimePaths::default();
43 let configured_port =
44 resolve_daemon_port(std::env::var("NOMOREIDE_DAEMON_PORT").ok().as_deref());
45
46 let code = match command {
47 "setup" => setup::run(&args[1..]),
48 // Bare `start` is the MCP server: it is what an agent spawns, and it
49 // predates the service-runtime meaning of the word.
50 "mcp" | "start" if args.len() <= 1 => match nomoreide_mcp::run_stdio().await {
51 Ok(()) => 0,
52 Err(error) => {
53 eprintln!("nomoreide: MCP server failed: {error}");
54 1
55 }
56 },
57 "daemon" => {
58 let rest = &args[1..];
59 let port = daemon_cli::port_flag(rest).unwrap_or(configured_port);
60 if rest.iter().any(|arg| !arg.starts_with("--")) {
61 daemon_cli::run(rest, &paths, port).await
62 } else {
63 return run_foreground_daemon(port).await;
64 }
65 }
66 "web" => web(&paths, configured_port).await,
67 "tui" => match tui::run(&paths, configured_port).await {
68 Ok(()) => 0,
69 Err(error) => {
70 if let Some(message) = error.message_text() {
71 eprintln!("{message}");
72 }
73 error.exit_code()
74 }
75 },
76 // Not a user-facing command: the daemon spawns this inside a terminal
77 // window it opened, handing it the socket and the one-shot token that
78 // authorise the attachment. Named with a `__` prefix for that reason,
79 // and absent from every usage string.
80 // The shared terminal launcher runs inside either this CLI daemon or
81 // the Tauri executable. Tauri historically used the flag spelling;
82 // accept both spellings here so the daemon's current executable can
83 // always attach the Terminal.app window it just opened.
84 command if is_terminal_attach_command(command) => {
85 let socket = args.get(1).map(String::as_str).unwrap_or("");
86 let token = args.get(2).map(String::as_str).unwrap_or("");
87 match nomoreide_core::external_terminal::run_attach(socket, token) {
88 Ok(()) => 0,
89 Err(error) => {
90 eprintln!("{error}");
91 1
92 }
93 }
94 }
95 _ => commands::run(&args, &paths, configured_port).await,
96 };
97 ExitCode::from(code)
98}
99
100fn is_terminal_attach_command(command: &str) -> bool {
101 matches!(command, "__terminal-attach" | "--terminal-attach")
102}
103
104/// `nomoreide daemon` with no subcommand: be the machine-global daemon.
105///
106/// Split out because it never returns normally — everything else in `main`
107/// produces an exit code, and this produces a process that lives until a
108/// signal or `/api/daemon/shutdown`.
109async fn run_foreground_daemon(port: u16) -> ExitCode {
110 let options = nomoreide_daemon::DaemonOptions {
111 port,
112 ..Default::default()
113 };
114 match nomoreide_daemon::run(options).await {
115 Ok(()) => ExitCode::SUCCESS,
116 Err(error) => {
117 eprintln!("nomoreide: daemon failed: {error:#}");
118 ExitCode::FAILURE
119 }
120 }
121}
122
123/// `nomoreide web [--port=N]` — make sure a daemon is up and say where it is.
124///
125/// Both lines go to **stderr**, including the URL. That looks wrong until you
126/// remember what calls this: a shell function that opens the browser, and a
127/// wrapper that wants the URL out of band from whatever the daemon then logs.
128/// The reference puts both there, so both stay there.
129async fn web(paths: &RuntimePaths, configured_port: u16) -> u8 {
130 let port = std::env::args()
131 .find_map(|arg| {
132 arg.strip_prefix("--port=")
133 .map(|value| resolve_daemon_port(Some(value)))
134 })
135 .unwrap_or(configured_port);
136 match nomoreide_daemon_client::ensure_daemon(paths, port, env!("CARGO_PKG_VERSION")).await {
137 Ok(daemon) => {
138 if let Some(warning) = &daemon.version_warning {
139 eprintln!("{warning}");
140 }
141 // Said out loud because it costs a second of startup. Silence would
142 // be a pause with no cause, which reads as the tool being slow.
143 if daemon.status == nomoreide_daemon_client::EnsureStatus::Upgraded {
144 eprintln!(
145 "NoMoreIDE daemon updated to v{}.",
146 env!("CARGO_PKG_VERSION")
147 );
148 }
149 eprintln!(
150 "NoMoreIDE web UI: http://127.0.0.1:{}",
151 daemon.endpoint.port()
152 );
153 0
154 }
155 Err(error) => {
156 eprintln!("{error}");
157 1
158 }
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use super::is_terminal_attach_command;
165
166 #[test]
167 fn terminal_attachment_accepts_the_shared_launchers_flag() {
168 assert!(is_terminal_attach_command("--terminal-attach"));
169 assert!(is_terminal_attach_command("__terminal-attach"));
170 assert!(!is_terminal_attach_command("terminal-attach"));
171 }
172}