Simplify the config file format

This commit is contained in:
Zykino 2023-02-06 00:10:12 +01:00
parent 5956795564
commit f76f6da898
5 changed files with 51 additions and 66 deletions

View File

@ -1,25 +1,18 @@
packages: packages:
- name: Apt - name: Apt
fetch: fetch:
exe: sudo - exe: sudo
params: params:
- apt - apt
- update - update
current_dir: null
env: {}
compile: null
install: install:
exe: sudo exe: sudo
params: params:
- apt - apt
- upgrade - upgrade
- --yes - --yes
current_dir: null
env: {}
post_install: post_install:
- exe: sudo - exe: sudo
params: params:
- apt - apt
- autoremove - autoremove
current_dir: null
env: {}

View File

@ -1,14 +1,12 @@
packages: packages:
- name: Flatpack - name: Flatpack
fetch: fetch:
exe: flatpak - exe: flatpak
params: params:
- update - update
- --no-deploy - --no-deploy
- --assumeyes - --assumeyes
- --noninteractive - --noninteractive
current_dir: null
env: {}
install: install:
exe: flatpak exe: flatpak
params: params:
@ -16,8 +14,6 @@ packages:
- --no-pull - --no-pull
- --assumeyes - --assumeyes
- --noninteractive - --noninteractive
current_dir: null
env: {}
post-install: post-install:
- exe: flatpak - exe: flatpak
params: params:
@ -25,5 +21,3 @@ packages:
- --unused - --unused
- --assumeyes - --assumeyes
- --noninteractive - --noninteractive
current_dir: null
env: {}

View File

@ -4,5 +4,3 @@ packages:
exe: pipx exe: pipx
params: params:
- upgrade-all - upgrade-all
current_dir: null
env: {}

View File

@ -4,13 +4,9 @@ packages:
exe: rustup exe: rustup
params: params:
- update - update
current_dir: null
env: {}
- name: Cargo - name: Cargo
install: install:
exe: cargo exe: cargo
params: params:
- install-update - install-update
- -a - -a
current_dir: null
env: {}

View File

@ -28,8 +28,8 @@ pub struct System {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Package { pub struct Package {
pub name: String, pub name: String,
fetch: Option<Cmd>, fetch: Option<Vec<Cmd>>,
compile: Option<Cmd>, compile: Option<Vec<Cmd>>,
install: Cmd, install: Cmd,
post_install: Option<Vec<Cmd>>, post_install: Option<Vec<Cmd>>,
// deps or rDeps : Tree // deps or rDeps : Tree
@ -39,9 +39,9 @@ pub struct Package {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
struct Cmd { struct Cmd {
exe: String, exe: String,
params: Vec<String>, params: Option<Vec<String>>,
current_dir: Option<PathBuf>, current_dir: Option<PathBuf>,
env: HashMap<String, String>, env: Option<HashMap<String, String>>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -131,9 +131,9 @@ impl Updater {
compile: None, compile: None,
install: Cmd { install: Cmd {
exe: "rustup".to_owned(), exe: "rustup".to_owned(),
params: vec!["self".to_owned(), "update".to_owned()], params: Some(vec!["self".to_owned(), "update".to_owned()]),
current_dir: None, current_dir: None,
env: HashMap::new(), env: None,
}, },
post_install: None, post_install: None,
}); });
@ -144,9 +144,9 @@ impl Updater {
compile: None, compile: None,
install: Cmd { install: Cmd {
exe: "cargo".to_owned(), 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, current_dir: None,
env: HashMap::new(), env: None,
}, },
post_install: None, post_install: None,
}); });
@ -229,7 +229,8 @@ impl Updater {
impl Package { impl Package {
pub fn fetch(&self, opt: &Opt) -> Result<()> { pub fn fetch(&self, opt: &Opt) -> Result<()> {
if let Some(fetch) = &self.fetch { if let Some(fetch) = &self.fetch {
let cmd = fetch.clone().prepare(opt); for cmd in fetch {
let cmd = cmd.clone().prepare(opt);
let exit_status = cmd let exit_status = cmd
.execute(opt) .execute(opt)
.map_err(|err| MyError::new(MyErrorKind::Fetch, err, cmd.clone()))?; .map_err(|err| MyError::new(MyErrorKind::Fetch, err, cmd.clone()))?;
@ -242,12 +243,14 @@ impl Package {
)); ));
} }
} }
}
Ok(()) Ok(())
} }
pub fn compile(&self, opt: &Opt) -> Result<()> { pub fn compile(&self, opt: &Opt) -> Result<()> {
if let Some(compile) = &self.compile { if let Some(compile) = &self.compile {
let cmd = compile.clone().prepare(opt); for cmd in compile {
let cmd = cmd.clone().prepare(opt);
let exit_status = cmd let exit_status = cmd
.execute(opt) .execute(opt)
.map_err(|err| MyError::new(MyErrorKind::Compile, err, cmd.clone()))?; .map_err(|err| MyError::new(MyErrorKind::Compile, err, cmd.clone()))?;
@ -260,6 +263,7 @@ impl Package {
)); ));
} }
} }
}
Ok(()) Ok(())
} }
@ -304,23 +308,23 @@ impl Cmd {
fn new() -> Cmd { fn new() -> Cmd {
Cmd { Cmd {
exe: "".into(), exe: "".into(),
params: vec![], params: None,
current_dir: None, current_dir: None,
env: HashMap::new(), env: None,
} }
} }
fn prepare(self, opt: &Opt) -> ActualCmd { fn prepare(self, opt: &Opt) -> ActualCmd {
let params = self.params; // TODO: Im 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 mut env = self.env; let env = match self.env {
if !env.contains_key("PATH") { Some(env) => env,
env.insert("PATH".to_owned(), std::env!("PATH").to_owned()); None => HashMap::from([("PATH".to_owned(), std::env!("PATH").to_owned())]),
} };
ActualCmd { ActualCmd {
exe: self.exe, exe: self.exe,
params, params: self.params.unwrap_or_default(),
current_dir: self.current_dir, current_dir: self.current_dir,
env, env,
} }