Skip to content

Commit f4eabb1

Browse files
committed
Move port CLI parsing into config
1 parent 3b993e7 commit f4eabb1

2 files changed

Lines changed: 71 additions & 39 deletions

File tree

anycode-backend/src/config.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Result;
12
use serde::{Deserialize, Serialize};
23
use std::{fmt::format, path::Path};
34

@@ -146,6 +147,75 @@ pub struct Terminal {
146147
pub command: String,
147148
}
148149

150+
const DEFAULT_PORT: u16 = 3000;
151+
152+
fn print_help() {
153+
println!("anycode - Code editor server");
154+
println!();
155+
println!("USAGE:");
156+
println!(" anycode [OPTIONS]");
157+
println!();
158+
println!("OPTIONS:");
159+
println!(" -h, --help Print help information");
160+
println!(" --version Print version information");
161+
println!(" -p, --port <PORT> Port to listen on");
162+
println!();
163+
println!("ENVIRONMENT:");
164+
println!(" ANYCODE_PORT Port to listen on (default: 3000)");
165+
println!(" ANYCODE_HOME Path to configuration directory");
166+
println!(" ANYCODE_ACP_PERMISSION_MODE ACP permission mode: full_access (default) or ask");
167+
println!();
168+
println!("Start the anycode server. The server will be available at http://localhost:<port>");
169+
}
170+
171+
fn parse_port(value: &str, source: &str) -> Result<u16> {
172+
value
173+
.parse::<u16>()
174+
.map_err(|_| anyhow::anyhow!("Invalid {source}: {value}"))
175+
}
176+
177+
pub fn resolve_server_port() -> Result<u16> {
178+
let mut args = std::env::args().skip(1);
179+
let mut cli_port: Option<u16> = None;
180+
181+
while let Some(arg) = args.next() {
182+
match arg.as_str() {
183+
"--help" | "-h" => {
184+
print_help();
185+
std::process::exit(0);
186+
}
187+
"--version" | "-V" => {
188+
println!("anycode {}", env!("CARGO_PKG_VERSION"));
189+
std::process::exit(0);
190+
}
191+
"--port" | "-p" => {
192+
let value = args
193+
.next()
194+
.ok_or_else(|| anyhow::anyhow!("Missing value for {arg}"))?;
195+
cli_port = Some(parse_port(&value, "CLI port")?);
196+
}
197+
_ if arg.starts_with("--port=") => {
198+
cli_port = Some(parse_port(&arg["--port=".len()..], "CLI port")?);
199+
}
200+
_ if arg.starts_with('-') => {
201+
anyhow::bail!("Unknown option: {arg}");
202+
}
203+
_ => {
204+
anyhow::bail!("Unknown positional argument: {arg}");
205+
}
206+
}
207+
}
208+
209+
if let Some(port) = cli_port {
210+
return Ok(port);
211+
}
212+
213+
match std::env::var("ANYCODE_PORT") {
214+
Ok(value) => parse_port(&value, "ANYCODE_PORT"),
215+
Err(_) => Ok(DEFAULT_PORT),
216+
}
217+
}
218+
149219
#[cfg(test)]
150220
mod congif_tests {
151221
use super::*;

anycode-backend/src/main.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -229,46 +229,8 @@ async fn not_found() -> Response {
229229
(StatusCode::NOT_FOUND, "404").into_response()
230230
}
231231

232-
fn print_help() {
233-
println!("anycode - Code editor server");
234-
println!();
235-
println!("USAGE:");
236-
println!(" anycode [OPTIONS]");
237-
println!();
238-
println!("OPTIONS:");
239-
println!(" -h, --help Print help information");
240-
println!(" --version Print version information");
241-
println!();
242-
println!("ENVIRONMENT:");
243-
println!(" ANYCODE_PORT Port to listen on (default: 3000)");
244-
println!(" ANYCODE_HOME Path to configuration directory");
245-
println!(" ANYCODE_ACP_PERMISSION_MODE ACP permission mode: full_access (default) or ask");
246-
println!();
247-
println!("Start the anycode server. The server will be available at http://localhost:<port>");
248-
}
249-
250232
#[tokio::main]
251233
async fn main() -> Result<()> {
252-
let args: Vec<String> = std::env::args().collect();
253-
254-
if args.len() > 1 {
255-
match args[1].as_str() {
256-
"--help" | "-h" => {
257-
print_help();
258-
return Ok(());
259-
}
260-
"--version" | "-V" => {
261-
println!("anycode {}", env!("CARGO_PKG_VERSION"));
262-
return Ok(());
263-
}
264-
_ => {
265-
eprintln!("Unknown option: {}", args[1]);
266-
eprintln!("Run 'anycode --help' for usage information.");
267-
std::process::exit(1);
268-
}
269-
}
270-
}
271-
272234
tracing_subscriber::fmt()
273235
.with_env_filter(tracing_subscriber::EnvFilter::new("info"))
274236
.init();
@@ -344,7 +306,7 @@ async fn main() -> Result<()> {
344306
.with_state(io.clone())
345307
.layer(cors);
346308

347-
let port = std::env::var("ANYCODE_PORT").unwrap_or("3000".to_string());
309+
let port = crate::config::resolve_server_port()?;
348310
let url = format!("0.0.0.0:{}", port);
349311
let listener = tokio::net::TcpListener::bind(url).await?;
350312

0 commit comments

Comments
 (0)