diff options
| author | dev <dev@localhost> | 2026-11-20 18:34:26 +0000 |
|---|---|---|
| committer | dev <dev@localhost> | 2026-11-20 18:34:26 +0000 |
| commit | 895f3547043def08f0822f936ae34458315f6982 (patch) | |
| tree | 9e7b08db7225786f1d1ec5b312314801e3d09c90 /src/main.rs | |
| parent | bbae6c8e83cf145f9af472f69591bae804d03048 (diff) | |
| download | aur-build-895f3547043def08f0822f936ae34458315f6982.tar.gz aur-build-895f3547043def08f0822f936ae34458315f6982.zip | |
usecase: one implementation of each workflow, run by both front ends
`main` opened the repository inline before its command match. `serve` opened it
again, four calls in an order that matters. The web routes wrote subscriptions,
toggles, notices and build retries straight through `db::`, so the CLI and the
UI were two independent guesses at the same product, and the guesses had already
drifted: `sub_add` fell back to repo id 1 where the CLI reported the repository
as uninitialised, and `build_retry` remembered to write the audit row while
nothing else did.
`src/usecase/` is the workflow layer now — bootstrap, detect, reviews,
subscriptions, builds, maintenance. A function earns a place here by having more
than one step in an order that matters: `approve` records the decision *and*
queues the build it authorises, because an approval that silently fails to queue
is the exact failure the review queue exists to prevent, and both front ends had
to remember to pair them.
Nothing in the layer formats output or knows a status code exists. Where the two
front ends genuinely disagree, the result carries the disagreement instead of
resolving it: `Approved::queued` is a `Result` because a failed enqueue is fatal
in the CLI — an approved candidate with no build is a stall nobody notices — and
a soft failure in the UI, which has already committed the decision and can retry
from the package page.
`src/http/routes/mutations.rs` now contains no `db::` call at all, and
`main.rs` is 314 lines with no multi-step sequence and nothing exported.
Rank map: usecase 9, http 10, composition roots 11. The layer test enforces
that usecase cannot name http, which is why `serve` starts the scheduler and the
build pool but leaves the HTTP server to `main`.
One user-visible message moves: `aur-build reject <missing>` now says
`not found: candidate N` (the review layer's wording) instead of
`no candidate #N` (main's). Both exit 1; the check moved with the workflow.
280 passed / 0 failed, clippy -D warnings clean, architecture 8/8.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 141 |
1 files changed, 47 insertions, 94 deletions
diff --git a/src/main.rs b/src/main.rs index 1686680..118d06e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,8 @@ use clap::{Parser, Subcommand}; use aur_build::config::Config; use aur_build::db::{self, builds, model::*}; use aur_build::pipeline; -use aur_build::review::{approve as ap, candidates, diff}; +use aur_build::review::candidates; +use aur_build::usecase; #[derive(Parser)] #[command(name = "aur-build", version, about = "AUR build server: nothing is published that a human did not approve")] @@ -95,21 +96,10 @@ fn main() -> anyhow::Result<()> { if let Some(b) = bind { cfg.bind = b.clone(); } - cfg.ensure_dirs()?; - db::migrate(&cfg.db_path())?; - let db = db::Db::open_migrated(&cfg.db_path())?; - db::ensure_repo( - &db.writer, - &cfg.repo_name, - &cfg.repo_arch_dir().display().to_string(), - &cfg.arch, - &cfg.signing_key_id, - )?; - let _ = pipeline::publish::reconcile(&cfg, &db); - let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - aur_build::watch::scheduler::spawn_scheduler(std::sync::Arc::new(cfg.clone()), db.clone(), stop.clone()); - let _server = aur_build::http::spawn(cfg.clone(), db.clone())?; - let _pool = aur_build::pipeline::dispatch::Pool::spawn(std::sync::Arc::new(cfg.clone()), db.clone()); + let db = usecase::serve(&cfg)?; + // Starting the UI is the composition root's job: nothing below it may + // know the UI exists. + let _server = aur_build::http::spawn(cfg.clone(), db)?; println!("serving on http://{} — ctrl-c to stop", cfg.bind); loop { std::thread::sleep(std::time::Duration::from_secs(3600)); @@ -126,46 +116,26 @@ fn main() -> anyhow::Result<()> { if let Some(k) = signing_key { cfg.signing_key_id = k; } - cfg.save()?; - cfg.ensure_dirs()?; - db::migrate(&cfg.db_path())?; - let db = db::Db::open_migrated(&cfg.db_path())?; - let rid = db::ensure_repo( - &db.writer, - &cfg.repo_name, - &cfg.repo_arch_dir().display().to_string(), - &cfg.arch, - &cfg.signing_key_id, - )?; + let rid = usecase::init(&cfg)?; println!("initialized {} (repo id {rid})", cfg.root.display()); println!("add to pacman.conf:\n [{}]\n Server = {}/$repo/os/$arch", cfg.repo_name, cfg.base_url); Ok(()) } Cmd::Subscribe { pkgbase, url } => { - let rid = db::repo_id(&ctx.db)?; - let url = url.unwrap_or_else(|| format!("https://aur.archlinux.org/{pkgbase}.git")); - db::subscribe(&ctx.db.writer, rid, &pkgbase, &url)?; - println!("subscribed {pkgbase} ({url})"); + let s = usecase::subscribe(&ctx.db, &pkgbase, url)?; + println!("subscribed {} ({})", s.pkgbase, s.url); Ok(()) } Cmd::Unsubscribe { pkgbase } => { - db::set_subscription_state(&ctx.db.writer, &pkgbase, SubscriptionState::Removed)?; + usecase::unsubscribe(&ctx.db, &pkgbase)?; println!("unsubscribed {pkgbase}"); Ok(()) } Cmd::Detect { pkgbase } => { - let names: Vec<String> = match pkgbase { - Some(n) => vec![n], - None => db::list_subscriptions(&ctx.db)? - .into_iter() - .filter(|s| s.state == SubscriptionState::Active) - .map(|s| s.pkgbase) - .collect(), - }; - for n in names { - match aur_build::watch::detect::check_pkgbase(&cfg, &ctx.db, &n) { - Ok(r) => println!("{n}: {:?}", r.outcome), - Err(e) => eprintln!("{n}: {e}"), + for c in usecase::detect(&cfg, &ctx.db, pkgbase)? { + match c.outcome { + Ok(o) => println!("{}: {:?}", c.pkgbase, o), + Err(e) => eprintln!("{}: {e}", c.pkgbase), } } Ok(()) @@ -193,60 +163,59 @@ fn main() -> anyhow::Result<()> { Ok(()) } Cmd::Show { candidate } => { - let c = need_candidate(&ctx.db, candidate)?; - let (text, digest) = diff::render_current(&ctx.db, &cfg, &c)?; - candidates::set_diff_digest(&ctx.db, c.id, &digest)?; - candidates::mark_viewed(&ctx.db, c.id)?; - println!("{text}"); + let Some(p) = usecase::view_review(&cfg, &ctx.db, candidate)? else { + return Err(anyhow::anyhow!("no candidate #{candidate}")); + }; + println!("{}", p.text); println!("---"); - println!("identity_hash: {}", c.identity_hash); - println!("diff_digest: {digest}"); - println!("approve with: aur-build approve {} --yes", c.id); + println!("identity_hash: {}", p.candidate.identity_hash); + println!("diff_digest: {}", p.digest); + println!("approve with: aur-build approve {} --yes", p.candidate.id); Ok(()) } Cmd::Approve { candidate, yes, note, identity, diff: diff_arg } => { - let c = need_candidate(&ctx.db, candidate)?; - let (text, digest) = diff::render_current(&ctx.db, &cfg, &c)?; - candidates::set_diff_digest(&ctx.db, c.id, &digest)?; - let identity = identity.unwrap_or_else(|| c.identity_hash.clone()); - let diff_arg = diff_arg.unwrap_or(digest); - println!("{text}"); + let Some(p) = usecase::prepare_review(&cfg, &ctx.db, candidate)? else { + return Err(anyhow::anyhow!("no candidate #{candidate}")); + }; + let identity = identity.unwrap_or_else(|| p.candidate.identity_hash.clone()); + let diff_arg = diff_arg.unwrap_or(p.digest); + println!("{}", p.text); println!("---"); if !yes { println!("not approved: re-run with --yes to approve exactly this"); return Ok(()); } let who = whoami(); - match ap::approve(&ctx.db, &who, c.id, &identity, &diff_arg, note.as_deref()) { - Ok(a) => { - println!("approved #{} (approval {})", c.id, a.id); - let id = pipeline::enqueue_for_candidate(&cfg, &ctx.db, &c)?; + match usecase::approve(&cfg, &ctx.db, &who, &p.candidate, &identity, &diff_arg, note.as_deref())? { + usecase::Approval::Recorded(a) => { + println!("approved #{} (approval {})", p.candidate.id, a.approval.id); + let id = a.queued?; println!("queued build {id}"); println!("run it with: aur-build run {id}"); Ok(()) } - Err(ap::ApproveError::Stale(stale)) => { + usecase::Approval::Refused(stale) => { eprintln!("refused: {stale}"); std::process::exit(2) } - Err(ap::ApproveError::System(e)) => Err(e.into()), } } Cmd::Reject { candidate, note } => { - let c = need_candidate(&ctx.db, candidate)?; let who = whoami(); - ap::reject(&ctx.db, &who, c.id, ¬e)?; - println!("rejected #{}", c.id); + usecase::reject(&ctx.db, &who, candidate, ¬e)?; + println!("rejected #{candidate}"); Ok(()) } Cmd::Build { candidate } => { - let c = need_candidate(&ctx.db, candidate)?; - let id = pipeline::enqueue_for_candidate(&cfg, &ctx.db, &c)?; + let Some(c) = candidates::candidate_by_id(&ctx.db, candidate)? else { + return Err(anyhow::anyhow!("no candidate #{candidate}")); + }; + let id = usecase::enqueue_candidate(&cfg, &ctx.db, &c)?; println!("queued build {id}"); Ok(()) } Cmd::Rebuild { pkgbase } => { - let id = pipeline::enqueue_manual_rebuild(&cfg, &ctx.db, &pkgbase)?; + let id = usecase::manual_rebuild(&cfg, &ctx.db, &pkgbase)?; println!("queued rebuild {id}"); Ok(()) } @@ -297,21 +266,19 @@ fn main() -> anyhow::Result<()> { Ok(()) } Cmd::Gc { keep, dry_run } => { - let keep = keep.unwrap_or(aur_build::ops::DEFAULT_KEEP); if dry_run { - let (kept, evict) = aur_build::ops::plan_retention(&ctx.db, keep)?; - println!("retention: keep {keep} version(s) per pkgbase"); - println!(" {} release(s) keep their files", kept.len()); - for e in evict { - let files = aur_build::ops::release_files(&cfg, &ctx.db, e.release_id)?; - println!(" release {} would lose {} file(s)", e.release_id, files.len()); + let plan = usecase::plan_gc(&cfg, &ctx.db, keep)?; + println!("retention: keep {} version(s) per pkgbase", plan.keep); + println!(" {} release(s) keep their files", plan.kept); + for (release_id, files) in plan.evicting { + println!(" release {} would lose {} file(s)", release_id, files.len()); for f in files { println!(" {}", f.display()); } } return Ok(()); } - let r = aur_build::ops::retention(&cfg, &ctx.db, Some(keep))?; + let r = usecase::run_gc(&cfg, &ctx.db, keep)?; println!( "kept {} release(s), evicted {}, removed {} file(s), freed {} bytes, swept {} staging dir(s)", r.releases_kept, r.releases_evicted, r.files_removed, r.bytes_freed, r.staging_swept @@ -319,7 +286,7 @@ fn main() -> anyhow::Result<()> { Ok(()) } Cmd::Backup { to } => { - let path = aur_build::ops::backup(&ctx.db, &to)?; + let path = usecase::backup(&ctx.db, &to)?; println!( "wrote {} bytes to {} (VACUUM INTO, single file, no WAL)", std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0), @@ -336,17 +303,7 @@ struct Ctx { impl Ctx { fn open(cfg: &Config) -> anyhow::Result<Self> { - cfg.ensure_dirs()?; - db::migrate(&cfg.db_path())?; - let db = db::Db::open_migrated(&cfg.db_path())?; - db::ensure_repo( - &db.writer, - &cfg.repo_name, - &cfg.repo_arch_dir().display().to_string(), - &cfg.arch, - &cfg.signing_key_id, - )?; - Ok(Self { db }) + Ok(Self { db: usecase::open(cfg)? }) } } @@ -354,7 +311,3 @@ fn whoami() -> String { std::env::var("USER").or_else(|_| std::env::var("USERNAME")).unwrap_or_else(|_| "cli".into()) } -fn need_candidate(db: &db::Db, id: i64) -> anyhow::Result<CandidateRow> { - candidates::candidate_by_id(db, id)? - .ok_or_else(|| anyhow::anyhow!("no candidate #{id}")) -} |
