use anyhow::{Result, bail};
use crate::exit::Code;
use crate::home::Home;
#[cfg(windows)]
use super::run::windows::account as platform;
#[cfg(not(windows))]
mod platform {
use anyhow::{Result, bail};
use crate::exit::Code;
use crate::home::Home;
const NOT_WINDOWS: &str = "`openmoat agent-account` is for Windows: `openmoat run` here \
needs no account, its sandbox holds reads and the network already (docs/SANDBOX.md)";
pub fn supported() -> Result<()> {
bail!("{NOT_WINDOWS}")
}
pub fn require_elevated(_: &str) -> Result<()> {
supported()
}
pub fn setup(_: &Home) -> Result<Vec<String>> {
supported().map(|()| Vec::new())
}
pub fn status(_: &Home) -> Result<Code> {
supported().map(|()| Code::Ok)
}
pub fn remove(_: &Home) -> Result<Option<Vec<String>>> {
supported().map(|()| None)
}
}
fn person(command: &str) -> Result<Home> {
platform::supported()?;
crate::terminal::refuse_agent(command)?;
if !crate::terminal::interactive() {
bail!("`{command}` must be run by a person in a terminal, not from a hook or script");
}
platform::require_elevated(command)?;
let home = Home::locate()?;
home.require_exists()?;
Ok(home)
}
pub fn setup() -> Result<Code> {
const COMMAND: &str = "openmoat agent-account setup";
let home = person(COMMAND)?;
let pass = crate::gate::pass(&home, COMMAND)?;
let details = platform::setup(&home)?;
pass.record(&home, "agent account set up (ADR-129)", &details);
Ok(Code::Ok)
}
pub fn status() -> Result<Code> {
platform::supported()?;
platform::status(&Home::locate()?)
}
pub fn remove() -> Result<Code> {
const COMMAND: &str = "openmoat agent-account remove";
let home = person(COMMAND)?;
let pass = crate::gate::pass(&home, COMMAND)?;
if let Some(details) = platform::remove(&home)? {
pass.record(&home, "agent account removed (ADR-129)", &details);
}
Ok(Code::Ok)
}