Compartmentalised AppExitCodes

main
Michal 2 years ago
parent e581c60d39
commit 5e0d50e995
No known key found for this signature in database
GPG Key ID: A6A1A4DCB22279B9

@ -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,
}

@ -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\""
);
}

@ -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"
);
}

@ -68,7 +68,7 @@ pub fn build(packages: &[String], exclude: Vec<String>, 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<String>, 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

@ -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::<Vec<String>>().join(", ")

@ -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()

@ -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()

@ -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 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<String>, 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;
}
}

@ -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
);

@ -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

Loading…
Cancel
Save