From 145bae1ca5347d38f53bfba0e934ea6cd73fac46 Mon Sep 17 00:00:00 2001 From: afterlifepro Date: Fri, 5 Sep 2025 18:12:30 +0100 Subject: [PATCH] Extract cli arguments --- Cargo.toml | 1 + src/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7b401a..ad2c32a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,6 @@ name = "tangled-on-commit" version = "0.1.0" edition = "2024" license = "GPL-3.0-or-later" +rust-version = "1.88" [dependencies] diff --git a/src/main.rs b/src/main.rs index 317dd85..3c54276 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,15 +23,78 @@ fn load_config() -> Result { } } + // if 0 args are passed, skip + // if 1 arg is passed, its the shell command + // if 2 args are passed: + // if arg 1 starts in @ its a handle + // if it contains a / the contents after the slash is the reponame + // else its the reponame + // arg 2 is always the shell command + // if 3 args are passed its handle repo shell + // if more args are passed its an error + + let mut handle: Option = None; + let mut repo_name: Option = None; + let mut shell: Option = None; + match std::env::args().collect::>().len() { + // 0 args (std env args includes this script) + 1 => {} + 2 => { + shell = Some(std::env::args().last().expect("Invalid state: 2 `Some` std::env::args() but found no Some values")) + } + 3 => { + // load args and consume first + let mut args = std::env::args(); + args.next(); + + if let Some(val) = args.next() { + if val.starts_with("@") { + if val.contains("/") { + let entries: Vec<_> = val.split("/").collect(); + if entries.len() != 2 { + return Err(()); + } + handle = Some(entries[0][1..].to_string()); + repo_name = Some(entries[1].to_string()); + } else { + handle = Some(val[1..].to_string()); + } + } else { + repo_name = Some(val) + }; + } + shell = Some(args.next().expect("Invalid state: 3 `Some` std::env::args() but only found 2")); + } + 4 => { + // load args and consume first + let mut args = std::env::args(); + args.next(); + + handle = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 1")); + repo_name = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 2")); + shell = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 3")); + } + _ => { + // err + } + } + + if let Some(ref handle) = handle && let Some(ref repo_name) = repo_name && let Some(ref shell) = shell { + return Ok(Config { + handle: handle.to_string(), + repo_name: repo_name.to_string(), + shell: shell.to_string(), + }); + } + return Ok(Config { - handle: String::from(""), - repo_name: String::from(""), - shell: String::from("") + handle: handle.unwrap_or(String::new()), + repo_name: repo_name.unwrap_or(String::new()), + shell: shell.unwrap_or(String::new()), }); } fn main() -> Result<(), ()> { - // load configuration let config = match load_config() { Ok(res) => res, -- knotmirror 0.0.1