Compartmentalised AppExitCodes

main
Michal 2 years ago
parent 4467f08ac8
commit 9fc3677ae3

@ -1,14 +1,9 @@
pub enum AppExitCode { pub enum AppExitCode {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
RunAsRoot = 1, RunAsRoot = 1,
BuildInWorkspace = 2, PkgsNotFound = 2,
PkgNotFound = 3, DirNotEmpty = 3,
InvalidMode = 4, ConfigParseError = 4,
DirNotEmpty = 5, RepoParseError = 5,
ConfigNotFound = 6, RepoNotClean = 6,
NoPkgs = 7,
ConfigParseError = 8,
InvalidRepo = 9,
NotInit = 10,
NotClean = 11,
} }

@ -9,7 +9,7 @@ pub fn parse_cfg(verbose: bool) -> Config {
// Crash if mlc.toml doesn't exist // Crash if mlc.toml doesn't exist
if !Path::exists("mlc.toml".as_ref()) { if !Path::exists("mlc.toml".as_ref()) {
crash!( crash!(
AppExitCode::ConfigNotFound, AppExitCode::ConfigParseError,
"Config file not found (mlc.toml)" "Config file not found (mlc.toml)"
); );
} }
@ -31,7 +31,7 @@ pub fn parse_cfg(verbose: bool) -> Config {
// Crash if incorrect mode is set // Crash if incorrect mode is set
if config.base.mode != "workspace" && config.base.mode != "repository" { if config.base.mode != "workspace" && config.base.mode != "repository" {
crash!( crash!(
AppExitCode::InvalidMode, AppExitCode::ConfigParseError,
"Invalid mode in mlc.toml, must be either \"repository\" or \"workspace\"" "Invalid mode in mlc.toml, must be either \"repository\" or \"workspace\""
); );
} }

@ -51,7 +51,7 @@ fn main() {
} => { } => {
if !repository(verbose) { if !repository(verbose) {
crash!( crash!(
AppExitCode::BuildInWorkspace, AppExitCode::ConfigParseError,
"Cannot build packages in workspace mode" "Cannot build packages in workspace mode"
); );
} }
@ -63,7 +63,7 @@ fn main() {
Operation::RepoGen => { Operation::RepoGen => {
if !repository(verbose) { if !repository(verbose) {
crash!( crash!(
AppExitCode::BuildInWorkspace, AppExitCode::ConfigParseError,
"Cannot generate repository in workspace mode" "Cannot generate repository in workspace mode"
); );
} }
@ -73,7 +73,7 @@ fn main() {
Operation::Prune => { Operation::Prune => {
if !repository(verbose) { if !repository(verbose) {
crash!( crash!(
AppExitCode::BuildInWorkspace, AppExitCode::ConfigParseError,
"Cannot prune packages in workspace mode" "Cannot prune packages in workspace mode"
); );
} }

@ -68,7 +68,7 @@ pub fn build(packages: &[String], exclude: Vec<String>, no_regen: bool, verbose:
} }
} else { } else {
crash!( crash!(
AppExitCode::PkgNotFound, AppExitCode::PkgsNotFound,
"Package repo {} not found in in mlc.toml", "Package repo {} not found in in mlc.toml",
pkg pkg
); );
@ -108,7 +108,7 @@ pub fn build(packages: &[String], exclude: Vec<String>, no_regen: bool, verbose:
// If all is not specified, but packages is empty, crash // If all is not specified, but packages is empty, crash
if !all && packages.is_empty() { if !all && packages.is_empty() {
log!(verbose, "Packages empty. Crashing"); 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 // If no_regen is passed, do not generate a repository

@ -45,7 +45,7 @@ pub fn clean(verbose: bool, force: bool) {
if !unclean_dirs.is_empty() && !force && crate::parse_cfg(verbose).base.mode == "workspace" { if !unclean_dirs.is_empty() && !force && crate::parse_cfg(verbose).base.mode == "workspace" {
crash!( crash!(
AppExitCode::NotClean, AppExitCode::RepoNotClean,
"The following directories are not clean: \n {}\n\ "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.", 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::<Vec<String>>().join(", ") unclean_dirs.iter().map(|x| (*x).to_string().replace("./", "").replace(".\\", "")).collect::<Vec<String>>().join(", ")

@ -63,7 +63,7 @@ pub fn clone(verbose: bool) {
if r.extra.is_some() && config.base.mode == "workspace" { if r.extra.is_some() && config.base.mode == "workspace" {
info!( info!(
"Cloning ({} mode): {} at depth: {}", "Cloning ({} mode): {} with `--depth {}`",
config.base.mode, config.base.mode,
r.name, r.name,
r.extra.as_ref().unwrap() r.extra.as_ref().unwrap()

@ -38,7 +38,7 @@ pub fn git_status(verbose: bool, repo: &str, colorblind: bool) -> String {
); );
env::set_current_dir(&repo).unwrap_or_else(|e| { env::set_current_dir(&repo).unwrap_or_else(|e| {
crash!( crash!(
AppExitCode::NotInit, AppExitCode::RepoParseError,
"Failed to enter directory {} for Git info: {}, Have you initialized the repo?", "Failed to enter directory {} for Git info: {}, Have you initialized the repo?",
repo, repo,
e.to_string() e.to_string()

@ -135,7 +135,7 @@ pub fn pull(packages: Vec<String>, exclude: &[String], verbose: bool, no_regen:
// If all is not specified and packages is empty, crash // If all is not specified and packages is empty, crash
if repos_applicable.is_empty() { if repos_applicable.is_empty() {
crash!(AppExitCode::NoPkgs, "No packages specified"); crash!(AppExitCode::PkgsNotFound, "No packages specified");
} }
// Sort repos_applicable by priority // Sort repos_applicable by priority
@ -158,14 +158,15 @@ pub fn pull(packages: Vec<String>, exclude: &[String], verbose: bool, no_regen:
log!(verbose, "Pulling {:?}", repos_applicable); 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 { for repo in &repos_applicable {
if !std::path::Path::new(repo).exists() { if !std::path::Path::new(repo).exists() {
crash!( info!(
AppExitCode::NoPkgs, "Repo {} does not exist, ensuring all repos are cloned",
"Package {} does not exist, have you run `mlc clone/init`?",
repo repo
); );
crate::operations::clone(verbose);
break;
} }
} }

@ -22,7 +22,7 @@ pub fn build(pkg: &str, sign: bool, verbose: bool) -> i32 {
// If package directory is not found, crash // If package directory is not found, crash
if !Path::exists(pkg.as_ref()) { if !Path::exists(pkg.as_ref()) {
crash!( crash!(
AppExitCode::PkgNotFound, AppExitCode::PkgsNotFound,
"Repo for package {} not found, aborting", "Repo for package {} not found, aborting",
pkg pkg
); );

@ -9,10 +9,22 @@ pub fn generate(verbose: bool) {
let config = parse_cfg(verbose); let config = parse_cfg(verbose);
log!(verbose, "Config: {:?}", config); 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 // Get repository name from config
let name = &config.mode.repository.as_ref().unwrap().name; let name = &config.mode.repository.as_ref().unwrap().name;
log!(verbose, "Name: {}", 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); info!("Generating repository: {}", name);
// If repository exists, delete it // If repository exists, delete it
@ -39,61 +51,19 @@ pub fn generate(verbose: bool) {
log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); log!(verbose, "Current dir: {:?}", env::current_dir().unwrap());
// Sign all package files in repository if signing and on_gen are true // Sign all package files in repository if signing and on_gen are true
if config.mode.repository.as_ref().unwrap().signing.enabled if *signing && on_gen.is_some() && on_gen.unwrap() {
&& config
.mode
.repository
.as_ref()
.unwrap()
.signing
.on_gen
.is_some()
&& config
.mode
.repository
.as_ref()
.unwrap()
.signing
.on_gen
.unwrap()
{
// Get a list of all .tar.* files in repository // Get a list of all .tar.* files in repository
let files = fs::read_dir(".").unwrap(); let files = fs::read_dir(".").unwrap();
for file in files { for file in files {
// Get file name // Get file name
let file = file.unwrap(); let file = file.unwrap();
let path = file.path(); let path = file.path();
let sign_command = if config let sign_command = if key.is_some() && !key.as_ref().unwrap().is_empty() {
.mode
.repository
.as_ref()
.unwrap()
.signing
.key
.is_some()
&& !config
.mode
.repository
.as_ref()
.unwrap()
.signing
.key
.as_ref()
.unwrap()
.is_empty()
{
format!( format!(
"gpg --default-key {} --detach-sign {}", "gpg --default-key {} --detach-sign {}",
config key.as_ref().unwrap(),
.mode
.repository
.as_ref()
.unwrap()
.signing
.key
.as_ref()
.unwrap(),
path.to_str().unwrap() path.to_str().unwrap()
) )
} else { } else {
@ -135,7 +105,7 @@ pub fn generate(verbose: bool) {
// This should never happen, crash and burn if it does // This should never happen, crash and burn if it does
if zst.success() && xz.success() { if zst.success() && xz.success() {
crash!( crash!(
AppExitCode::InvalidRepo, AppExitCode::RepoParseError,
"Both .tar.zst and .tar.xz files found in repository. You've done something wrong. Aborting" "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 true
} else { } else {
crash!( crash!(
AppExitCode::NoPkgs, AppExitCode::PkgsNotFound,
"No .zst or .xz packages found in repository" "No .zst or .xz packages found in repository"
); );
// This should theoretically never be reached, but let's just give the compiler what it wants // This should theoretically never be reached, but let's just give the compiler what it wants

Loading…
Cancel
Save