From 9fc3677ae3213bce5f766bf539c62e813535a254 Mon Sep 17 00:00:00 2001 From: Michal Date: Mon, 25 Jul 2022 19:44:17 +0100 Subject: [PATCH] Compartmentalised AppExitCodes --- src/internal/exit_codes.rs | 15 +++------ src/internal/read.rs | 4 +-- src/main.rs | 6 ++-- src/operations/build.rs | 4 +-- src/operations/clean.rs | 2 +- src/operations/clone.rs | 2 +- src/operations/info.rs | 2 +- src/operations/pull.rs | 11 ++++--- src/repository/package.rs | 2 +- src/repository/repo.rs | 66 +++++++++++--------------------------- 10 files changed, 40 insertions(+), 74 deletions(-) diff --git a/src/internal/exit_codes.rs b/src/internal/exit_codes.rs index 5b04209..b850e1d 100644 --- a/src/internal/exit_codes.rs +++ b/src/internal/exit_codes.rs @@ -1,14 +1,9 @@ pub enum AppExitCode { #[cfg(target_os = "linux")] RunAsRoot = 1, - BuildInWorkspace = 2, - PkgNotFound = 3, - InvalidMode = 4, - DirNotEmpty = 5, - ConfigNotFound = 6, - NoPkgs = 7, - ConfigParseError = 8, - InvalidRepo = 9, - NotInit = 10, - NotClean = 11, + PkgsNotFound = 2, + DirNotEmpty = 3, + ConfigParseError = 4, + RepoParseError = 5, + RepoNotClean = 6, } diff --git a/src/internal/read.rs b/src/internal/read.rs index 7cd9bd7..e960060 100644 --- a/src/internal/read.rs +++ b/src/internal/read.rs @@ -9,7 +9,7 @@ pub fn parse_cfg(verbose: bool) -> Config { // Crash if mlc.toml doesn't exist if !Path::exists("mlc.toml".as_ref()) { crash!( - AppExitCode::ConfigNotFound, + AppExitCode::ConfigParseError, "Config file not found (mlc.toml)" ); } @@ -31,7 +31,7 @@ pub fn parse_cfg(verbose: bool) -> Config { // Crash if incorrect mode is set if config.base.mode != "workspace" && config.base.mode != "repository" { crash!( - AppExitCode::InvalidMode, + AppExitCode::ConfigParseError, "Invalid mode in mlc.toml, must be either \"repository\" or \"workspace\"" ); } diff --git a/src/main.rs b/src/main.rs index cbddd02..9cfc99a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ fn main() { } => { if !repository(verbose) { crash!( - AppExitCode::BuildInWorkspace, + AppExitCode::ConfigParseError, "Cannot build packages in workspace mode" ); } @@ -63,7 +63,7 @@ fn main() { Operation::RepoGen => { if !repository(verbose) { crash!( - AppExitCode::BuildInWorkspace, + AppExitCode::ConfigParseError, "Cannot generate repository in workspace mode" ); } @@ -73,7 +73,7 @@ fn main() { Operation::Prune => { if !repository(verbose) { crash!( - AppExitCode::BuildInWorkspace, + AppExitCode::ConfigParseError, "Cannot prune packages in workspace mode" ); } diff --git a/src/operations/build.rs b/src/operations/build.rs index 49c3db3..8a26888 100644 --- a/src/operations/build.rs +++ b/src/operations/build.rs @@ -68,7 +68,7 @@ pub fn build(packages: &[String], exclude: Vec, no_regen: bool, verbose: } } else { crash!( - AppExitCode::PkgNotFound, + AppExitCode::PkgsNotFound, "Package repo {} not found in in mlc.toml", pkg ); @@ -108,7 +108,7 @@ pub fn build(packages: &[String], exclude: Vec, no_regen: bool, verbose: // If all is not specified, but packages is empty, crash if !all && packages.is_empty() { log!(verbose, "Packages empty. Crashing"); - crash!(AppExitCode::NoPkgs, "No packages specified"); + crash!(AppExitCode::PkgsNotFound, "No packages specified"); } // If no_regen is passed, do not generate a repository diff --git a/src/operations/clean.rs b/src/operations/clean.rs index cf52050..b4761f6 100644 --- a/src/operations/clean.rs +++ b/src/operations/clean.rs @@ -45,7 +45,7 @@ pub fn clean(verbose: bool, force: bool) { if !unclean_dirs.is_empty() && !force && crate::parse_cfg(verbose).base.mode == "workspace" { crash!( - AppExitCode::NotClean, + AppExitCode::RepoNotClean, "The following directories are not clean: \n {}\n\ If you are sure no important changes are staged, run `mlc clean` with the `--force` flag to delete them.", unclean_dirs.iter().map(|x| (*x).to_string().replace("./", "").replace(".\\", "")).collect::>().join(", ") diff --git a/src/operations/clone.rs b/src/operations/clone.rs index 09e2536..f7ab993 100644 --- a/src/operations/clone.rs +++ b/src/operations/clone.rs @@ -63,7 +63,7 @@ pub fn clone(verbose: bool) { if r.extra.is_some() && config.base.mode == "workspace" { info!( - "Cloning ({} mode): {} at depth: {}", + "Cloning ({} mode): {} with `--depth {}`", config.base.mode, r.name, r.extra.as_ref().unwrap() diff --git a/src/operations/info.rs b/src/operations/info.rs index d43a174..6baa58f 100644 --- a/src/operations/info.rs +++ b/src/operations/info.rs @@ -38,7 +38,7 @@ pub fn git_status(verbose: bool, repo: &str, colorblind: bool) -> String { ); env::set_current_dir(&repo).unwrap_or_else(|e| { crash!( - AppExitCode::NotInit, + AppExitCode::RepoParseError, "Failed to enter directory {} for Git info: {}, Have you initialized the repo?", repo, e.to_string() diff --git a/src/operations/pull.rs b/src/operations/pull.rs index 7f179bb..184697b 100644 --- a/src/operations/pull.rs +++ b/src/operations/pull.rs @@ -135,7 +135,7 @@ pub fn pull(packages: Vec, exclude: &[String], verbose: bool, no_regen: // If all is not specified and packages is empty, crash if repos_applicable.is_empty() { - crash!(AppExitCode::NoPkgs, "No packages specified"); + crash!(AppExitCode::PkgsNotFound, "No packages specified"); } // Sort repos_applicable by priority @@ -158,14 +158,15 @@ pub fn pull(packages: Vec, exclude: &[String], verbose: bool, no_regen: log!(verbose, "Pulling {:?}", repos_applicable); - // If the directories specified in repos_applicable do not exist, crash + // If any repos are not in the config, run a clone for repo in &repos_applicable { if !std::path::Path::new(repo).exists() { - crash!( - AppExitCode::NoPkgs, - "Package {} does not exist, have you run `mlc clone/init`?", + info!( + "Repo {} does not exist, ensuring all repos are cloned", repo ); + crate::operations::clone(verbose); + break; } } diff --git a/src/repository/package.rs b/src/repository/package.rs index bf7e41f..544c4ed 100755 --- a/src/repository/package.rs +++ b/src/repository/package.rs @@ -22,7 +22,7 @@ pub fn build(pkg: &str, sign: bool, verbose: bool) -> i32 { // If package directory is not found, crash if !Path::exists(pkg.as_ref()) { crash!( - AppExitCode::PkgNotFound, + AppExitCode::PkgsNotFound, "Repo for package {} not found, aborting", pkg ); diff --git a/src/repository/repo.rs b/src/repository/repo.rs index e7211ac..2447fda 100644 --- a/src/repository/repo.rs +++ b/src/repository/repo.rs @@ -9,10 +9,22 @@ pub fn generate(verbose: bool) { let config = parse_cfg(verbose); log!(verbose, "Config: {:?}", config); + // Get signing from config + let signing = &config.mode.repository.as_ref().unwrap().signing.enabled; + log!(verbose, "Signing: {:?}", signing); + // Get repository name from config let name = &config.mode.repository.as_ref().unwrap().name; log!(verbose, "Name: {}", name); + // Read on_gen from config + let on_gen = &config.mode.repository.as_ref().unwrap().signing.on_gen; + log!(verbose, "On gen: {:?}", on_gen); + + // Read key from config + let key = &config.mode.repository.as_ref().unwrap().signing.key; + log!(verbose, "Key: {:?}", key); + info!("Generating repository: {}", name); // If repository exists, delete it @@ -39,61 +51,19 @@ pub fn generate(verbose: bool) { log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); // Sign all package files in repository if signing and on_gen are true - if config.mode.repository.as_ref().unwrap().signing.enabled - && config - .mode - .repository - .as_ref() - .unwrap() - .signing - .on_gen - .is_some() - && config - .mode - .repository - .as_ref() - .unwrap() - .signing - .on_gen - .unwrap() - { + if *signing && on_gen.is_some() && on_gen.unwrap() { // Get a list of all .tar.* files in repository let files = fs::read_dir(".").unwrap(); + for file in files { // Get file name let file = file.unwrap(); let path = file.path(); - let sign_command = if config - .mode - .repository - .as_ref() - .unwrap() - .signing - .key - .is_some() - && !config - .mode - .repository - .as_ref() - .unwrap() - .signing - .key - .as_ref() - .unwrap() - .is_empty() - { + let sign_command = if key.is_some() && !key.as_ref().unwrap().is_empty() { format!( "gpg --default-key {} --detach-sign {}", - config - .mode - .repository - .as_ref() - .unwrap() - .signing - .key - .as_ref() - .unwrap(), + key.as_ref().unwrap(), path.to_str().unwrap() ) } else { @@ -135,7 +105,7 @@ pub fn generate(verbose: bool) { // This should never happen, crash and burn if it does if zst.success() && xz.success() { crash!( - AppExitCode::InvalidRepo, + AppExitCode::RepoParseError, "Both .tar.zst and .tar.xz files found in repository. You've done something wrong. Aborting" ); } @@ -147,7 +117,7 @@ pub fn generate(verbose: bool) { true } else { crash!( - AppExitCode::NoPkgs, + AppExitCode::PkgsNotFound, "No .zst or .xz packages found in repository" ); // This should theoretically never be reached, but let's just give the compiler what it wants