Skip to main content

cli_core/
ssh_key.rs

1//! Registering SSH keys over the `ssh-key-add` form.
2
3use forms::{FormValues, SSH_KEY_ADD};
4use rpc::Session;
5
6use crate::CommandError;
7use crate::flat::{Reply, submit};
8
9/// Register the public key line `key`, named `label` or by its comment, and
10/// return the id of the stored key.
11///
12/// # Errors
13/// [`CommandError::FormInvalid`] for a malformed or already registered key,
14/// or the server error.
15pub async fn add(
16    session: &mut Session,
17    key: &str,
18    label: Option<&str>,
19) -> Result<String, CommandError> {
20    let mut values = FormValues::default();
21    values.set("key", key);
22    label
23        .into_iter()
24        .for_each(|label| values.set("label", label));
25    match submit(session, &SSH_KEY_ADD, &values).await?.reply {
26        Reply::Entity(id) => Ok(id),
27        other => Err(CommandError::Command(format!(
28            "unexpected ssh-key reply: {other:?}"
29        ))),
30    }
31}