From c26d67a25b920e67af9d72c3122a268d11e4563c Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Mon, 20 Nov 2023 18:23:35 -0600 Subject: [PATCH] main: Factor out get_sshca_server_url function The `get_sshca_server_url` function encapsulates the logic of identifying the URL of the SSHCA server. For now, it only considers the `SSHCA_SERVER` environment variable, but eventually, it will also support other configuration methods like a configuration file. Moving this to a separate function will allow other areas of the code to share the same logic. --- src/main.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3d09fa5..c2f425e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,15 +96,7 @@ async fn host_cmd(args: HostArgs) -> MainResult { } async fn sign_key(args: SignArgs) -> MainResult { - let url = match std::env::var("SSHCA_SERVER") { - Ok(v) => v, - Err(std::env::VarError::NotPresent) => { - return Err("SSHCA_SERVER environment variable is not set".into()); - } - Err(std::env::VarError::NotUnicode(_)) => { - return Err("SSHCA_SERVER environment variable is invalid".into()); - } - }; + let url = get_sshca_server_url()?; let Some(hostname) = get_hostname() else { return Err("Hostname must be valid UTF-8".into()); }; @@ -175,6 +167,18 @@ async fn sign_key(args: SignArgs) -> MainResult { Ok(()) } +fn get_sshca_server_url() -> Result { + match std::env::var("SSHCA_SERVER") { + Ok(v) => Ok(v), + Err(std::env::VarError::NotPresent) => { + Err("SSHCA_SERVER environment variable is not set".into()) + } + Err(std::env::VarError::NotUnicode(_)) => { + Err("SSHCA_SERVER environment variable is invalid".into()) + } + } +} + fn get_hostname() -> Option { gethostname::gethostname().into_string().ok() }