use kernel::Maturity;
use sprawling::release;
use sprawling::release::Built;
use std::process::ExitCode;
use wire::ReleaseAnswer;
pub(super) fn headline(maturity: Maturity) -> String {
format!(
"sprawling {} ({}){}",
env!("CARGO_PKG_VERSION"),
maturity.word(),
cut()
)
}
fn cut() -> String {
match release::built() {
Ok(Built::Released(mine)) => format!(", released {}", mine.released()),
Ok(Built::FromSource) => String::from(", built from source"),
Err(err) => format!(", carrying a tag that does not decode: {}", err.recovery()),
}
}
pub(super) fn check() -> ExitCode {
println!();
match release::answer() {
ReleaseAnswer::Refused { refusal } => {
eprintln!("could not check: {refusal}");
eprintln!("recovery: {}", refusal.recovery());
ExitCode::FAILURE
}
ReleaseAnswer::Unreleased { newest } => {
println!("this binary was built from source, so it is none of the published releases.");
println!(
"newest published: {}, cut {}",
newest.version, newest.released
);
ExitCode::SUCCESS
}
ReleaseAnswer::Stands {
mine,
newest,
verdict,
} => {
match verdict {
kernel::ReleaseVerdict::Current => println!(
"{} is the newest release published. Nothing to do.",
mine.version
),
kernel::ReleaseVerdict::Behind => {
println!(
"a newer release is published: {}, cut {} (this binary is {}).",
newest.version, newest.released, mine.version
);
println!();
println!(" npm bunx sprawling@latest up");
println!(" archive download it, then run `sprawling install` again:");
println!(" https://github.com/2youg1/sprawling-agents/releases");
println!();
println!(
"Nothing was downloaded and nothing was changed. \
Updating is yours to run."
);
}
kernel::ReleaseVerdict::Ahead => println!(
"this binary ({}) is newer than the newest on npm ({}); \
the packages are published after the release page.",
mine.version, newest.version
),
}
ExitCode::SUCCESS
}
}
}
#[cfg(test)]
mod tests {
use super::{cut, headline};
use kernel::Maturity;
#[test]
fn the_status_line_says_the_maturity_it_is_given() {
assert_eq!(
headline(Maturity::Alpha),
format!("sprawling {} (alpha){}", env!("CARGO_PKG_VERSION"), cut())
);
}
}