Skip to main content

hardpass/
lib.rs

1mod api;
2pub mod cli;
3mod cloud_init;
4mod images;
5mod instance;
6mod lock;
7mod ports;
8mod qemu;
9mod ssh;
10mod ssh_config;
11mod state;
12
13use anyhow::Result;
14
15pub use api::{Hardpass, Vm, VmSpec};
16use cli::{Args, Command, ImageCommand};
17use instance::InstanceManager;
18pub use instance::{VmInfo, VmSshInfo};
19pub use ssh::ExecOutput;
20use state::HardpassState;
21pub use state::{AccelMode, GuestArch, InstanceStatus, PortForward};
22
23pub async fn run(args: Args) -> Result<()> {
24    let state = HardpassState::load().await?;
25    let manager = InstanceManager::new(state);
26    if should_auto_configure_ssh(&args.command) {
27        manager.auto_configure_ssh_if_enabled().await;
28    }
29    match args.command {
30        Command::Doctor => manager.doctor().await,
31        Command::Image(args) => match args.command {
32            ImageCommand::Prefetch(args) => manager.prefetch_image(args).await,
33        },
34        Command::Create(args) => manager.create(args).await,
35        Command::Start(args) => manager.start(&args.name).await,
36        Command::Stop(args) => manager.stop(&args.name).await,
37        Command::Delete(args) => manager.delete(&args.name).await,
38        Command::List => manager.list().await,
39        Command::Info(args) => manager.info(&args.name, args.json).await,
40        Command::Ssh(args) => manager.ssh(&args.name, &args.ssh_args).await,
41        Command::Exec(args) => manager.exec(&args.name, &args.command).await,
42    }
43}
44
45fn should_auto_configure_ssh(command: &Command) -> bool {
46    !matches!(command, Command::Doctor | Command::Image(_))
47}