From 14eaf86da2b2044608d89a5353b321320c43327d Mon Sep 17 00:00:00 2001 From: Zykino Date: Sun, 29 Jan 2023 16:38:30 +0100 Subject: [PATCH] Add post installation commands --- examples/debian.yml | 7 +++++++ src/command.rs | 39 ++++++++++++++++++++++++++++++++++++--- src/errors.rs | 13 ++++++++++--- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/examples/debian.yml b/examples/debian.yml index fa896b3..ccd0404 100644 --- a/examples/debian.yml +++ b/examples/debian.yml @@ -16,6 +16,13 @@ systems: - --yes current_dir: null env: {} + post_install: + - exe: sudo + params: + - apt + - autoremove + current_dir: null + env: {} - name: Rustup install: exe: rustup diff --git a/src/command.rs b/src/command.rs index 84db3db..486b1eb 100644 --- a/src/command.rs +++ b/src/command.rs @@ -11,6 +11,7 @@ enum UpdateSteps { Fetch, Compile, Install, + PostInstall, } #[derive(Debug, Serialize, Deserialize)] @@ -24,6 +25,7 @@ pub struct System { fetch: Option, compile: Option, install: Cmd, + post_install: Option>, // deps or rDeps : Tree // exclusive_with : List } @@ -51,6 +53,7 @@ impl From<&str> for UpdateSteps { "fetch" => UpdateSteps::Fetch, "compile" => UpdateSteps::Compile, "install" => UpdateSteps::Install, + "post_install" => UpdateSteps::PostInstall, _ => panic!("Step {} not recognized", value), } @@ -64,6 +67,7 @@ impl From<&String> for UpdateSteps { "fetch" => UpdateSteps::Fetch, "compile" => UpdateSteps::Compile, "install" => UpdateSteps::Install, + "post_install" => UpdateSteps::PostInstall, _ => panic!("Step {} not recognized", value), } @@ -74,10 +78,11 @@ impl Updater { fn new() -> Updater { let mut up = Updater { systems: vec![] }; up.systems.push(System { - name: "".into(), + name: Default::default(), fetch: None, compile: None, install: Cmd::new(), + post_install: None, }); up } @@ -117,11 +122,13 @@ impl Updater { } fn update(&self, sys: &System, opt: &Opt) -> Result<()> { + // TODO: compute once before calling this function, maybe? let steps = if opt.steps.is_empty() { vec![ UpdateSteps::Fetch, UpdateSteps::Compile, UpdateSteps::Install, + UpdateSteps::PostInstall, ] } else { opt.steps.iter().map(|u| u.into()).collect() @@ -136,6 +143,9 @@ impl Updater { if steps.contains(&UpdateSteps::Install) { sys.install(opt)?; } + if steps.contains(&UpdateSteps::PostInstall) { + sys.post_install(opt)?; + } Ok(()) } @@ -148,6 +158,7 @@ impl System { let exit_status = cmd .execute(opt) .map_err(|err| MyError::new(MyErrorKind::Fetch, err, cmd.clone()))?; + if !exit_status.success() { return Err(MyError::new( MyErrorKind::Fetch, @@ -165,9 +176,10 @@ impl System { let exit_status = cmd .execute(opt) .map_err(|err| MyError::new(MyErrorKind::Compile, err, cmd.clone()))?; + if !exit_status.success() { return Err(MyError::new( - MyErrorKind::Fetch, + MyErrorKind::Compile, io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), cmd.clone(), )); @@ -181,15 +193,36 @@ impl System { let exit_status = cmd .execute(opt) .map_err(|err| MyError::new(MyErrorKind::Install, err, cmd.clone()))?; + if !exit_status.success() { return Err(MyError::new( - MyErrorKind::Fetch, + MyErrorKind::Install, io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), cmd.clone(), )); } Ok(()) } + + pub fn post_install(&self, opt: &Opt) -> Result<()> { + if let Some(post_install) = &self.post_install { + for cmd in post_install { + let cmd = cmd.clone().prepare(opt); + let exit_status = cmd + .execute(opt) + .map_err(|err| MyError::new(MyErrorKind::PostInstall, err, cmd.clone()))?; + + if !exit_status.success() { + return Err(MyError::new( + MyErrorKind::PostInstall, + io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), + cmd.clone(), + )); + } + } + } + Ok(()) + } } impl Cmd { diff --git a/src/errors.rs b/src/errors.rs index 6a1b7aa..5537bcf 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -22,9 +22,10 @@ pub struct MyError { #[non_exhaustive] pub enum MyErrorKind { Config, - Fetch, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? - Compile, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? - Install, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? + Fetch, + Compile, + Install, + PostInstall, } impl MyError { @@ -70,6 +71,12 @@ impl Display for MyError { self.cmd, self.source().unwrap() ), + MyErrorKind::PostInstall => write!( + f, + "Could not do the post_install command {}: {}", + self.cmd, + self.source().unwrap() + ), } } }