Lines
81.25 %
Functions
50 %
Branches
100 %
//! Registering SSH keys over the `ssh-key-add` form.
use forms::{FormValues, SSH_KEY_ADD};
use rpc::Session;
use crate::CommandError;
use crate::flat::{Reply, submit};
/// Register the public key line `key`, named `label` or by its comment, and
/// return the id of the stored key.
///
/// # Errors
/// [`CommandError::FormInvalid`] for a malformed or already registered key,
/// or the server error.
pub async fn add(
session: &mut Session,
key: &str,
label: Option<&str>,
) -> Result<String, CommandError> {
let mut values = FormValues::default();
values.set("key", key);
label
.into_iter()
.for_each(|label| values.set("label", label));
match submit(session, &SSH_KEY_ADD, &values).await?.reply {
Reply::Entity(id) => Ok(id),
other => Err(CommandError::Command(format!(
"unexpected ssh-key reply: {other:?}"
))),
}