add a struct for storing identities, construct it when importing and save

This commit is contained in:
kalmenn 2026-02-02 09:02:11 +01:00
parent a844ec48a4
commit 76fe594896
Signed by: kalmenn
GPG key ID: F500055C44BC3834
5 changed files with 169 additions and 20 deletions

View file

@ -25,17 +25,27 @@ pub fn main(_global_cli: crate::cli::GlobalArgs, cli: crate::cli::ImportCli) ->
.to_str()
.expect("this should always be valid UTF-8");
let identity_name = dialoguer::Input::<String>::with_theme(&*THEME)
let alias = dialoguer::Input::<String>::with_theme(&*THEME)
.with_prompt("What should this identity be named?")
.interact_text()?;
let authkey: Option<PathBuf> = select_authkey(&mut ctx, &key, &identity_name)?;
let authkey: Option<PathBuf> = select_authkey(&mut ctx, &key, &alias)?;
let (name, email) = select_name_and_email(&key)?;
info!(
"Imported identity: [{identity_name}] {name:?} <{email:?}> (signing key: {fingerprint}, authentication key: {authkey:?})"
);
let identity = crate::data::Identity {
alias,
data: crate::data::IdentityData {
name,
email,
sigkey: Some(fingerprint.to_owned()),
authkey,
},
};
info!("Imported identity {identity}");
identity.save_interactively()?;
Ok(())
}
@ -207,6 +217,17 @@ fn select_authkey(
) -> anyhow::Result<Option<PathBuf>> {
let mut authkey_prompt = dialoguer::Input::<String>::with_theme(&*THEME)
.with_prompt("Select an SSH key (leave empty for none)")
.validate_with(|input: &String| -> Result<(), Cow<str>> {
if input.is_empty() {
return Ok(());
}
match std::fs::exists(input) {
Ok(true) => Ok(()),
Ok(false) => Err(Cow::Borrowed("file not found")),
Err(err) => Err(Cow::Owned(err.to_string())),
}
})
.allow_empty(true);
if let Some(authkey) = try_export_as_ssh_key(ctx, key, identity_name)? {
@ -248,7 +269,7 @@ fn try_export_as_ssh_key(
}
let default_destination = {
let mut path = crate::DATA_FOLDER.clone();
let mut path = crate::data::DATA_FOLDER.clone();
path.push("ssh");
path.push(format!("{identity_name}.pub"));
path