From f76f6da8984776528e916ee6b5506b453cb0a638 Mon Sep 17 00:00:00 2001 From: Zykino Date: Mon, 6 Feb 2023 00:10:12 +0100 Subject: [PATCH] Simplify the config file format --- examples/apt.yaml | 15 +++----- examples/flatpack.yaml | 18 ++++------ examples/pipx.yaml | 2 -- examples/rust.yaml | 4 --- src/command.rs | 78 ++++++++++++++++++++++-------------------- 5 files changed, 51 insertions(+), 66 deletions(-) diff --git a/examples/apt.yaml b/examples/apt.yaml index 9fc978f..52aae84 100644 --- a/examples/apt.yaml +++ b/examples/apt.yaml @@ -1,25 +1,18 @@ packages: - name: Apt fetch: - exe: sudo - params: - - apt - - update - current_dir: null - env: {} - compile: null + - exe: sudo + params: + - apt + - update install: exe: sudo params: - apt - upgrade - --yes - current_dir: null - env: {} post_install: - exe: sudo params: - apt - autoremove - current_dir: null - env: {} diff --git a/examples/flatpack.yaml b/examples/flatpack.yaml index be7af86..10aa2f5 100644 --- a/examples/flatpack.yaml +++ b/examples/flatpack.yaml @@ -1,14 +1,12 @@ packages: - name: Flatpack fetch: - exe: flatpak - params: - - update - - --no-deploy - - --assumeyes - - --noninteractive - current_dir: null - env: {} + - exe: flatpak + params: + - update + - --no-deploy + - --assumeyes + - --noninteractive install: exe: flatpak params: @@ -16,8 +14,6 @@ packages: - --no-pull - --assumeyes - --noninteractive - current_dir: null - env: {} post-install: - exe: flatpak params: @@ -25,5 +21,3 @@ packages: - --unused - --assumeyes - --noninteractive - current_dir: null - env: {} diff --git a/examples/pipx.yaml b/examples/pipx.yaml index 60c2d52..70faa7c 100644 --- a/examples/pipx.yaml +++ b/examples/pipx.yaml @@ -4,5 +4,3 @@ packages: exe: pipx params: - upgrade-all - current_dir: null - env: {} diff --git a/examples/rust.yaml b/examples/rust.yaml index 4a774b2..f1a183a 100644 --- a/examples/rust.yaml +++ b/examples/rust.yaml @@ -4,13 +4,9 @@ packages: exe: rustup params: - update - current_dir: null - env: {} - name: Cargo install: exe: cargo params: - install-update - -a - current_dir: null - env: {} diff --git a/src/command.rs b/src/command.rs index 5f1f03a..4724586 100644 --- a/src/command.rs +++ b/src/command.rs @@ -28,8 +28,8 @@ pub struct System { #[derive(Debug, Serialize, Deserialize)] pub struct Package { pub name: String, - fetch: Option, - compile: Option, + fetch: Option>, + compile: Option>, install: Cmd, post_install: Option>, // deps or rDeps : Tree @@ -39,9 +39,9 @@ pub struct Package { #[derive(Debug, Clone, Serialize, Deserialize)] struct Cmd { exe: String, - params: Vec, + params: Option>, current_dir: Option, - env: HashMap, + env: Option>, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -131,9 +131,9 @@ impl Updater { compile: None, install: Cmd { exe: "rustup".to_owned(), - params: vec!["self".to_owned(), "update".to_owned()], + params: Some(vec!["self".to_owned(), "update".to_owned()]), current_dir: None, - env: HashMap::new(), + env: None, }, post_install: None, }); @@ -144,9 +144,9 @@ impl Updater { compile: None, install: Cmd { exe: "cargo".to_owned(), - params: vec!["install-update".to_owned(), "-a".to_owned()], + params: Some(vec!["install-update".to_owned(), "-a".to_owned()]), current_dir: None, - env: HashMap::new(), + env: None, }, post_install: None, }); @@ -229,17 +229,19 @@ impl Updater { impl Package { pub fn fetch(&self, opt: &Opt) -> Result<()> { if let Some(fetch) = &self.fetch { - let cmd = fetch.clone().prepare(opt); - let exit_status = cmd - .execute(opt) - .map_err(|err| MyError::new(MyErrorKind::Fetch, err, cmd.clone()))?; + for cmd in fetch { + let cmd = cmd.clone().prepare(opt); + 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, - io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), - cmd.clone(), - )); + if !exit_status.success() { + return Err(MyError::new( + MyErrorKind::Fetch, + io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), + cmd.clone(), + )); + } } } Ok(()) @@ -247,17 +249,19 @@ impl Package { pub fn compile(&self, opt: &Opt) -> Result<()> { if let Some(compile) = &self.compile { - let cmd = compile.clone().prepare(opt); - let exit_status = cmd - .execute(opt) - .map_err(|err| MyError::new(MyErrorKind::Compile, err, cmd.clone()))?; + for cmd in compile { + let cmd = cmd.clone().prepare(opt); + 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::Compile, - io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), - cmd.clone(), - )); + if !exit_status.success() { + return Err(MyError::new( + MyErrorKind::Compile, + io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), + cmd.clone(), + )); + } } } Ok(()) @@ -304,23 +308,23 @@ impl Cmd { fn new() -> Cmd { Cmd { exe: "".into(), - params: vec![], + params: None, current_dir: None, - env: HashMap::new(), + env: None, } } fn prepare(self, opt: &Opt) -> ActualCmd { - let params = self.params; - - let mut env = self.env; - if !env.contains_key("PATH") { - env.insert("PATH".to_owned(), std::env!("PATH").to_owned()); - } + // TODO: I’m not convinced by helping the user and only escaping the PATH. Either all or none + // This means I need to know how to know which values to pass for (at least) rustup & cargo + let env = match self.env { + Some(env) => env, + None => HashMap::from([("PATH".to_owned(), std::env!("PATH").to_owned())]), + }; ActualCmd { exe: self.exe, - params, + params: self.params.unwrap_or_default(), current_dir: self.current_dir, env, }