use std::io::{self, Write};
use std::path::PathBuf;
use clap::{Args, Subcommand};
use crate::knowledge::input::read;
use crate::knowledge::model::{MAX_BODY, invalid};
use crate::memory::dispatch::Memory;
use crate::memory::grammar::Flags;
use crate::memory::mounts::Refusal;
use crate::store::error::Error;
#[derive(Args)]
pub struct Target {
path: String,
#[command(flatten)]
flags: Flags,
}
#[derive(Args)]
pub struct Selection {
path: String,
#[arg(long)]
write: bool,
#[command(flatten)]
flags: Flags,
}
#[derive(Subcommand)]
pub enum Command {
Discover {
#[command(flatten)]
flags: Flags,
},
Load(Target),
Inspect(Target),
Validate(Target),
Import {
input: PathBuf,
#[arg(long)]
to: String,
#[arg(long)]
replace: bool,
#[arg(long)]
write: bool,
#[command(flatten)]
flags: Flags,
},
Export {
path: String,
#[arg(long)]
output: PathBuf,
#[command(flatten)]
flags: Flags,
},
Edit {
path: String,
#[arg(long)]
file: PathBuf,
#[arg(long, value_parser = clap::value_parser!(u64).range(1..))]
expect_version: u64,
#[arg(long)]
write: bool,
#[command(flatten)]
flags: Flags,
},
Diff {
path: String,
#[arg(long, value_parser = clap::value_parser!(u64).range(1..))]
from: u64,
#[arg(long, value_parser = clap::value_parser!(u64).range(1..))]
to: Option<u64>,
#[command(flatten)]
flags: Flags,
},
Revert {
path: String,
#[arg(long, value_parser = clap::value_parser!(u64).range(1..))]
version: u64,
#[arg(long, value_parser = clap::value_parser!(u64).range(1..))]
expect_version: u64,
#[arg(long)]
write: bool,
#[command(flatten)]
flags: Flags,
},
Select(Selection),
Unselect(Selection),
Pin(Selection),
Unpin(Selection),
}
impl Command {
fn flags(&self) -> &Flags {
match self {
Self::Discover { flags }
| Self::Import { flags, .. }
| Self::Export { flags, .. }
| Self::Edit { flags, .. }
| Self::Diff { flags, .. }
| Self::Revert { flags, .. } => flags,
Self::Load(t) | Self::Inspect(t) | Self::Validate(t) => &t.flags,
Self::Select(t) | Self::Unselect(t) | Self::Pin(t) | Self::Unpin(t) => &t.flags,
}
}
}
pub fn run(command: &Command, out: &mut impl Write, err: &mut impl Write) -> io::Result<u8> {
let flags = command.flags();
let result = (|| -> Result<u8, Error> {
let local = match command {
Command::Select(s) => Some((s, "select")),
Command::Unselect(s) => Some((s, "unselect")),
Command::Unpin(s) => Some((s, "unpin")),
_ => None,
};
if let Some((s, action)) = local {
flags.local_profile().map_err(invalid)?;
let (report, code) =
super::profile::change(flags.profile.as_deref(), &s.path, action, s.write, None)?;
writeln!(out, "{report}")?;
return Ok(code);
}
let input = match command {
Command::Import { input, to, .. } => Some(super::package::load(input, to)?),
_ => None,
};
let edited = match command {
Command::Edit { file, .. } => Some(read(file, MAX_BODY)?),
_ => None,
};
let cwd = std::env::current_dir()?;
let selection = flags.select(&cwd).map_err(invalid)?;
let (mounts, actor) = flags
.selected_settings(&cwd, &selection)
.map_err(|r| match r {
Refusal::Usage(s) => invalid(s),
Refusal::Memory(e) => Error::Memory(e),
})?;
let memory = Memory {
store: selection.open()?,
mounts,
actor,
};
let (report, code) = match command {
Command::Discover { .. } => super::discover(&memory)?,
Command::Load(t) => super::load(&memory, &t.path)?,
Command::Inspect(t) => super::inspect(&memory, &t.path, false)?,
Command::Validate(t) => super::inspect(&memory, &t.path, true)?,
Command::Import {
to, replace, write, ..
} => super::package::import(
&memory,
to,
input.expect("prepared package"),
*replace,
*write,
)?,
Command::Export { path, output, .. } => super::package::export(&memory, path, output)?,
Command::Edit {
path,
expect_version,
write,
..
} => super::revision::edit(
&memory,
path,
edited.as_deref(),
None,
*expect_version,
*write,
)?,
Command::Diff { path, from, to, .. } => {
super::revision::diff(&memory, path, *from, *to)?
}
Command::Revert {
path,
version,
expect_version,
write,
..
} => {
super::revision::edit(&memory, path, None, Some(*version), *expect_version, *write)?
}
Command::Pin(s) => super::profile::change(
flags.profile.as_deref(),
&s.path,
"pin",
s.write,
Some(super::profile::pin(&memory, &s.path)?),
)?,
_ => unreachable!("local profile changes handled above"),
};
writeln!(out, "{report}")?;
Ok(code)
})();
crate::memory::operate::settle(result, flags.json, out, err)
}