diff --git a/src/cli/command.rs b/src/cli/command.rs new file mode 100644 index 0000000..4699a69 --- /dev/null +++ b/src/cli/command.rs @@ -0,0 +1,13 @@ +use super::Exit; + +/// A subcommand that keel can run. +pub trait Command +{ + fn run(&self) -> anyhow::Result; + + /// The exit status used when `run` returns an error. + /// + /// Hook subcommands return `Exit::Ok` here, so an + /// internal error never aborts a pacman transaction. + fn on_error(&self) -> Exit { Exit::Error } +} diff --git a/src/cli/commands/ack.rs b/src/cli/commands/ack.rs new file mode 100644 index 0000000..e7ae150 --- /dev/null +++ b/src/cli/commands/ack.rs @@ -0,0 +1,11 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Clear a failed state without rolling back. +#[derive(Debug, Args)] +pub struct Ack; + +impl Command for Ack { + fn run(&self) -> anyhow::Result { todo!("keel ack") } +} diff --git a/src/cli/commands/cache.rs b/src/cli/commands/cache.rs new file mode 100644 index 0000000..c131cce --- /dev/null +++ b/src/cli/commands/cache.rs @@ -0,0 +1,41 @@ +use clap::{ Args, Subcommand }; + +use crate::cli::{ Exit, Command }; + +/// Manage the ELF symbol index. +#[derive(Debug, Args)] +pub struct Cache { + #[command(subcommand)] + pub action: CacheAction, +} + +#[derive(Debug, Subcommand)] +pub enum CacheAction { + Prune(Prune), +} + +impl CacheAction +{ + pub fn command(&self) -> &dyn Command + { + match self { + Self::Prune(c) => c, + } + } +} + +impl Command for Cache +{ + fn run(&self) -> anyhow::Result { self.action.command().run() } + + fn on_error(&self) -> Exit { self.action.command().on_error() } +} + +/// Drop index entries for package versions that are no longer installed. +#[derive(Debug, Args)] +pub struct Prune; + +impl Command for Prune +{ + fn run(&self) -> anyhow::Result { todo!("keel cache prune") } +} diff --git a/src/cli/commands/check.rs b/src/cli/commands/check.rs new file mode 100644 index 0000000..4fc2955 --- /dev/null +++ b/src/cli/commands/check.rs @@ -0,0 +1,16 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Run the pre-transaction checks as if a full system upgrade ran now. +#[derive(Debug, Args)] +pub struct Check { + /// Download the package archives of the upgrade, so the ELF check can inspect them. + #[arg(long)] + pub download: bool, +} + +impl Command for Check +{ + fn run(&self) -> anyhow::Result { todo!("keel check") } +} diff --git a/src/cli/commands/diff.rs b/src/cli/commands/diff.rs new file mode 100644 index 0000000..0bbfa9a --- /dev/null +++ b/src/cli/commands/diff.rs @@ -0,0 +1,17 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Show the package changes between a snapshot and the live root. +#[derive(Debug, Args)] +pub struct Diff { + /// The snapshot to compare with, as the snapshot provider names it. + /// The default is the rollback target. + #[arg(value_name = "SNAPSHOT")] + pub snapshot: Option, +} + +impl Command for Diff +{ + fn run(&self) -> anyhow::Result { todo!("keel diff") } +} diff --git a/src/cli/commands/doctor.rs b/src/cli/commands/doctor.rs new file mode 100644 index 0000000..b0b33c4 --- /dev/null +++ b/src/cli/commands/doctor.rs @@ -0,0 +1,12 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Show the detected layout and everything that would stop a rollback. +#[derive(Debug, Args)] +pub struct Doctor; + +impl Command for Doctor +{ + fn run(&self) -> anyhow::Result { todo!("keel doctor") } +} diff --git a/src/cli/commands/gc.rs b/src/cli/commands/gc.rs new file mode 100644 index 0000000..6df659e --- /dev/null +++ b/src/cli/commands/gc.rs @@ -0,0 +1,12 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Delete root subvolumes set aside by earlier rollbacks once they are old enough. +#[derive(Debug, Args)] +pub struct Gc; + +impl Command for Gc +{ + fn run(&self) -> anyhow::Result { todo!("keel gc") } +} diff --git a/src/cli/commands/hook.rs b/src/cli/commands/hook.rs new file mode 100644 index 0000000..6dfdb6e --- /dev/null +++ b/src/cli/commands/hook.rs @@ -0,0 +1,69 @@ +use clap::{ Args, Subcommand }; + +use crate::cli::{ Exit, Command }; + +/// Run one of keel's pacman hooks. Pacman passes the targets on standard input. +#[derive(Debug, Args)] +pub struct Hook { + #[command(subcommand)] + pub phase: Phase, +} + +#[derive(Debug, Subcommand)] +pub enum Phase { + Removals(Removals), + Pre(Pre), + Post(Post), +} + +impl Phase +{ + pub fn command(&self) -> &dyn Command + { + match self { + Self::Removals(c) => c, + Self::Pre(c) => c, + Self::Post(c) => c, + } + } +} + +impl Command for Hook +{ + fn run(&self) -> anyhow::Result { self.phase.command().run() } + + fn on_error(&self) -> Exit { self.phase.command().on_error() } +} + +/// Record the packages this transaction removes. +#[derive(Debug, Args)] +pub struct Removals; + +impl Command for Removals +{ + fn run(&self) -> anyhow::Result { todo!("keel hook removals") } + + fn on_error(&self) -> Exit { Exit::Ok } +} + +/// Check the pending transaction for skew and abort it on blocking findings. +#[derive(Debug, Args)] +pub struct Pre; + +impl Command for Pre +{ + fn run(&self) -> anyhow::Result { todo!("keel hook pre") } + + fn on_error(&self) -> Exit { Exit::Ok } +} + +/// Verify the system after the transaction and offer a rollback on failure. +#[derive(Debug, Args)] +pub struct Post; + +impl Command for Post +{ + fn run(&self) -> anyhow::Result { todo!("keel hook post") } + + fn on_error(&self) -> Exit { Exit::Ok } +} diff --git a/src/cli/commands/index.rs b/src/cli/commands/index.rs new file mode 100644 index 0000000..9dcf8ca --- /dev/null +++ b/src/cli/commands/index.rs @@ -0,0 +1,12 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Build the ELF symbol index for every installed package, or resume an interrupted build. +#[derive(Debug, Args)] +pub struct Index; + +impl Command for Index +{ + fn run(&self) -> anyhow::Result { todo!("keel index") } +} diff --git a/src/cli/commands/mod.rs b/src/cli/commands/mod.rs new file mode 100644 index 0000000..aaab79f --- /dev/null +++ b/src/cli/commands/mod.rs @@ -0,0 +1,27 @@ +mod ack; +mod cache; +mod check; +mod diff; +mod doctor; +mod gc; +mod hook; +mod index; +mod notify; +mod rollback; +mod status; +mod toggle; +mod verify; + +pub use ack::Ack; +pub use cache::{Cache, CacheAction, Prune}; +pub use check::Check; +pub use diff::Diff; +pub use doctor::Doctor; +pub use gc::Gc; +pub use hook::{Hook, Phase, Post, Pre, Removals}; +pub use index::Index; +pub use notify::Notify; +pub use rollback::Rollback; +pub use status::Status; +pub use toggle::{Disable, Enable, HookSet}; +pub use verify::Verify; diff --git a/src/cli/commands/notify.rs b/src/cli/commands/notify.rs new file mode 100644 index 0000000..7966480 --- /dev/null +++ b/src/cli/commands/notify.rs @@ -0,0 +1,12 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Show a desktop notification if the last update failed, was blocked, or keel is disabled. +#[derive(Debug, Args)] +pub struct Notify; + +impl Command for Notify +{ + fn run(&self) -> anyhow::Result { todo!("keel notify") } +} diff --git a/src/cli/commands/rollback.rs b/src/cli/commands/rollback.rs new file mode 100644 index 0000000..78089e0 --- /dev/null +++ b/src/cli/commands/rollback.rs @@ -0,0 +1,34 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Replace the root subvolume with a writable copy of a snapshot. +#[derive(Debug, Args)] +pub struct Rollback { + /// The snapshot to restore, as the snapshot provider names it. The default is the rollback + /// target. + #[arg(value_name = "SNAPSHOT", conflicts_with = "promote")] + pub snapshot: Option, + + /// Restore the snapshot the system is currently booted from. + #[arg(long)] + pub promote: bool, + + /// Skip the confirmation prompt. + #[arg(long, short = 'y')] + pub yes: bool, + + /// Reboot without asking once the rollback is done. + #[arg(long)] + pub reboot: bool, + + /// Wait until pacman releases its database lock, then roll back without asking. The post + /// hook starts this in a transient unit after the user has already confirmed. + #[arg(long, hide = true, requires = "snapshot")] + pub deferred: bool, +} + +impl Command for Rollback +{ + fn run(&self) -> anyhow::Result { todo!("keel rollback") } +} diff --git a/src/cli/commands/status.rs b/src/cli/commands/status.rs new file mode 100644 index 0000000..3673a92 --- /dev/null +++ b/src/cli/commands/status.rs @@ -0,0 +1,12 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Show the last recorded state, its findings and rollback target, and any disabled hooks. +#[derive(Debug, Args)] +pub struct Status; + +impl Command for Status +{ + fn run(&self) -> anyhow::Result { todo!("keel status") } +} diff --git a/src/cli/commands/toggle.rs b/src/cli/commands/toggle.rs new file mode 100644 index 0000000..db5db2d --- /dev/null +++ b/src/cli/commands/toggle.rs @@ -0,0 +1,35 @@ +use clap::{ Args, ValueEnum }; + +use crate::cli::{ Exit, Command }; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] +pub enum HookSet { + /// Both pre-transaction hooks, removals and pre. + Pre, + /// The post-transaction hook. + Post, + /// All three hooks. + All, +} + +/// Unmask keel's pacman hooks. +#[derive(Debug, Args)] +pub struct Enable { + pub hooks: HookSet, +} + +impl Command for Enable +{ + fn run(&self) -> anyhow::Result { todo!("keel enable") } +} + +/// Mask keel's pacman hooks. This works even when keel's config or state is broken. +#[derive(Debug, Args)] +pub struct Disable { + pub hooks: HookSet, +} + +impl Command for Disable +{ + fn run(&self) -> anyhow::Result { todo!("keel disable") } +} diff --git a/src/cli/commands/verify.rs b/src/cli/commands/verify.rs new file mode 100644 index 0000000..5766195 --- /dev/null +++ b/src/cli/commands/verify.rs @@ -0,0 +1,12 @@ +use clap::Args; + +use crate::cli::{ Exit, Command }; + +/// Run the post-transaction checks on the current system. +#[derive(Debug, Args)] +pub struct Verify; + +impl Command for Verify +{ + fn run(&self) -> anyhow::Result { todo!("keel verify") } +} diff --git a/src/cli/exit.rs b/src/cli/exit.rs new file mode 100644 index 0000000..f4fbf6c --- /dev/null +++ b/src/cli/exit.rs @@ -0,0 +1,18 @@ +use std::process::ExitCode; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum Exit { + Ok = 0, + /// Blocking findings, or a refused rollback. + /// The pre hook's wrapper aborts pacman on + /// this status and on no other. + Blocked = 1, + /// A usage or environment error. + Error = 2, +} + +impl From for ExitCode +{ + fn from(exit: Exit) -> Self { ExitCode::from(exit as u8) } +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs new file mode 100644 index 0000000..0e48df3 --- /dev/null +++ b/src/cli/mod.rs @@ -0,0 +1,82 @@ +mod command; +mod exit; + +pub mod commands; + +// ------ + +use clap::{ Parser, Subcommand }; + +pub use command::Command; +pub use exit::Exit; + +use commands::{ + Ack, Cache, Check, Diff, Disable, Doctor, Enable, + Gc, Hook, Index, Notify, Rollback, Status, Verify, +}; + +#[derive(Debug, Parser)] +#[command(name = "keel", version, about)] +pub struct Cli { + #[command(subcommand)] + pub command: Commands, +} + +impl Cli +{ + /// Runs the parsed subcommand and turns an error into that subcommand's error status. + pub fn run(&self) -> Exit + { + let command = self.command.command(); + + match command.run() + { + Ok(exit) => exit, + Err(err) => { + eprintln!("keel: {err:#}"); + command.on_error() + } + } + } +} + +#[derive(Debug, Subcommand)] +pub enum Commands { + Hook(Hook), + Enable(Enable), + Disable(Disable), + Check(Check), + Verify(Verify), + Status(Status), + Notify(Notify), + Diff(Diff), + Rollback(Rollback), + Ack(Ack), + Doctor(Doctor), + Index(Index), + Cache(Cache), + Gc(Gc), +} + +impl Commands +{ + pub fn command(&self) -> &dyn Command + { + match self { + Self::Hook(c) => c, + Self::Enable(c) => c, + Self::Disable(c) => c, + Self::Check(c) => c, + Self::Verify(c) => c, + Self::Status(c) => c, + Self::Notify(c) => c, + Self::Diff(c) => c, + Self::Rollback(c) => c, + Self::Ack(c) => c, + Self::Doctor(c) => c, + Self::Index(c) => c, + Self::Cache(c) => c, + Self::Gc(c) => c, + } + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4f77372 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod cli; diff --git a/src/main.rs b/src/main.rs index f328e4d..fc9499c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1 +1,11 @@ -fn main() {} +use std::process::ExitCode; +use clap::Parser; + +use keel::cli::Cli; + +fn main() -> ExitCode +{ + Cli::parse() + .run() + .into() +}