From 68c2751b3d3ffa3a163842c02a827a558075a508 Mon Sep 17 00:00:00 2001 From: Michal Date: Fri, 22 Jul 2022 23:48:47 +0100 Subject: [PATCH] Fully reworked config struct + began work on signing on repo gen --- example/mlc.toml | 6 +++++- src/internal/structs.rs | 23 ++++++++++++++++------- src/main.rs | 6 +++--- src/operations/build.rs | 13 +++++++++---- src/operations/clone.rs | 4 ++-- src/operations/pull.rs | 8 ++++---- src/repository/repo.rs | 30 +++++++++++++++++++++++++++++- src/workspace/read.rs | 9 +++------ 8 files changed, 71 insertions(+), 28 deletions(-) diff --git a/example/mlc.toml b/example/mlc.toml index b1e407f..9a56512 100644 --- a/example/mlc.toml +++ b/example/mlc.toml @@ -4,9 +4,13 @@ smart_pull = true [mode.repository] name = "test" -sign = false build_on_update = true +[mode.repository.signing] +enabled = true +key = "" +on_gen = true + [mode.workspace] [repositories] diff --git a/src/internal/structs.rs b/src/internal/structs.rs index 978eda0..a44a0d6 100755 --- a/src/internal/structs.rs +++ b/src/internal/structs.rs @@ -3,12 +3,9 @@ use serde_derive::Deserialize; //// Config structs #[derive(Debug, Deserialize)] pub struct Config { - pub mode: String, - pub name: String, - pub sign: bool, - pub smart_pull: bool, - pub build_on_update: bool, - pub repo: Vec, + pub base: ConfigBase, + pub mode: ConfigMode, + pub repositories: Vec, } #[derive(Debug, Deserialize)] @@ -33,8 +30,15 @@ pub struct ConfigMode { #[derive(Debug, Deserialize)] pub struct ConfigModeRepository { pub name: String, - pub sign: bool, pub build_on_update: bool, + pub signing: ConfigModeRepositorySigning, +} + +#[derive(Debug, Deserialize)] +pub struct ConfigModeRepositorySigning { + pub enabled: bool, + pub key: String, + pub on_gen: bool, } #[derive(Debug, Deserialize)] @@ -46,6 +50,11 @@ pub struct ConfigRepositories { pub urls: Vec, } +#[derive(Debug, Deserialize)] +pub struct ConfigRepositoriesExpanded { + pub repos: Vec, +} + //// Repository structs #[derive(Debug, Deserialize)] pub struct Repo { diff --git a/src/main.rs b/src/main.rs index 358b96f..5583bea 100755 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ fn main() { env::set_current_dir("../").unwrap(); log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); - if config.smart_pull { + if config.base.smart_pull { log!(verbose, "Smart pull"); Command::new("git") .args(&["remote", "update"]) @@ -80,13 +80,13 @@ fn main() { Operation::Pull { packages, .. } => operations::pull(packages, exclude.to_vec(), verbose), Operation::RepoGen => { let config = read_cfg(verbose); - if config.mode != "repository" { + if config.base.mode != "repository" { crash!( AppExitCode::BuildInWorkspace, "Cannot build packages in workspace mode" ) } - info!("Generating repository: {}", config.name); + info!("Generating repository: {}", config.mode.repository.name); repository::generate(verbose); } Operation::Config => operations::config(verbose), diff --git a/src/operations/build.rs b/src/operations/build.rs index f22fdef..302e99f 100644 --- a/src/operations/build.rs +++ b/src/operations/build.rs @@ -8,10 +8,15 @@ pub fn build(packages: Vec, exclude: Vec, no_regen: bool, verbos log!(verbose, "Config: {:?}", config); let all = packages.is_empty(); log!(verbose, "All: {:?}", all); - log!(verbose, "Signing: {:?}", config.sign); + let sign = if config.mode.repository.signing.enabled && config.mode.repository.signing.on_gen { + false + } else { + config.mode.repository.signing.enabled + }; + log!(verbose, "Signing: {:?}", config.mode.repository.signing); // Get list of repos and subtract exclude - let mut repos: Vec = config.repo; + let mut repos: Vec = config.repositories; log!(verbose, "{} Repos: {:?}", repos.len(), repos); if !exclude.is_empty() { log!(verbose, "Exclude not empty: {:?}", exclude); @@ -42,7 +47,7 @@ pub fn build(packages: Vec, exclude: Vec, no_regen: bool, verbos } else { // Otherwise, build log!(verbose, "Building {}", pkg); - let code = repository::build(pkg, config.sign, verbose); + let code = repository::build(pkg, sign, verbose); log!( verbose, "Package {} finished with exit code: {:?}", @@ -70,7 +75,7 @@ pub fn build(packages: Vec, exclude: Vec, no_regen: bool, verbos log!(verbose, "Sorted: {:?}", repos); for pkg in repos { log!(verbose, "Building {}", pkg.name); - let code = repository::build(&pkg.name, config.sign, verbose); + let code = repository::build(&pkg.name, sign, verbose); log!( verbose, "Package {} finished with exit code: {:?}", diff --git a/src/operations/clone.rs b/src/operations/clone.rs index 837909e..3d78741 100644 --- a/src/operations/clone.rs +++ b/src/operations/clone.rs @@ -6,7 +6,7 @@ pub fn clone(verbose: bool) { // Read config struct from mlc.toml let config = workspace::read_cfg(verbose); log!(verbose, "Config: {:?}", config); - let repos = &config.repo; + let repos = &config.repositories; log!(verbose, "Repos: {:?}", repos); // Get a vector of all files/dirs in the current directory, excluding config file @@ -44,7 +44,7 @@ pub fn clone(verbose: bool) { // Clone all diff repos for r in repo_diff { log!(verbose, "Cloning {}", r.name); - info!("Cloning ({} mode): {}", config.mode, r.name); + info!("Cloning ({} mode): {}", config.base.mode, r.name); Command::new("git") .args(&["clone", &r.url, &r.name]) .spawn() diff --git a/src/operations/pull.rs b/src/operations/pull.rs index c9d3bab..b6b0b7f 100644 --- a/src/operations/pull.rs +++ b/src/operations/pull.rs @@ -46,7 +46,7 @@ fn do_the_pulling(repos: Vec, verbose: bool, smart_pull: bool, build_on_ // If build_on_update is set, rebuild package if build_on_update { - unimplemented!() + unimplemented!() // TODO: Implement build_on_update } } else { // If there are no changes, alert the user @@ -80,15 +80,15 @@ pub fn pull(packages: Vec, exclude: Vec, verbose: bool) { let all = packages.is_empty(); log!(verbose, "All: {}", all); // Read smart_pull from config - let smart_pull = config.smart_pull; + let smart_pull = config.base.smart_pull; log!(verbose, "Smart pull: {}", smart_pull); // Read build_on_update from config - let build_on_update = config.build_on_update; + let build_on_update = config.mode.repository.build_on_update; log!(verbose, "Build on update: {}", build_on_update); // Read repos from config let repos = config - .repo + .repositories .iter() .map(|x| x.name.clone()) .collect::>(); diff --git a/src/repository/repo.rs b/src/repository/repo.rs index 2896a13..a283536 100644 --- a/src/repository/repo.rs +++ b/src/repository/repo.rs @@ -10,7 +10,7 @@ pub fn generate(verbose: bool) { log!(verbose, "Config: {:?}", config); // Get repository name from config - let name = config.name; + let name = config.mode.repository.name; log!(verbose, "Name: {}", name); // If repository exists, delete it @@ -32,6 +32,34 @@ pub fn generate(verbose: bool) { .unwrap(); log!(verbose, "Copied out packages to {}", name); + // Sign all package files in repository if signing and on_gen are true + if config.mode.repository.signing.enabled && config.mode.repository.signing.on_gen { + // Directory stuff + let dir = env::current_dir().unwrap(); + log!(verbose, "Current dir: {:?}", dir); + env::set_current_dir(&name).unwrap(); + log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); + // Get a list of all .tar.* files in repository + let files = fs::read_dir("./").unwrap(); + for file in files { + let file = file.unwrap(); + let path = file.path(); + if path.extension().unwrap() == "zst" || path.extension().unwrap() == "xz" { + log!(verbose, "Signing {}", path.display()); + Command::new("bash") + .args(&["-c", &format!("gpg --default-key {} --detach-sign {}", config.mode.repository.signing.key, file.file_name().to_str().unwrap())]) + .spawn() + .unwrap() + .wait() + .unwrap(); + } + } + // Return to root dir + env::set_current_dir(dir).unwrap(); + log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); + log!(verbose, "Signed repository"); + } + // Enter repository directory env::set_current_dir(&name).unwrap(); log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); diff --git a/src/workspace/read.rs b/src/workspace/read.rs index 7df1cb0..1c4e439 100755 --- a/src/workspace/read.rs +++ b/src/workspace/read.rs @@ -67,12 +67,9 @@ pub fn read_cfg(verbose: bool) -> Config { // Returns parsed config file let conf = Config { - mode: config.base.mode, - smart_pull: config.base.smart_pull, - sign: config.mode.repository.sign, - name: config.mode.repository.name, - build_on_update: config.mode.repository.build_on_update, - repo: expanded_repos, + base: config.base, + mode: config.mode, + repositories: expanded_repos, }; log!(verbose, "Config: {:?}", conf); conf