Listen to git commits for a specific repo and run a shell command

Extract cli arguments

vielle.dev 145bae1c 81487e13

verified
Changed files
+68 -4
src
+1
Cargo.toml
··· 3 3 version = "0.1.0" 4 4 edition = "2024" 5 5 license = "GPL-3.0-or-later" 6 + rust-version = "1.88" 6 7 7 8 [dependencies]
+67 -4
src/main.rs
··· 23 23 } 24 24 } 25 25 26 + // if 0 args are passed, skip 27 + // if 1 arg is passed, its the shell command 28 + // if 2 args are passed: 29 + // if arg 1 starts in @ its a handle 30 + // if it contains a / the contents after the slash is the reponame 31 + // else its the reponame 32 + // arg 2 is always the shell command 33 + // if 3 args are passed its handle repo shell 34 + // if more args are passed its an error 35 + 36 + let mut handle: Option<String> = None; 37 + let mut repo_name: Option<String> = None; 38 + let mut shell: Option<String> = None; 39 + match std::env::args().collect::<Vec<_>>().len() { 40 + // 0 args (std env args includes this script) 41 + 1 => {} 42 + 2 => { 43 + shell = Some(std::env::args().last().expect("Invalid state: 2 `Some` std::env::args() but found no Some values")) 44 + } 45 + 3 => { 46 + // load args and consume first 47 + let mut args = std::env::args(); 48 + args.next(); 49 + 50 + if let Some(val) = args.next() { 51 + if val.starts_with("@") { 52 + if val.contains("/") { 53 + let entries: Vec<_> = val.split("/").collect(); 54 + if entries.len() != 2 { 55 + return Err(()); 56 + } 57 + handle = Some(entries[0][1..].to_string()); 58 + repo_name = Some(entries[1].to_string()); 59 + } else { 60 + handle = Some(val[1..].to_string()); 61 + } 62 + } else { 63 + repo_name = Some(val) 64 + }; 65 + } 66 + shell = Some(args.next().expect("Invalid state: 3 `Some` std::env::args() but only found 2")); 67 + } 68 + 4 => { 69 + // load args and consume first 70 + let mut args = std::env::args(); 71 + args.next(); 72 + 73 + handle = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 1")); 74 + repo_name = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 2")); 75 + shell = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 3")); 76 + } 77 + _ => { 78 + // err 79 + } 80 + } 81 + 82 + if let Some(ref handle) = handle && let Some(ref repo_name) = repo_name && let Some(ref shell) = shell { 83 + return Ok(Config { 84 + handle: handle.to_string(), 85 + repo_name: repo_name.to_string(), 86 + shell: shell.to_string(), 87 + }); 88 + } 89 + 26 90 return Ok(Config { 27 - handle: String::from(""), 28 - repo_name: String::from(""), 29 - shell: String::from("") 91 + handle: handle.unwrap_or(String::new()), 92 + repo_name: repo_name.unwrap_or(String::new()), 93 + shell: shell.unwrap_or(String::new()), 30 94 }); 31 95 } 32 96 33 97 fn main() -> Result<(), ()> { 34 - 35 98 // load configuration 36 99 let config = match load_config() { 37 100 Ok(res) => res,