From 866aaf740e10fcfce169aaf81380ea0257fbe272 Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Thu, 21 May 2026 20:19:54 +0300 Subject: refactor: separate server client boundaries --- src/cli/workspace.rs | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/cli/workspace.rs (limited to 'src/cli/workspace.rs') diff --git a/src/cli/workspace.rs b/src/cli/workspace.rs new file mode 100644 index 00000000..ae29792c --- /dev/null +++ b/src/cli/workspace.rs @@ -0,0 +1,163 @@ +use crate::api::schema::{ + EmptyParams, Method, Request, WorkspaceCreateParams, WorkspaceRenameParams, WorkspaceTarget, +}; + +pub(super) fn run_workspace_command(args: &[String]) -> std::io::Result { + let Some(subcommand) = args.first().map(|arg| arg.as_str()) else { + print_workspace_help(); + return Ok(2); + }; + + match subcommand { + "list" => workspace_list(&args[1..]), + "create" => workspace_create(&args[1..]), + "get" => workspace_get(&args[1..]), + "focus" => workspace_focus(&args[1..]), + "rename" => workspace_rename(&args[1..]), + "close" => workspace_close(&args[1..]), + "help" | "--help" | "-h" => { + print_workspace_help(); + Ok(0) + } + _ => { + print_workspace_help(); + Ok(2) + } + } +} + +fn workspace_list(args: &[String]) -> std::io::Result { + if !args.is_empty() { + eprintln!("usage: herdr workspace list"); + return Ok(2); + } + + super::print_response(&super::send_request(&Request { + id: "cli:workspace:list".into(), + method: Method::WorkspaceList(EmptyParams::default()), + })?) +} + +fn workspace_create(args: &[String]) -> std::io::Result { + let mut cwd = None; + let mut focus = false; + let mut label = None; + + let mut index = 0; + while index < args.len() { + match args[index].as_str() { + "--cwd" => { + let Some(value) = args.get(index + 1) else { + eprintln!("missing value for --cwd"); + return Ok(2); + }; + cwd = Some(value.clone()); + index += 2; + } + "--label" => { + let Some(value) = args.get(index + 1) else { + eprintln!("missing value for --label"); + return Ok(2); + }; + label = Some(value.clone()); + index += 2; + } + "--focus" => { + focus = true; + index += 1; + } + "--no-focus" => { + focus = false; + index += 1; + } + other => { + eprintln!("unknown option: {other}"); + return Ok(2); + } + } + } + + super::print_response(&super::send_request(&Request { + id: "cli:workspace:create".into(), + method: Method::WorkspaceCreate(WorkspaceCreateParams { cwd, focus, label }), + })?) +} + +fn workspace_get(args: &[String]) -> std::io::Result { + let Some(raw_workspace_id) = args.first() else { + eprintln!("usage: herdr workspace get "); + return Ok(2); + }; + if args.len() != 1 { + eprintln!("usage: herdr workspace get "); + return Ok(2); + } + + super::print_response(&super::send_request(&Request { + id: "cli:workspace:get".into(), + method: Method::WorkspaceGet(WorkspaceTarget { + workspace_id: super::normalize_workspace_id(raw_workspace_id), + }), + })?) +} + +fn workspace_focus(args: &[String]) -> std::io::Result { + let Some(raw_workspace_id) = args.first() else { + eprintln!("usage: herdr workspace focus "); + return Ok(2); + }; + if args.len() != 1 { + eprintln!("usage: herdr workspace focus "); + return Ok(2); + } + + super::print_response(&super::send_request(&Request { + id: "cli:workspace:focus".into(), + method: Method::WorkspaceFocus(WorkspaceTarget { + workspace_id: super::normalize_workspace_id(raw_workspace_id), + }), + })?) +} + +fn workspace_rename(args: &[String]) -> std::io::Result { + if args.len() < 2 { + eprintln!("usage: herdr workspace rename