Skip to main content

cli/
ssh_key.rs

1//! The `ssh-key add` command over the `ssh-key-add` form.
2
3use cli_core::{CommandError, ssh_key};
4use rpc::Session;
5
6/// The key line from the file or the inline argument.
7fn key_text(key_file: Option<String>, public_key: Option<String>) -> Result<String, CommandError> {
8    match (key_file, public_key) {
9        (Some(path), None) => std::fs::read_to_string(&path)
10            .map_err(|e| CommandError::Argument(format!("ssh-key file {path}: {e}"))),
11        (None, Some(line)) => Ok(line),
12        _ => Err(CommandError::Argument(
13            "either --key-file or --public-key is required".to_string(),
14        )),
15    }
16}
17
18/// Registers the key and prints the id of the stored one.
19pub async fn add(
20    session: &mut Session,
21    key_file: Option<String>,
22    public_key: Option<String>,
23    annotation: Option<String>,
24) -> Result<(), CommandError> {
25    let key = key_text(key_file, public_key)?;
26    println!(
27        "{}",
28        ssh_key::add(session, &key, annotation.as_deref()).await?
29    );
30    Ok(())
31}
32
33#[cfg(test)]
34mod tests;