Skip to content

Commit

Permalink
feat: script priority support
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed May 24, 2024
1 parent 8336014 commit e549ddc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ nix = { version = "0.27", features = ["mount", "hostname", "dir"] }
uuid = { version = "1.4.1", features = ["v4", "serde"] }
loopdev-fyra = { version = "0.5.0" }
bytesize = { version = "1.3.0", features = ["serde"] }
indexmap = "2.2.6"
9 changes: 6 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::{
};
use cmd_lib::{run_cmd, run_fun};
use color_eyre::{eyre::bail, Result};
use indexmap::IndexMap;
use serde_derive::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
collections::BTreeMap,
fs,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -406,13 +407,15 @@ pub fn run_script(script: Script, chroot: &Path, is_post: bool) -> Result<()> {

pub fn run_all_scripts(scrs: &[Script], chroot: &Path, is_post: bool) -> Result<()> {
// name => (Script, is_executed)
let mut scrs = scrs.to_owned();
scrs.sort_by_cached_key(|s| s.priority);
let scrs = scrs.iter().map(|s| (s.id.as_ref().map_or("<?>", |s| s), (s.clone(), false)));
run_scripts(scrs.collect(), chroot, is_post)
}

#[tracing::instrument]
pub fn run_scripts(
mut scripts: HashMap<&str, (Script, bool)>, chroot: &Path, is_post: bool,
mut scripts: IndexMap<&str, (Script, bool)>, chroot: &Path, is_post: bool,
) -> Result<()> {
trace!("Running scripts");
for idx in scripts.clone().keys() {
Expand All @@ -426,7 +429,7 @@ pub fn run_scripts(

// Find needs
let id = scr.id.clone().unwrap_or("<NULL>".into());
let mut needs = HashMap::new();
let mut needs = IndexMap::new();
let scr_needs_vec = &scr.needs.clone();
for need in scr_needs_vec {
// when funny rust doesn't know how to convert &String to &str
Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ pub struct ScriptsManifest {
pub post: Vec<Script>,
}

fn script_default_priority() -> i32 {
50
}

#[derive(Deserialize, Debug, Clone, Serialize, PartialEq, Eq, Default)]
// load script from file, or inline if there's one specified
pub struct Script {
Expand All @@ -214,6 +218,9 @@ pub struct Script {
pub chroot: Option<bool>,
#[serde(default)]
pub needs: Vec<String>,
/// Default 50, the higher, the later the script executes
#[serde(default = "script_default_priority")]
pub priority: i32,
}

impl Script {
Expand Down

0 comments on commit e549ddc

Please sign in to comment.