diff options
author | Henrique Alves <henriquelalves@gmail.com> | 2022-07-26 11:16:51 -0300 |
---|---|---|
committer | Henrique Alves <henriquelalves@gmail.com> | 2022-07-26 11:16:51 -0300 |
commit | c4c208c15575e64907291d4567cd72d5ceb7c0ef (patch) | |
tree | 0e50ecaaa62a152de61ad8ebda2f06bf5a6f109b | |
parent | ef02e2bbd6e0bebfc39f21c1a790c8c06d856eb3 (diff) | |
download | glam-c4c208c15575e64907291d4567cd72d5ceb7c0ef.tar.gz glam-c4c208c15575e64907291d4567cd72d5ceb7c0ef.tar.bz2 glam-c4c208c15575e64907291d4567cd72d5ceb7c0ef.zip |
Asserting results form shell commands
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/commands.rs | 37 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/utils.rs | 9 |
4 files changed, 40 insertions, 10 deletions
@@ -16,4 +16,5 @@ categories = ["command-line-utilities"] clap = { version = "3.0", features = ["derive"] } serde = { version = "1.0.140", features = ["derive"] } colored = "2" -serde_json = "1.0"
\ No newline at end of file +serde_json = "1.0" +assert_fs = "1.0"
\ No newline at end of file diff --git a/src/commands.rs b/src/commands.rs index 4a97bb6..4916234 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -207,30 +207,36 @@ fn install_glam_package(root : &str, commit : &str, package : &mut GlamPackage, package.commit = res.trim().to_string(); } else { utils::log_info("Git checkout to package commit"); - utils::run_shell_command( + let res = utils::run_shell_command( &format!("cd .glam.d/{} && git checkout {}", package.name, package.commit), &root, verbose); + + utils::assert_res(&res, "Couldn't checkout repository!"); } if copy_files { // If project addon folder doesn't exist, create it - utils::run_shell_command( + let res = utils::run_shell_command( &format!("mkdir -p {}/addons/{}", root, package.name), &root, verbose ); + utils::assert_res(&res, "Couldn't create addons folder!"); + // TODO: use source_folder to copy files from (default: /addons/) // TODO: use target_folder to copy files to (defautl: (root)/) // Copy addon repository content to target folder - utils::run_shell_command( + let res = utils::run_shell_command( &format!("cp -rf .glam.d/{}/addons/* -t {}/addons/", package.name, root), &root, verbose ); + + utils::assert_res(&res, "Couldn't copy files to addons!"); } } @@ -238,38 +244,47 @@ fn apply_glam_package_files(root : &str, package : &GlamPackage, verbose : bool) // TODO: use source_folder to copy files from (default: /addons/) // TODO: use target_folder to copy files to (defautl: (root)/) // Copy addon repository content to target folder - utils::run_shell_command( + let res = utils::run_shell_command( &format!("for f in $(ls ./.glam.d/{}/addons/); do cp -rf ./addons/$f ./.glam.d/{}/addons/; done", package.name, package.name), &root, verbose ); + + utils::assert_res(&res, "Couldn't copy files to repository!"); } fn remove_glam_package_files(root : &str, package : &GlamPackage, verbose : bool) { // TODO: use source_folder to copy files from (default: /addons/) // TODO: use target_folder to copy files to (defautl: (root)/) // Copy addon repository content to target folder - utils::run_shell_command( + let res = utils::run_shell_command( &format!("for f in $(ls ./.glam.d/{}/addons/); do rm -rf ./addons/$f ; done", package.name), &root, verbose ); - utils::run_shell_command( + utils::assert_res(&res, "Couldn't remove files from root addons!"); + + let res = utils::run_shell_command( &format!("rm -rf ./.glam.d/{}", package.name), &root, verbose ); + + utils::assert_res(&res, "Couldn't copy files to addons!"); } pub fn initialize_glam_files(root : &str) { // Create glam.d/ folder if it doesn't exist if !Path::new(&format!("{}/.glam.d/", root)).exists() { - utils::run_shell_command( + let res = utils::run_shell_command( &format!("mkdir -p {}/.glam.d/", root), &root, false ); + + utils::assert_res(&res, "Couldn't create .glam.d/ folder!"); + utils::log_info("Created .glam.d/ folder"); } @@ -285,18 +300,22 @@ pub fn initialize_glam_files(root : &str) { fn clone_or_fetch_package(root : &str, package : &GlamPackage, verbose : bool) { // If glam package folder doesn't exist, clone project if !Path::new(&format!("{}/.glam.d/{}", root, package.name)).exists() { - utils::run_shell_command( + let res = utils::run_shell_command( &format!("cd .glam.d/ && git clone {} {} --progress", package.git_repo, package.name), &root, verbose ); + + utils::assert_res(&res, "Couldn't clone repository!"); + utils::log_check("Created package folder on .glam.d"); } else { - utils::run_shell_command( + let res = utils::run_shell_command( &format!("cd .glam.d/{} && git fetch origin && git pull", package.name), &root, verbose ); + utils::assert_res(&res, "Couldn't fetch package repository updates!"); utils::log_info("Glam package folder already exists, fetched and pulled latest changes"); } diff --git a/src/main.rs b/src/main.rs index 5d05698..3726b23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ enum Commands { package_name: String, }, + // TODO: Add "push" flag to automate applying changes, commiting them and pushing /// Apply changes to a package Apply { /// Name of the package to apply changes to diff --git a/src/utils.rs b/src/utils.rs index e925f9d..81bd63f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ use colored::Colorize; use std::io::Write; +use std::process::exit; pub fn log_warning(msg: &str) { let msg = format!("⚠️ {}", msg).yellow(); @@ -21,6 +22,14 @@ pub fn log_check(msg: &str) { println!("{}", msg); } +pub fn assert_res(res : &Result<String, String>, error_msg : &str) { + if res.is_err() { + log_error(error_msg); + log_error(&format!("{:?}", res)); + exit(1); + } +} + pub fn get_repo_name(repo: &str) -> String { let mut chars = repo.chars().rev(); let length = repo.chars().count(); |