use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
use clap::{Parser, Subcommand};
use agentvfs::commands::{self, Output};
use agentvfs::error::VfsError;
#[derive(Parser)]
#[command(name = "avfs")]
#[command(version, about = "Virtual filesystem CLI backed by embedded databases")]
struct Cli {
#[arg(long, global = true)]
vault: Option<String>,
#[arg(long, global = true)]
json: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Vault(commands::vault::VaultArgs),
Ls(commands::ls::LsArgs),
Cat(commands::cat::CatArgs),
Checkpoint(commands::checkpoint::CheckpointArgs),
Write(commands::write::WriteArgs),
Mkdir(commands::mkdir::MkdirArgs),
Rm(commands::rm::RmArgs),
Cp(commands::cp::CpArgs),
Mv(commands::mv::MvArgs),
Tree(commands::tree::TreeArgs),
Log(commands::log::LogArgs),
Checkout(commands::checkout::CheckoutArgs),
Revert(commands::revert::RevertArgs),
Diff(commands::diff::DiffArgs),
Search(commands::search::SearchArgs),
Grep(commands::grep::GrepArgs),
Find(commands::find::FindArgs),
Tag(commands::tag::TagArgs),
Untag(commands::untag::UntagArgs),
Meta(commands::meta::MetaArgs),
Import(commands::import::ImportArgs),
Export(commands::export::ExportArgs),
Exec(commands::exec::ExecArgs),
Stats(commands::stats::StatsArgs),
Prune(commands::prune::PruneArgs),
Gc(commands::gc::GcArgs),
Compact(commands::compact::CompactArgs),
Maintain(commands::maintain::MaintainArgs),
Quota(commands::quota::QuotaArgs),
Audit(commands::audit::AuditArgs),
Snapshot(commands::snapshot::SnapshotArgs),
Shell(commands::shell::ShellArgs),
Aliases(commands::aliases::AliasesArgs),
#[cfg(feature = "fuse")]
Mount(commands::mount::MountArgs),
#[cfg(feature = "fuse")]
Unmount(commands::unmount::UnmountArgs),
#[cfg(feature = "fuse")]
Proxy(commands::proxy::ProxyArgs),
Pwd,
}
fn main() {
let cli = Cli::parse();
let output = Output::new(cli.json);
let result = match cli.command {
Commands::Vault(args) => commands::vault::run(args, &output),
Commands::Ls(args) => commands::ls::run(args, &output, cli.vault),
Commands::Cat(args) => commands::cat::run(args, &output, cli.vault),
Commands::Checkpoint(args) => commands::checkpoint::run(args, &output, cli.vault),
Commands::Write(args) => commands::write::run(args, &output, cli.vault),
Commands::Mkdir(args) => commands::mkdir::run(args, &output, cli.vault),
Commands::Rm(args) => commands::rm::run(args, &output, cli.vault),
Commands::Cp(args) => commands::cp::run(args, &output, cli.vault),
Commands::Mv(args) => commands::mv::run(args, &output, cli.vault),
Commands::Tree(args) => commands::tree::run(args, &output, cli.vault),
Commands::Log(args) => commands::log::run(args, &output, cli.vault),
Commands::Checkout(args) => commands::checkout::run(args, &output, cli.vault),
Commands::Revert(args) => commands::revert::run(args, &output, cli.vault),
Commands::Diff(args) => commands::diff::run(args, &output, cli.vault),
Commands::Search(args) => commands::search::run(args, &output, cli.vault),
Commands::Grep(args) => commands::grep::run(args, &output, cli.vault),
Commands::Find(args) => commands::find::run(args, &output, cli.vault),
Commands::Tag(args) => commands::tag::run(args, &output, cli.vault),
Commands::Untag(args) => commands::untag::run(args, &output, cli.vault),
Commands::Meta(args) => commands::meta::run(args, &output, cli.vault),
Commands::Import(args) => commands::import::run(args, &output, cli.vault),
Commands::Export(args) => commands::export::run(args, &output, cli.vault),
Commands::Exec(args) => commands::exec::run(args, &output, cli.vault),
Commands::Stats(args) => commands::stats::run(args, &output, cli.vault),
Commands::Prune(args) => commands::prune::run(args, &output, cli.vault),
Commands::Gc(args) => commands::gc::run(args, &output, cli.vault),
Commands::Compact(args) => commands::compact::run(args, &output, cli.vault),
Commands::Maintain(args) => commands::maintain::run(args, &output, cli.vault),
Commands::Quota(args) => commands::quota::run(args, &output, cli.vault),
Commands::Audit(args) => commands::audit::run(args, &output, cli.vault),
Commands::Snapshot(args) => commands::snapshot::run(args, &output, cli.vault),
Commands::Shell(args) => commands::shell::run(args, &output),
Commands::Aliases(args) => commands::aliases::run(args, &output),
#[cfg(feature = "fuse")]
Commands::Mount(args) => commands::mount::run(args, &output),
#[cfg(feature = "fuse")]
Commands::Unmount(args) => commands::unmount::run(args, &output),
#[cfg(feature = "fuse")]
Commands::Proxy(args) => commands::proxy::run(args, &output, cli.vault),
Commands::Pwd => {
if cli.json {
output.print_json(&serde_json::json!({"path": "/"}));
} else {
println!("/");
}
Ok(())
}
};
if let Err(e) = result {
match e {
VfsError::ExitStatus(code) => std::process::exit(normalize_exit_code(code)),
other => {
output.print_error(&other);
std::process::exit(1);
}
}
}
}
fn normalize_exit_code(code: i32) -> i32 {
if code > 0 { code } else { 1 }
}