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

Load config from json file if present

vielle.dev df72cb91 5ddf431e

verified
+9
Cargo.lock
··· 3 3 version = 4 4 4 5 5 [[package]] 6 + name = "json" 7 + version = "0.12.4" 8 + source = "registry+https://github.com/rust-lang/crates.io-index" 9 + checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" 10 + 11 + [[package]] 6 12 name = "tangled-on-commit" 7 13 version = "0.1.0" 14 + dependencies = [ 15 + "json", 16 + ]
+1
Cargo.toml
··· 6 6 rust-version = "1.88" 7 7 8 8 [dependencies] 9 + json = "0.12.4"
+47 -6
src/main.rs
··· 95 95 } 96 96 } 97 97 98 - if let Some(ref handle) = handle && let Some(ref repo_name) = repo_name && let Some(ref shell) = shell { 98 + if let Some(ref handle) = handle 99 + && let Some(ref repo_name) = repo_name 100 + && let Some(ref shell) = shell 101 + { 102 + return Ok(Config { 103 + handle: handle.to_string(), 104 + repo_name: repo_name.to_string(), 105 + shell: shell.to_string(), 106 + }); 107 + } 108 + 109 + // now load config 110 + if let Ok(file) = std::fs::read_to_string("./tangled-on-commit.json") { 111 + if let Ok(parsed) = json::parse(&file) { 112 + if let Some(json_handle) = parsed["handle"].as_str() { 113 + handle = Some(String::from(json_handle)) 114 + } 115 + if let Some(json_repo_name) = parsed["repo_name"].as_str() { 116 + repo_name = Some(String::from(json_repo_name)) 117 + } 118 + if let Some(json_shell) = parsed["shell"].as_str() { 119 + shell = Some(String::from(json_shell)) 120 + } 121 + } 122 + } 123 + 124 + if let Some(ref handle) = handle 125 + && let Some(ref repo_name) = repo_name 126 + && let Some(ref shell) = shell 127 + { 99 128 return Ok(Config { 100 129 handle: handle.to_string(), 101 130 repo_name: repo_name.to_string(), ··· 103 132 }); 104 133 } 105 134 106 - return Ok(Config { 107 - handle: handle.unwrap_or(String::new()), 108 - repo_name: repo_name.unwrap_or(String::new()), 109 - shell: shell.unwrap_or(String::new()), 110 - }); 135 + // now load from env 136 + 137 + if let Some(ref handle) = handle 138 + && let Some(ref repo_name) = repo_name 139 + && let Some(ref shell) = shell 140 + { 141 + return Ok(Config { 142 + handle: handle.to_string(), 143 + repo_name: repo_name.to_string(), 144 + shell: shell.to_string(), 145 + }); 146 + } 147 + 148 + // couldnt resolve every value 149 + // print an error and quit 150 + println!("@{handle:?}/{repo_name:?}: {shell:?}"); 151 + return Err(()); 111 152 } 112 153 113 154 fn main() -> Result<(), ()> {
+5
tangled-on-commit.json
··· 1 + { 2 + "handle": "vielle.dev", 3 + "repo_name": "tangled-on-commit", 4 + "shell": "./commit.sh" 5 + }