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.
dev/auto-reload
Dustin 2023-11-20 18:23:35 -06:00
parent 1d0e558163
commit c26d67a25b
1 changed files with 13 additions and 9 deletions

View File

@ -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<String, String> {
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<String> {
gethostname::gethostname().into_string().ok()
}