mirror of
https://github.com/edera-dev/sprout.git
synced 2025-12-19 10:10:17 +00:00
add print action
This commit is contained in:
@@ -3,6 +3,9 @@ version = 1
|
||||
[values]
|
||||
default-options = "tty=hvc0"
|
||||
|
||||
[actions.welcome]
|
||||
print.text = "Welcome to Sprout!"
|
||||
|
||||
[actions.chainload-kernel]
|
||||
chainload.path = "$path"
|
||||
chainload.options = ["$default-options"]
|
||||
@@ -12,3 +15,6 @@ entry.title = "Boot Kernel $name"
|
||||
entry.values.path = "\\EFI\\BOOT\\$name"
|
||||
entry.actions = ["chainload-kernel"]
|
||||
values.name = ["kernel.efi"]
|
||||
|
||||
[[phases.startup]]
|
||||
actions = ["welcome"]
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
use crate::config::ActionDeclaration;
|
||||
use crate::context::Context;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub mod chainload;
|
||||
pub mod print;
|
||||
|
||||
pub fn execute(context: Rc<Context>, action: &ActionDeclaration) {
|
||||
pub fn execute(context: Rc<Context>, name: impl AsRef<str>) {
|
||||
let Some(action) = context.root().actions().get(name.as_ref()) else {
|
||||
panic!("unknown action: {}", name.as_ref());
|
||||
};
|
||||
let context = context.finalize().freeze();
|
||||
|
||||
if let Some(chainload) = &action.chainload {
|
||||
chainload::chainload(context, chainload);
|
||||
} else if let Some(print) = &action.print {
|
||||
print::print(context, print);
|
||||
} else {
|
||||
panic!("unknown action configuration");
|
||||
}
|
||||
|
||||
7
src/actions/print.rs
Normal file
7
src/actions/print.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use crate::config::PrintConfiguration;
|
||||
use crate::context::Context;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn print(context: Rc<Context>, configuration: &PrintConfiguration) {
|
||||
println!("{}", context.stamp(&configuration.text));
|
||||
}
|
||||
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Clone)]
|
||||
pub struct RootConfiguration {
|
||||
#[serde(default = "default_version")]
|
||||
#[serde(default = "latest_version")]
|
||||
pub version: u32,
|
||||
#[serde(default)]
|
||||
pub values: BTreeMap<String, String>,
|
||||
@@ -22,6 +22,8 @@ pub struct RootConfiguration {
|
||||
pub struct ActionDeclaration {
|
||||
#[serde(default)]
|
||||
pub chainload: Option<ChainloadConfiguration>,
|
||||
#[serde(default)]
|
||||
pub print: Option<PrintConfiguration>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Clone)]
|
||||
@@ -68,11 +70,17 @@ pub struct ChainloadConfiguration {
|
||||
pub options: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Clone)]
|
||||
pub struct PrintConfiguration {
|
||||
#[serde(default)]
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
pub fn load() -> RootConfiguration {
|
||||
let content = utils::read_file_contents("sprout.toml");
|
||||
toml::from_slice(&content).expect("unable to parse sprout.toml file")
|
||||
}
|
||||
|
||||
pub fn default_version() -> u32 {
|
||||
pub fn latest_version() -> u32 {
|
||||
1
|
||||
}
|
||||
|
||||
29
src/main.rs
29
src/main.rs
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::config::PhaseConfiguration;
|
||||
use crate::context::{Context, RootContext};
|
||||
use log::info;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub mod actions;
|
||||
@@ -18,10 +19,6 @@ fn phase(context: Rc<Context>, phase: &[PhaseConfiguration]) {
|
||||
let context = context.freeze();
|
||||
|
||||
for action in item.actions.iter() {
|
||||
let Some(action) = context.root().actions().get(action) else {
|
||||
panic!("unknown action: {}", action);
|
||||
};
|
||||
|
||||
actions::execute(context.clone(), action);
|
||||
}
|
||||
}
|
||||
@@ -31,6 +28,11 @@ fn main() {
|
||||
setup::init();
|
||||
|
||||
let config = config::load();
|
||||
|
||||
if config.version > config::latest_version() {
|
||||
panic!("unsupported configuration version: {}", config.version);
|
||||
}
|
||||
|
||||
let mut root = RootContext::new();
|
||||
root.actions_mut().extend(config.actions.clone());
|
||||
|
||||
@@ -63,31 +65,18 @@ fn main() {
|
||||
final_entries.push((context, entry));
|
||||
}
|
||||
|
||||
println!("Boot Entries:");
|
||||
info!("entries:");
|
||||
for (index, (context, entry)) in final_entries.iter().enumerate() {
|
||||
let title = context.stamp(&entry.title);
|
||||
println!(" Entry {}: {}", index + 1, title);
|
||||
info!(" entry {}: {}", index + 1, title);
|
||||
}
|
||||
|
||||
// let mut input = String::new();
|
||||
// std::io::stdin().read_line(&mut input).expect("failed to read line");
|
||||
// let input = input.trim();
|
||||
// let Some(index) = input.parse::<usize>().ok().and_then(|value| if value > final_entries.len() {
|
||||
// None
|
||||
// } else {
|
||||
// Some(value)
|
||||
// }) else {
|
||||
// eprintln!("invalid entry number");
|
||||
// continue;
|
||||
// };
|
||||
let index = 1;
|
||||
|
||||
let (context, entry) = &final_entries[index - 1];
|
||||
|
||||
for action in &entry.actions {
|
||||
let Some(action) = context.root().actions().get(action) else {
|
||||
panic!("unknown action: {}", action);
|
||||
};
|
||||
let action = context.stamp(action);
|
||||
actions::execute(context.clone(), action);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user