use dialoguer::Input;
use std::io::{self, IsTerminal};
use crate::cli::output;
use crate::core::config::Config;
use crate::core::domain::Identity;
use crate::error::Result;
pub fn execute(name: Option<String>) -> Result<()> {
if !Identity::has_global()? {
output::error("no identity found");
output::hint("run: dugout setup");
return Err(
crate::error::StoreError::NoPrivateKey("~/.dugout/identity".to_string()).into(),
);
}
if Config::exists() {
let config = Config::load()?;
let pubkey = Identity::load_global_pubkey()?;
if config.recipients.values().any(|k| k == &pubkey) {
output::warn("you already have access");
return Ok(());
}
}
let name = if let Some(n) = name {
n
} else if io::stdin().is_terminal() {
Input::new()
.with_prompt("What's your name?")
.interact_text()?
} else {
output::error("name required in non-interactive mode");
return Err(crate::error::ValidationError::EmptyKey.into());
};
let pubkey = Identity::load_global_pubkey()?;
std::fs::create_dir_all(".dugout/requests")?;
let request_path = format!(".dugout/requests/{}.pub", name);
std::fs::write(&request_path, format!("{}\n", pubkey))?;
output::success("created access request");
output::hint(&format!("share {} with an admin", request_path));
Ok(())
}