mod check;
#[cfg(test)]
mod command_tests;
mod handover;
#[cfg(test)]
mod handover_tests;
mod plan;
mod receipt;
#[cfg(test)]
mod tests;
mod version;
use crate::{clients::context::Context, config, home, shape, store::time::Stamp};
use check::{RELEASE_URL, cached, latest, stamp};
use clap::Args;
use handover::Stream;
use plan::plan;
use serde_json::{Value, json};
use std::{
io::{self, Write},
path::Path,
process::{Command, Stdio},
};
const MODES: [&str; 3] = ["off", "notify", "auto"];
#[derive(Args)]
pub struct Update {
#[arg(long)]
check: bool,
#[arg(long, conflicts_with = "check")]
write: bool,
#[arg(long)]
json: bool,
#[arg(long, value_parser = MODES)]
mode: Option<String>,
}
fn mode(root: &Path) -> String {
config::read(&root.join("config.toml"))
.ok()
.and_then(|doc| doc.get("update")?.get("mode")?.as_str().map(str::to_string))
.filter(|m| MODES.contains(&m.as_str()))
.unwrap_or("off".into())
}
fn apply(argv: &[String], err: &mut impl Write) -> io::Result<u8> {
let result = Command::new(&argv[0])
.args(&argv[1..])
.stdin(Stdio::null())
.output()?;
err.write_all(&result.stdout)?;
err.write_all(&result.stderr)?;
Ok(result
.status
.code()
.and_then(|c| c.try_into().ok())
.unwrap_or(1))
}
fn install(decision: &plan::Decision, context: &Context, err: &mut impl Write) -> io::Result<u8> {
match apply_and_verify(decision, context, err) {
Err(error) if error.kind() != io::ErrorKind::BrokenPipe => {
writeln!(err, "error: {error}")?;
Ok(1)
}
result => result,
}
}
fn apply_and_verify(
decision: &plan::Decision,
context: &Context,
err: &mut impl Write,
) -> io::Result<u8> {
let code = apply(&decision.argv, err)?;
if code != 0 {
return Ok(code);
}
let result = Command::new(&context.executable).arg("version").output()?;
let expected = format!("neosian v{}", decision.latest.as_deref().unwrap());
if !result.status.success() || String::from_utf8_lossy(&result.stdout).trim() != expected {
writeln!(
err,
"error: updater exited successfully but the executable does not report the checked release"
)?;
return Ok(1);
}
Ok(0)
}
pub fn run(args: &Update, out: &mut impl Write, err: &mut impl Write) -> io::Result<u8> {
match execute(args, out, err) {
Err(error) if error.kind() != io::ErrorKind::BrokenPipe => {
if args.json {
writeln!(out, "{}", json!({"error": error.to_string(), "hint": null}))?;
}
writeln!(err, "error: {error}")?;
Ok(1)
}
result => result,
}
}
fn execute(args: &Update, out: &mut impl Write, err: &mut impl Write) -> io::Result<u8> {
let root = home::home()?;
if let Some(mode) = &args.mode {
let path = root.join("config.toml");
let mut doc = config::read(&path)?;
let section = doc
.entry("update")
.or_insert_with(|| toml::Value::Table(toml::Table::new()));
if !section.is_table() {
*section = toml::Value::Table(toml::Table::new());
}
section
.as_table_mut()
.unwrap()
.insert("mode".into(), toml::Value::String(mode.clone()));
config::write(&path, &doc)?;
if args.json {
writeln!(out, "{}", json!({"mode": mode}))?;
} else {
writeln!(out, "update mode {mode}")?;
}
return Ok(0);
}
let context = Context::new()?;
let mode = mode(&root);
let latest = latest(RELEASE_URL);
if let Some(latest) = &latest {
stamp(&root, latest, Stamp::now())?;
}
complete(args, &context, latest.as_ref(), &mode, out, err)
}
fn complete(
args: &Update,
context: &Context,
latest: Option<&version::Version>,
mode: &str,
out: &mut impl Write,
err: &mut impl Write,
) -> io::Result<u8> {
let decision = plan(crate::version::VERSION, latest, context, !args.check);
if !args.json {
writeln!(out, "{}", decision.line)?;
out.flush()?;
}
let mut code = u8::from(matches!(decision.action, "refuse" | "unreachable"));
let mut refresh = Value::Null;
if decision.action == "apply" {
code = install(&decision, context, err)?;
}
if !args.check && code == 0 && matches!(decision.action, "apply" | "current") {
(code, refresh) = handover::refresh(
&context.executable,
args.json,
&mut |stream, bytes| match stream {
Stream::Out => out.write_all(bytes).and_then(|()| out.flush()),
Stream::Err => err.write_all(bytes),
},
)?;
if decision.action == "apply" && !args.json {
writeln!(out, "{}", installed(&decision))?;
}
}
if args.json {
writeln!(
out,
"{}",
json!({"current": crate::version::VERSION, "latest": decision.latest, "shape": shape::kind(&context.env), "mode": mode, "action": decision.action, "line": decision.line, "refresh": refresh})
)?;
}
Ok(code)
}
fn installed(decision: &plan::Decision) -> String {
format!(
"neosian {} is installed; restart running clients so they use it.",
decision.latest.as_deref().unwrap_or("-")
)
}
pub fn human_door(verb: &str, terminal: bool, json: bool) -> bool {
terminal && !json && verb == "status"
}
pub fn human_check(err: &mut impl Write) -> io::Result<Option<u8>> {
let root = home::home()?;
let mode = mode(&root);
if mode == "off" {
return Ok(None);
}
let now = Stamp::now();
let latest = cached(&root, now).or_else(|| {
let version = latest(RELEASE_URL)?;
let _ = stamp(&root, &version, now);
Some(version)
});
let Some(latest) = latest else {
return Ok(None);
};
let context = Context::new()?;
let decision = plan(
crate::version::VERSION,
Some(&latest),
&context,
mode == "auto",
);
if decision.action != "current" {
writeln!(err, "{}", decision.line)?;
}
if decision.action == "apply" {
let code = install(&decision, &context, err)?;
if code != 0 {
return Ok(Some(code));
}
handover::refresh(&context.executable, false, &mut |_, bytes| {
err.write_all(bytes)
})?;
writeln!(err, "{}", installed(&decision))?;
let status = Command::new(context.executable)
.args(std::env::args_os().skip(1))
.status()?;
return Ok(Some(
status.code().and_then(|c| c.try_into().ok()).unwrap_or(1),
));
}
Ok(None)
}