//! Non-interactive SSH command handlers. //! //! When a user runs `ssh cli.makenot.work `, the exec_request handler //! dispatches to this module. All commands write output to a byte buffer and //! return it for the SSH channel. use crate::api::{MnwApiClient, UserInfo}; use crate::format; /// Execute a non-interactive command and return the output bytes. pub async fn execute( command_line: &str, user: &UserInfo, api: &MnwApiClient, ) -> Vec { let parts: Vec<&str> = command_line.split_whitespace().collect(); if parts.is_empty() { return help_text(); } let json = parts.contains(&"--json"); let parts: Vec<&str> = parts.into_iter().filter(|p| *p != "--json").collect(); match parts[0] { "projects" => cmd_projects(user, api, json).await, "analytics" => { let range = parts .iter() .find_map(|p| p.strip_prefix("--range=")) .unwrap_or("30d"); cmd_analytics(user, api, range, json).await } "transactions" => cmd_transactions(user, api, json).await, "export" if parts.get(1) == Some(&"sales") => cmd_export_sales(user, api).await, "promo" => match parts.get(1).copied() { Some("list") => cmd_promo_list(user, api, json).await, Some("create") => { let code = parts.get(2).unwrap_or(&""); let pct = parts.get(3).unwrap_or(&"0"); cmd_promo_create(user, api, code, pct).await } _ => b"Usage: promo list | promo create CODE DISCOUNT_PCT\r\n".to_vec(), }, "blog" => match parts.get(1).copied() { Some("list") => { let slug = parts.get(2).unwrap_or(&""); cmd_blog_list(user, api, slug, json).await } _ => b"Usage: blog list \r\n".to_vec(), }, "help" | "--help" | "-h" => help_text(), other => format!("Unknown command: {other}\r\nRun without arguments for usage help.\r\n") .into_bytes(), } } async fn cmd_projects(user: &UserInfo, api: &MnwApiClient, json: bool) -> Vec { match api.get_projects(&user.user_id).await { Ok(projects) => { if json { return serde_json::to_vec_pretty(&projects).unwrap_or_default(); } if projects.is_empty() { return b"No projects.\r\n".to_vec(); } let mut out = format!( "{:<30} {:<12} {:<8} {:<6} {:<10}\r\n", "Title", "Type", "Status", "Items", "Revenue" ); out.push_str(&"-".repeat(70)); out.push_str("\r\n"); for p in &projects { let status = if p.is_public { "public" } else { "draft" }; let revenue = format::format_cents(p.revenue_cents); out.push_str(&format!( "{:<30} {:<12} {:<8} {:<6} {:<10}\r\n", truncate(&p.title, 29), p.project_type, status, p.item_count, revenue, )); } out.into_bytes() } Err(e) => format!("Error: {e}\r\n").into_bytes(), } } async fn cmd_analytics( user: &UserInfo, api: &MnwApiClient, range: &str, json: bool, ) -> Vec { match api.get_analytics(&user.user_id, range).await { Ok(data) => { if json { return serde_json::to_vec_pretty(&data).unwrap_or_default(); } let mut out = String::new(); out.push_str(&format!("Analytics ({range})\r\n\r\n")); let rev = format::format_cents(data.current_revenue_cents); let prev_rev = format::format_cents(data.previous_revenue_cents); out.push_str(&format!("Revenue: {rev} (prev: {prev_rev})\r\n")); out.push_str(&format!( "Sales: {} (prev: {})\r\n", data.current_sales, data.previous_sales )); out.push_str(&format!( "Followers: {} (prev: {})\r\n", data.current_followers, data.previous_followers )); if !data.top_projects.is_empty() { out.push_str("\r\nTop Projects:\r\n"); for p in &data.top_projects { out.push_str(&format!( " {:<30} {}\r\n", truncate(&p.title, 29), format::format_cents(p.revenue_cents) )); } } out.into_bytes() } Err(e) => format!("Error: {e}\r\n").into_bytes(), } } async fn cmd_transactions(user: &UserInfo, api: &MnwApiClient, json: bool) -> Vec { match api.get_transactions(&user.user_id).await { Ok(txs) => { if json { return serde_json::to_vec_pretty(&txs).unwrap_or_default(); } if txs.is_empty() { return b"No transactions.\r\n".to_vec(); } let mut out = format!( "{:<30} {:<10} {:<12} {:<12}\r\n", "Item", "Amount", "Status", "Date" ); out.push_str(&"-".repeat(66)); out.push_str("\r\n"); for tx in &txs { let title = tx.item_title.as_deref().unwrap_or("--"); let amount = format::format_cents(tx.amount_cents as i64); let date = tx.created_at.get(..10).unwrap_or(&tx.created_at); out.push_str(&format!( "{:<30} {:<10} {:<12} {:<12}\r\n", truncate(title, 29), amount, tx.status, date, )); } out.into_bytes() } Err(e) => format!("Error: {e}\r\n").into_bytes(), } } async fn cmd_export_sales(user: &UserInfo, api: &MnwApiClient) -> Vec { match api.export_sales_csv(&user.user_id).await { Ok(result) => result.csv.into_bytes(), Err(e) => format!("Error: {e}\r\n").into_bytes(), } } async fn cmd_promo_list(user: &UserInfo, api: &MnwApiClient, json: bool) -> Vec { match api.list_promo_codes(&user.user_id).await { Ok(codes) => { if json { return serde_json::to_vec_pretty(&codes).unwrap_or_default(); } if codes.is_empty() { return b"No promo codes.\r\n".to_vec(); } let mut out = format!( "{:<20} {:<12} {:<20} {:<10}\r\n", "Code", "Discount", "Scope", "Uses" ); out.push_str(&"-".repeat(64)); out.push_str("\r\n"); for c in &codes { let discount = match (c.discount_type.as_deref(), c.discount_value) { (Some("percentage"), Some(v)) => format!("{}% off", v), (Some("fixed"), Some(v)) => format!("${}.{:02} off", v / 100, v % 100), _ => "Free".to_string(), }; let scope = c .item_title .as_deref() .or(c.project_title.as_deref()) .unwrap_or("All items"); let uses = match c.max_uses { Some(max) => format!("{}/{}", c.use_count, max), None => c.use_count.to_string(), }; out.push_str(&format!( "{:<20} {:<12} {:<20} {:<10}\r\n", truncate(&c.code, 19), discount, truncate(scope, 19), uses, )); } out.into_bytes() } Err(e) => format!("Error: {e}\r\n").into_bytes(), } } async fn cmd_promo_create( user: &UserInfo, api: &MnwApiClient, code: &str, pct: &str, ) -> Vec { if code.is_empty() { return b"Usage: promo create CODE DISCOUNT_PCT\r\n".to_vec(); } let discount: i32 = pct.parse().unwrap_or(0); match api .create_promo_code(&user.user_id, code, "percentage", discount, None, None) .await { Ok(_) => format!("Created promo code: {code} ({discount}% off)\r\n").into_bytes(), Err(e) => format!("Error: {e}\r\n").into_bytes(), } } async fn cmd_blog_list( user: &UserInfo, api: &MnwApiClient, slug: &str, json: bool, ) -> Vec { if slug.is_empty() { return b"Usage: blog list \r\n".to_vec(); } // Find the project by slug let projects = match api.get_projects(&user.user_id).await { Ok(p) => p, Err(e) => return format!("Error: {e}\r\n").into_bytes(), }; let Some(project) = projects.iter().find(|p| p.slug == slug) else { return format!("Project not found: {slug}\r\n").into_bytes(); }; match api.list_blog_posts(&user.user_id, &project.id).await { Ok(posts) => { if json { return serde_json::to_vec_pretty(&posts).unwrap_or_default(); } if posts.is_empty() { return b"No blog posts.\r\n".to_vec(); } let mut out = format!( "{:<30} {:<20} {:<10} {:<12}\r\n", "Title", "Slug", "Status", "Created" ); out.push_str(&"-".repeat(74)); out.push_str("\r\n"); for p in &posts { let status = if p.is_published { "published" } else { "draft" }; let date = p.created_at.get(..10).unwrap_or(&p.created_at); out.push_str(&format!( "{:<30} {:<20} {:<10} {:<12}\r\n", truncate(&p.title, 29), truncate(&p.slug, 19), status, date, )); } out.into_bytes() } Err(e) => format!("Error: {e}\r\n").into_bytes(), } } fn help_text() -> Vec { b"Usage: ssh cli.makenot.work \r\n\ \r\n\ Commands:\r\n\ \x20 projects List your projects\r\n\ \x20 analytics [--range=N] Revenue stats (7d/30d/90d/all)\r\n\ \x20 transactions Recent transactions\r\n\ \x20 export sales Export sales as CSV\r\n\ \x20 promo list List promo codes\r\n\ \x20 promo create CODE PCT Create a promo code\r\n\ \x20 blog list SLUG List blog posts for project\r\n\ \r\n\ Add --json to any command for machine-readable output.\r\n" .to_vec() } fn truncate(s: &str, max_len: usize) -> &str { if s.len() <= max_len { s } else { &s[..s.floor_char_boundary(max_len)] } } #[cfg(test)] mod tests { use super::*; #[test] fn truncate_short_string() { assert_eq!(truncate("hello", 10), "hello"); } #[test] fn truncate_exact_length() { assert_eq!(truncate("hello", 5), "hello"); } #[test] fn truncate_long_string() { assert_eq!(truncate("hello world", 5), "hello"); } #[test] fn truncate_multibyte_utf8() { // "café" is 5 bytes (é = 2 bytes), truncating at 4 should not panic let result = truncate("café", 4); assert_eq!(result, "caf"); } #[test] fn truncate_emoji() { // Each emoji is 4 bytes let result = truncate("🎵🎶🎸", 5); assert_eq!(result, "🎵"); } }