1
//! Registering SSH keys over the `ssh-key-add` form.
2

            
3
use forms::{FormValues, SSH_KEY_ADD};
4
use rpc::Session;
5

            
6
use crate::CommandError;
7
use 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.
15
36
pub async fn add(
16
36
    session: &mut Session,
17
36
    key: &str,
18
36
    label: Option<&str>,
19
36
) -> Result<String, CommandError> {
20
36
    let mut values = FormValues::default();
21
36
    values.set("key", key);
22
36
    label
23
36
        .into_iter()
24
12
        .for_each(|label| values.set("label", label));
25
36
    match submit(session, &SSH_KEY_ADD, &values).await?.reply {
26
24
        Reply::Entity(id) => Ok(id),
27
        other => Err(CommandError::Command(format!(
28
            "unexpected ssh-key reply: {other:?}"
29
        ))),
30
    }
31
36
}