refactor: decide phase, milestones and quit keys once
This commit is contained in:
1 parent
589cc3c82b
commit
8d96034574
5 files changed
+27
-39
No files matched your search
+6
-13
@@ -2,19 +2,16 @@ use std::{
|
||||
collections::HashMap,
|
||||
mem,
|
||||
os::fd::{AsFd, BorrowedFd},
|
||||
path::Path as FilePath,
|
||||
process,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use bootvine::boot_model::{
|
||||
BootState, HANDOFF_UNIT, HOST_MILESTONES, JobKind, Phase, SHUTDOWN_MILESTONES,
|
||||
};
|
||||
use bootvine::boot_model::{BootState, HANDOFF_UNIT, JobKind, Phase};
|
||||
use dbus::Path;
|
||||
|
||||
use crate::{
|
||||
monotonic_clock::monotonic_now,
|
||||
switch_root::{INITRD_RELEASE, Sysroot},
|
||||
switch_root::Sysroot,
|
||||
systemd_bus::{Scope, Session, Signal},
|
||||
watch_error::Error,
|
||||
};
|
||||
@@ -68,10 +65,10 @@ pub struct Feed {
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
pub fn new(scope: Scope, target: RootRequest) -> Self {
|
||||
pub fn new(phase: Phase, scope: Scope, target: RootRequest) -> Self {
|
||||
Self {
|
||||
connections: 0,
|
||||
root_fs: if scope == Scope::System && FilePath::new(INITRD_RELEASE).exists() {
|
||||
root_fs: if phase == Phase::Initrd {
|
||||
RootFs::Initrd
|
||||
} else {
|
||||
RootFs::Host
|
||||
@@ -165,11 +162,7 @@ impl Feed {
|
||||
.to_owned(),
|
||||
RootRequest::ManagerDefault => session.default_target()?,
|
||||
};
|
||||
let milestones: &[&str] = if shutting_down {
|
||||
&SHUTDOWN_MILESTONES
|
||||
} else {
|
||||
&HOST_MILESTONES
|
||||
};
|
||||
let milestones = state.milestone_targets.clone();
|
||||
if !self.gave_origin {
|
||||
state.origin = if session.boot_in_progress()? {
|
||||
Duration::ZERO
|
||||
@@ -186,7 +179,7 @@ impl Feed {
|
||||
}
|
||||
jobs.iter()
|
||||
.map(|job| job.unit.as_str())
|
||||
.chain(milestones.iter().copied())
|
||||
.chain(milestones.iter().map(String::as_str))
|
||||
.for_each(|unit| {
|
||||
self.track(&session, state, unit);
|
||||
});
|
||||
|
||||
+3
-3
@@ -114,19 +114,19 @@ pub struct BootState {
|
||||
pub finished_runs: Vec<FinishedRun>,
|
||||
pub handoff_unit: Option<String>,
|
||||
pub manager_exited_at: Option<Duration>,
|
||||
milestone_targets: Vec<String>,
|
||||
pub milestone_targets: Vec<String>,
|
||||
finished_before_reconnect: usize,
|
||||
units: HashMap<String, UnitRecord>,
|
||||
jobs: HashMap<u32, String>,
|
||||
}
|
||||
|
||||
pub const HOST_MILESTONES: [&str; 4] = [
|
||||
const HOST_MILESTONES: [&str; 4] = [
|
||||
"sysinit.target",
|
||||
"basic.target",
|
||||
"multi-user.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";
|
||||
@@ -15,6 +15,18 @@ pub fn masked(typed: &str) -> String {
|
||||
"*".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>> {
|
||||
let Event::Key(KeyEvent {
|
||||
code,
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::{
|
||||
|
||||
use ratatui::{
|
||||
DefaultTerminal,
|
||||
crossterm::event::{self, Event, KeyCode},
|
||||
crossterm::event::{self, Event},
|
||||
};
|
||||
|
||||
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<()> {
|
||||
let clock = Instant::now();
|
||||
let now = || clock.elapsed();
|
||||
@@ -346,7 +339,7 @@ pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<(
|
||||
}
|
||||
if let Some(event) = next_event()? {
|
||||
let asking = script.asking().is_some();
|
||||
if quits(&event, asking) {
|
||||
if passphrase_input::asks_to_quit(&event, asking) {
|
||||
return Ok(());
|
||||
}
|
||||
if asking
|
||||
|
||||
+4
-14
@@ -16,8 +16,9 @@ use bootvine::{
|
||||
boot_view::BootView,
|
||||
color_scheme::Colors,
|
||||
config_file::Timing,
|
||||
passphrase_input::asks_to_quit,
|
||||
};
|
||||
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
|
||||
use crossterm::event;
|
||||
use ratatui::{
|
||||
DefaultTerminal,
|
||||
backend::Backend,
|
||||
@@ -72,7 +73,7 @@ pub fn run(settings: &Settings) -> Result<(), Error> {
|
||||
Scope::System => Phase::Host,
|
||||
};
|
||||
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 terminal = ratatui::try_init()?;
|
||||
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> {
|
||||
backend.draw(
|
||||
frame
|
||||
@@ -229,7 +219,7 @@ fn watch(
|
||||
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 => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user