refactor: decide phase, milestones and quit keys once

This commit is contained in:
Willem Vanhulle committed 2026-10-21 12:12:49 +02:00
1 parent 589cc3c82b
commit 8d96034574
5 files changed
+27 -39

No files matched your search

+6 -13
View File
@@ -2,19 +2,16 @@ use std::{
collections::HashMap, collections::HashMap,
mem, mem,
os::fd::{AsFd, BorrowedFd}, os::fd::{AsFd, BorrowedFd},
path::Path as FilePath,
process, process,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use bootvine::boot_model::{ use bootvine::boot_model::{BootState, HANDOFF_UNIT, JobKind, Phase};
BootState, HANDOFF_UNIT, HOST_MILESTONES, JobKind, Phase, SHUTDOWN_MILESTONES,
};
use dbus::Path; use dbus::Path;
use crate::{ use crate::{
monotonic_clock::monotonic_now, monotonic_clock::monotonic_now,
switch_root::{INITRD_RELEASE, Sysroot}, switch_root::Sysroot,
systemd_bus::{Scope, Session, Signal}, systemd_bus::{Scope, Session, Signal},
watch_error::Error, watch_error::Error,
}; };
@@ -68,10 +65,10 @@ pub struct Feed {
} }
impl Feed { impl Feed {
pub fn new(scope: Scope, target: RootRequest) -> Self { pub fn new(phase: Phase, scope: Scope, target: RootRequest) -> Self {
Self { Self {
connections: 0, connections: 0,
root_fs: if scope == Scope::System && FilePath::new(INITRD_RELEASE).exists() { root_fs: if phase == Phase::Initrd {
RootFs::Initrd RootFs::Initrd
} else { } else {
RootFs::Host RootFs::Host
@@ -165,11 +162,7 @@ impl Feed {
.to_owned(), .to_owned(),
RootRequest::ManagerDefault => session.default_target()?, RootRequest::ManagerDefault => session.default_target()?,
}; };
let milestones: &[&str] = if shutting_down { let milestones = state.milestone_targets.clone();
&SHUTDOWN_MILESTONES
} else {
&HOST_MILESTONES
};
if !self.gave_origin { if !self.gave_origin {
state.origin = if session.boot_in_progress()? { state.origin = if session.boot_in_progress()? {
Duration::ZERO Duration::ZERO
@@ -186,7 +179,7 @@ impl Feed {
} }
jobs.iter() jobs.iter()
.map(|job| job.unit.as_str()) .map(|job| job.unit.as_str())
.chain(milestones.iter().copied()) .chain(milestones.iter().map(String::as_str))
.for_each(|unit| { .for_each(|unit| {
self.track(&session, state, unit); self.track(&session, state, unit);
}); });
+3 -3
View File
@@ -114,19 +114,19 @@ pub struct BootState {
pub finished_runs: Vec<FinishedRun>, pub finished_runs: Vec<FinishedRun>,
pub handoff_unit: Option<String>, pub handoff_unit: Option<String>,
pub manager_exited_at: Option<Duration>, pub manager_exited_at: Option<Duration>,
milestone_targets: Vec<String>, pub milestone_targets: Vec<String>,
finished_before_reconnect: usize, finished_before_reconnect: usize,
units: HashMap<String, UnitRecord>, units: HashMap<String, UnitRecord>,
jobs: HashMap<u32, String>, jobs: HashMap<u32, String>,
} }
pub const HOST_MILESTONES: [&str; 4] = [ const HOST_MILESTONES: [&str; 4] = [
"sysinit.target", "sysinit.target",
"basic.target", "basic.target",
"multi-user.target", "multi-user.target",
"graphical.target", "graphical.target",
]; ];
pub const SHUTDOWN_MILESTONES: [&str; 3] = ["shutdown.target", "umount.target", "final.target"]; const SHUTDOWN_MILESTONES: [&str; 3] = ["shutdown.target", "umount.target", "final.target"];
pub const HANDOFF_UNIT: &str = "display-manager.service"; pub const HANDOFF_UNIT: &str = "display-manager.service";
+12
View File
@@ -15,6 +15,18 @@ pub fn masked(typed: &str) -> String {
"*".repeat(typed.chars().count()) "*".repeat(typed.chars().count())
} }
#[must_use]
pub fn asks_to_quit(event: &Event, typing: bool) -> bool {
let Event::Key(KeyEvent {
code, modifiers, ..
}) = event
else {
return false;
};
let control_c = *code == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL);
*code == KeyCode::Esc || (!typing && (*code == KeyCode::Char('q') || control_c))
}
pub fn type_key(typed: &mut Zeroizing<String>, event: &Event) -> Option<Zeroizing<String>> { pub fn type_key(typed: &mut Zeroizing<String>, event: &Event) -> Option<Zeroizing<String>> {
let Event::Key(KeyEvent { let Event::Key(KeyEvent {
code, code,
+2 -9
View File
@@ -6,7 +6,7 @@ use std::{
use ratatui::{ use ratatui::{
DefaultTerminal, DefaultTerminal,
crossterm::event::{self, Event, KeyCode}, crossterm::event::{self, Event},
}; };
use crate::{ use crate::{
@@ -290,13 +290,6 @@ fn next_event() -> io::Result<Option<Event>> {
} }
} }
fn quits(event: &Event, typing: bool) -> bool {
matches!(
event,
Event::Key(key) if key.code == KeyCode::Esc || (!typing && key.code == KeyCode::Char('q'))
)
}
pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<()> { pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<()> {
let clock = Instant::now(); let clock = Instant::now();
let now = || clock.elapsed(); let now = || clock.elapsed();
@@ -346,7 +339,7 @@ pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<(
} }
if let Some(event) = next_event()? { if let Some(event) = next_event()? {
let asking = script.asking().is_some(); let asking = script.asking().is_some();
if quits(&event, asking) { if passphrase_input::asks_to_quit(&event, asking) {
return Ok(()); return Ok(());
} }
if asking if asking
+4 -14
View File
@@ -16,8 +16,9 @@ use bootvine::{
boot_view::BootView, boot_view::BootView,
color_scheme::Colors, color_scheme::Colors,
config_file::Timing, config_file::Timing,
passphrase_input::asks_to_quit,
}; };
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event;
use ratatui::{ use ratatui::{
DefaultTerminal, DefaultTerminal,
backend::Backend, backend::Backend,
@@ -72,7 +73,7 @@ pub fn run(settings: &Settings) -> Result<(), Error> {
Scope::System => Phase::Host, Scope::System => Phase::Host,
}; };
let mut state = BootState::new(phase); let mut state = BootState::new(phase);
let mut feed = Feed::new(settings.scope, settings.target.clone()); let mut feed = Feed::new(phase, settings.scope, settings.target.clone());
let mut console = Console::take(settings.scope == Scope::System); let mut console = Console::take(settings.scope == Scope::System);
let mut terminal = ratatui::try_init()?; let mut terminal = ratatui::try_init()?;
let watched = watch( let watched = watch(
@@ -104,17 +105,6 @@ fn sleep_until_readable<'fd>(
} }
} }
fn asks_to_quit(event: &Event) -> bool {
let Event::Key(KeyEvent {
code, modifiers, ..
}) = event
else {
return false;
};
*code == KeyCode::Char('q')
|| (*code == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL))
}
fn overwrite_every_cell<B: Backend>(backend: &mut B, frame: &Buffer) -> Result<(), B::Error> { fn overwrite_every_cell<B: Backend>(backend: &mut B, frame: &Buffer) -> Result<(), B::Error> {
backend.draw( backend.draw(
frame frame
@@ -229,7 +219,7 @@ fn watch(
eprintln!("bootvine: cannot answer the password prompt: {error}"); eprintln!("bootvine: cannot answer the password prompt: {error}");
} }
} }
None if asks_to_quit(&event) => return Ok(()), None if asks_to_quit(&event, false) => return Ok(()),
None => {} None => {}
} }
} }