Lines
100 %
Functions
50 %
Branches
//! The `ssh-key add` command over the `ssh-key-add` form.
use cli_core::{CommandError, ssh_key};
use rpc::Session;
/// The key line from the file or the inline argument.
fn key_text(key_file: Option<String>, public_key: Option<String>) -> Result<String, CommandError> {
match (key_file, public_key) {
(Some(path), None) => std::fs::read_to_string(&path)
.map_err(|e| CommandError::Argument(format!("ssh-key file {path}: {e}"))),
(None, Some(line)) => Ok(line),
_ => Err(CommandError::Argument(
"either --key-file or --public-key is required".to_string(),
)),
}
/// Registers the key and prints the id of the stored one.
pub async fn add(
session: &mut Session,
key_file: Option<String>,
public_key: Option<String>,
annotation: Option<String>,
) -> Result<(), CommandError> {
let key = key_text(key_file, public_key)?;
println!(
"{}",
ssh_key::add(session, &key, annotation.as_deref()).await?
);
Ok(())
#[cfg(test)]
mod tests;