Skip to content

Commit

Permalink
Call hooks on processing, skipping dry-run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Nov 8, 2023
1 parent b00a8c6 commit f427106
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/procdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
profile::Profile,
rootfs,
scanner::{binlib::ElfScanner, debpkg::DebPackageScanner, dlst::ContentFormatter, general::Scanner},
shcall::ShellScript,
};
use std::fs::{self, canonicalize, remove_file, DirEntry, File};
use std::{
Expand Down Expand Up @@ -141,14 +142,42 @@ impl TintProcessor {
np
}

/// Call a script hook
fn call_script(s: String) -> Result<(), Error> {
// XXX: It can run args, but from where pass them? Profile? CLI? Both? None at all?..
let (stdout, stderr) = ShellScript::new(s, None).run()?;

if !stdout.is_empty() {
log::debug!("Post-hook stdout:");
log::debug!("{}", stdout);
}

if !stderr.is_empty() {
log::error!("Post-hook error:");
log::error!("{}", stderr);
}

Ok(())
}

// Start tint processor
pub fn start(&self) -> Result<(), Error> {
self.switch_root()?;

// Bail-out if the image is already processed
if self.lockfile.exists() {
return Err(Error::new(std::io::ErrorKind::AlreadyExists, "This container seems already tinted."));
}

// Run pre-hook, if any
if self.profile.has_pre_hook() {
if self.dry_run {
log::debug!("Pre-hook:\n{}", self.profile.get_pre_hook());
} else {
Self::call_script(self.profile.get_pre_hook())?;
}
}

// Paths to keep
let mut paths: HashSet<PathBuf> = HashSet::default();

Expand Down Expand Up @@ -210,8 +239,15 @@ impl TintProcessor {
paths.sort();

if self.dry_run {
if self.profile.has_post_hook() {
log::debug!("Post-hook:\n{}", self.profile.get_post_hook());
}
ContentFormatter::new(&paths).set_removed(&p).format();
} else {
// Run post-hook (doesn't affect changes apply)
if self.profile.has_post_hook() {
Self::call_script(self.profile.get_post_hook())?;
}
self.apply_changes(p)?;
}

Expand Down

0 comments on commit f427106

Please sign in to comment.