don't
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(knot): adjust start and hook handling

Signed-off-by: tjh <x@tjh.dev>

tjh eec509f0 da878338

+37 -30
+1 -1
crates/knot/src/hooks.rs
··· 57 57 } 58 58 59 59 #[tracing::instrument] 60 - pub async fn post_hook(hook: &Hook) -> anyhow::Result<()> { 60 + pub async fn run_hook(hook: &Hook) -> anyhow::Result<()> { 61 61 let mut environment_vars: HashMap<_, _> = env::vars().collect(); 62 62 63 63 // Take the environment variables we need to post the hook to the internal API.
+21 -29
crates/knot/src/main.rs
··· 17 17 use std::{ 18 18 collections::BTreeMap, 19 19 env, 20 - ffi::{OsStr, OsString}, 20 + ffi::OsStr, 21 21 net::ToSocketAddrs, 22 - path::PathBuf, 23 22 sync::{Arc, atomic::AtomicU64}, 24 23 time::Duration, 25 24 }; ··· 40 41 #[global_allocator] 41 42 static GLOBAL: Jemalloc = Jemalloc; 42 43 43 - fn main() { 44 - use knot::private::Hook; 45 - 46 - let stderr_layer = tracing_subscriber::fmt::layer().with_writer(std::io::stderr); 44 + fn main() -> anyhow::Result<()> { 47 45 tracing_subscriber::registry() 48 46 .with( 49 47 EnvFilter::builder() 50 48 .with_default_directive(LevelFilter::INFO.into()) 51 49 .from_env_lossy(), 52 50 ) 53 - .with(stderr_layer) 54 - .try_init() 55 - .unwrap(); 51 + .with( 52 + tracing_subscriber::fmt::layer() 53 + .with_writer(std::io::stderr) 54 + .with_file(true) 55 + .with_line_number(true), 56 + ) 57 + .init(); 56 58 57 59 let runtime = Builder::new_current_thread() 58 60 .enable_all() 59 61 .build() 60 62 .expect("Failed to build runtime"); 61 63 62 - let name = executable_name().expect("Executable should have a name"); 63 - match Hook::try_from(name.as_os_str()) { 64 - Ok(hook) => runtime.block_on(hooks::post_hook(&hook)).unwrap(), 65 - Err(_) => { 66 - let arguments = cli::parse(); 67 - tracing::debug!(?arguments); 68 - runtime.block_on(run(arguments)).unwrap(); 64 + if let Ok(hook) = ().try_into() { 65 + match runtime.block_on(hooks::run_hook(&hook)) { 66 + Ok(()) => std::process::exit(0), 67 + Err(error) => { 68 + tracing::error!(?error, "failed to run hook"); 69 + std::process::exit(1); 70 + } 69 71 } 70 72 } 71 - } 72 73 73 - fn executable_name() -> anyhow::Result<OsString> { 74 - Ok(env::args_os() 75 - .next() 76 - .map(PathBuf::from) 77 - .ok_or(anyhow::anyhow!("Expected at least one argument"))? 78 - .file_name() 79 - .ok_or(anyhow::anyhow!( 80 - "Failed to extract file name from executable" 81 - ))? 82 - .to_os_string()) 74 + let arguments = cli::parse(); 75 + tracing::debug!(?arguments); 76 + runtime.block_on(knot_main(arguments)) 83 77 } 84 78 85 79 const X_REQUEST_ID: HeaderName = HeaderName::from_static("x-request-id"); ··· 105 113 .and_then(|hv| std::str::from_utf8(hv.as_bytes()).ok()) 106 114 } 107 115 108 - pub async fn run(arguments: cli::Arguments) -> anyhow::Result<()> { 116 + pub async fn knot_main(arguments: cli::Arguments) -> anyhow::Result<()> { 109 117 unsafe { env::set_var("GIT_CONFIG_GLOBAL", &arguments.git_config) }; 110 118 111 119 hooks::setup_global_hooks(&arguments.hooks)?; 112 120 assert!(git_config_global_set("core.hooksPath", &arguments.hooks)?); 113 121 assert!(git_config_global_set( 114 122 "receive.advertisePushOptions", 115 - &match arguments.require_signed_push { 123 + match arguments.require_signed_push { 116 124 true => "true", 117 125 false => "false", 118 126 }
+15
crates/knot/src/private.rs
··· 163 163 } 164 164 } 165 165 166 + impl TryFrom<()> for Hook { 167 + type Error = UnknownHook; 168 + /// Extract the hook from the invoked executable name. 169 + fn try_from(_: ()) -> Result<Self, Self::Error> { 170 + fn executable_name() -> Option<std::ffi::OsString> { 171 + use std::path::Path; 172 + std::env::args_os() 173 + .next() 174 + .and_then(|arg| Path::new(&arg).file_name().map(ToOwned::to_owned)) 175 + } 176 + 177 + Hook::try_from(executable_name().ok_or(UnknownHook)?.as_os_str()) 178 + } 179 + } 180 + 166 181 /// Extracts the 'X-Gordian-User-Did' header from a request. 167 182 pub struct GordianUserDid(pub OwnedDid); 168 183