From cee18255780926a633b43681e1a82a021c705d9d Mon Sep 17 00:00:00 2001 From: dkos Date: Thu, 14 May 2020 20:25:01 +0200 Subject: [PATCH] 0.1.2 --- Cargo.toml | 5 +- readme.md | 30 +++++----- src/app.rs | 24 ++++---- src/file_utils.rs | 2 +- src/git_utils.rs | 32 ++--------- src/log.rs | 34 ----------- src/main.rs | 22 +++++-- src/mvn_utils.rs | 144 +++++++++++++++++++++------------------------- 8 files changed, 116 insertions(+), 177 deletions(-) delete mode 100644 src/log.rs diff --git a/Cargo.toml b/Cargo.toml index 1953ade..a64b5ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gm-utils" -version = "0.1.1" +version = "0.1.2" authors = ["Dejan Kos "] edition = "2018" license = "Apache-2.0" @@ -12,7 +12,8 @@ description = "Cli tool for managing git branch and maven project version at the structopt = "0.3" roxmltree = "0.7.3" exitcode = "1.1.2" -colored = "1.8" +simplelog = "0.7.6" +log = "0.4.8" [[bin]] name = "gm" diff --git a/readme.md b/readme.md index 9415f98..5b0cd9b 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ Although there is a lot of options for this [use case](https://stackoverflow.com ## Usage ``` ~> gm --help - gm-utils 0.1.1 + gm-utils 0.1.2 USAGE: gm [FLAGS] [new-version] @@ -32,24 +32,26 @@ Although there is a lot of options for this [use case](https://stackoverflow.com ## Examples Change git branch and maven project version from project root: ``` ->gm test123 - -Checking if branch test123 already exists... -Creating new git branch test123 from master - -Branch test123 successfully created from master - -Current mvn project version 0.0.1-SNAPSHOT -Changing mvn project version to 0.0.1-test123-SNAPSHOT -Done +>gm test123 -d +18:17:09 [DEBUG] (1) gm::app: Received cli args = CliArgs { new_version: Some("test123"), debug: true, reset: false } +18:17:09 [DEBUG] (1) gm::app: Checking git is installed... +18:17:09 [DEBUG] (1) gm::app: Git installed. +18:17:09 [DEBUG] (1) gm::app: Checking maven is installed... +18:17:09 [DEBUG] (1) gm::app: Maven installed. +18:17:09 [DEBUG] (1) gm::app: Project working dir = "/home/user/current_project" +18:17:09 [ INFO] Creating new git branch test123 from master +18:17:09 [ INFO] Branch test successfully created from master +18:17:09 [ INFO] Current mvn project version 0.0.1-SNAPSHOT +18:17:09 [ INFO] Changing mvn project version to 0.0.1-test123-SNAPSHOT +18:17:11 [ INFO] Done ``` Reset version: ``` > gm -r -Current mvn project version 0.0.1-test123-SNAPSHOT -Changing mvn project version to 0.0.1-SNAPSHOT -Done +18:23:27 [ INFO] Current mvn project version 0.0.1-test123-SNAPSHOT +18:23:27 [ INFO] Changing mvn project version to 0.0.1-SNAPSHOT +18:23:28 [ INFO] Done ``` For debug output set flag `-d`. diff --git a/src/app.rs b/src/app.rs index eabb57d..d0452f1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,34 +1,30 @@ use std::{env, io}; use crate::git_utils::Git; -use crate::log::Logger; -use crate::mvn_utils::Mvn; +use crate::mvn_utils::{reset_version, set_new_version}; use crate::{validation, CliArgs}; pub fn run(args: &CliArgs) -> io::Result<()> { - let log = Logger::new(args.debug, "app"); - validation::validate_args(&args)?; - log.f_debug("Received cli args = ", &args); + debug!("Received cli args = {:?}", &args); - log.debug("Checking git is installed..."); + debug!("Checking git is installed..."); validation::git_available()?; - log.debug("Git installed."); + debug!("Git installed"); - log.debug("Checking maven is installed..."); + debug!("Checking maven is installed..."); validation::mvn_available()?; - log.debug("Maven installed."); + debug!("Maven installed"); let path = env::current_dir()?; - log.f_debug("Project working dir =", &path); + debug!("Project working dir = {:?}", &path); - let mvn = Mvn::new(&args); if let Some(ver) = &args.new_version { - let git = Git::open(path.clone(), &args)?; + let git = Git::open(path.clone())?; git.new_branch(ver)?; - mvn.set_new_version(ver, path.clone())?; + set_new_version(ver, path.clone())?; } else if args.reset { - mvn.reset_version(path.clone())?; + reset_version(path.clone())?; } Ok(()) diff --git a/src/file_utils.rs b/src/file_utils.rs index 3a06c86..75b1ea6 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -1,5 +1,5 @@ use io::Result; -use std::ffi::{OsString}; +use std::ffi::OsString; use std::fs::read_dir; use std::path::PathBuf; use std::{fs, io}; diff --git a/src/git_utils.rs b/src/git_utils.rs index 46649c1..4fd3b7e 100644 --- a/src/git_utils.rs +++ b/src/git_utils.rs @@ -4,17 +4,12 @@ use std::io::{Error, ErrorKind}; use std::path::PathBuf; use std::process::{Command, Output}; -use crate::log::Logger; -use crate::CliArgs; - const ERR_MSG: &str = "Error executing git command"; pub struct Git { - log: Logger, repository: Repository, } -#[derive(Debug)] pub struct Repository { project_path: PathBuf, } @@ -39,35 +34,20 @@ impl OutputHandler for Output { } impl Git { - pub fn open(project_path: PathBuf, args: &CliArgs) -> Result { + pub fn open(project_path: PathBuf) -> Result { let repo = Repository::open(project_path)?; - Ok(Git { - log: Logger::new(args.debug, "git-utils"), - repository: repo, - }) + Ok(Git { repository: repo }) } pub fn new_branch(&self, b_name: &str) -> Result<()> { - self.log - .info(format!("Checking if branch {} already exists...", b_name).as_str()); self.repository.branch_exists(b_name)?; - let current_branch = self.repository.current_branch()?; - self.log.info( - format!( - "Creating new git branch {} from {} ", - b_name, current_branch - ) - .as_str(), - ); + info!("Creating new git branch {} from {}", b_name, current_branch); self.repository.new_branch(b_name, current_branch.trim())?; - self.log.info( - format!( - "Branch {} successfully created from {}", - b_name, current_branch - ) - .as_str(), + info!( + "Branch {} successfully created from {}", + b_name, current_branch, ); Ok(()) diff --git a/src/log.rs b/src/log.rs deleted file mode 100644 index 74f4295..0000000 --- a/src/log.rs +++ /dev/null @@ -1,34 +0,0 @@ -use std::fmt::Debug; - -use colored::Colorize; - -pub struct Logger { - debug: bool, - name: &'static str, -} - -impl Logger { - pub fn new(debug: bool, name: &'static str) -> Self { - Logger { debug, name } - } - - pub fn info(&self, msg: &str) { - println!("{}", msg) - } - - pub fn debug(&self, msg: &str) { - if self.debug { - println!("{}::{}", self.name.green(), msg.green()) - } - } - - pub fn f_debug(&self, msg: &str, t: &T) { - if self.debug { - self.debug(format!("{}::{} {:?}", self.name, msg, t).as_str()) - } - } - - pub fn error(&self, e: &T) { - eprintln!("{}::{}", self.name.red(), format!("{:?}", e).red()) - } -} diff --git a/src/main.rs b/src/main.rs index 54ecd4c..9e052c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,16 @@ +#[macro_use] +extern crate log; +extern crate simplelog; + use std::process::exit; use exitcode::{OK, USAGE}; +use simplelog::{Config, LevelFilter, TermLogger, TerminalMode}; use structopt::StructOpt; -use crate::log::Logger; - mod app; mod file_utils; mod git_utils; -mod log; mod mvn_utils; mod validation; @@ -24,15 +26,23 @@ pub struct CliArgs { fn main() { let args = CliArgs::from_args(); - let log = Logger::new(args.debug, "main"); + TermLogger::init(log_lvl(args.debug), Config::default(), TerminalMode::Mixed).unwrap(); match app::run(&args) { Ok(_) => { - log.info("Done"); + info!("Done"); exit(OK); } Err(e) => { - log.error(&e); + error!("{}", &e); exit(USAGE); } } } + +fn log_lvl(debug: bool) -> LevelFilter { + if debug { + LevelFilter::Debug + } else { + LevelFilter::Info + } +} diff --git a/src/mvn_utils.rs b/src/mvn_utils.rs index 3674688..8356944 100644 --- a/src/mvn_utils.rs +++ b/src/mvn_utils.rs @@ -6,102 +6,86 @@ use std::process::Command; use roxmltree::Document; -use crate::log::Logger; -use crate::{file_utils, CliArgs}; +use crate::file_utils; const SUCCESS: &str = "BUILD SUCCESS"; const POM_FILE: &str = "pom.xml"; -pub struct Mvn { - log: Logger, -} +pub fn set_new_version(ver: &str, project_path: PathBuf) -> Result<()> { + let split: Vec = split_current_ver(&project_path)?; + let new_version = String::from(format!("{}-{}-{}", split[0], ver, split[1])); + info!("Changing mvn project version to {}", &new_version); -impl Mvn { - pub fn new(args: &CliArgs) -> Self { - Mvn { - log: Logger::new(args.debug, "mvn"), - } - } + change_version(&new_version, &project_path) +} - pub fn set_new_version(&self, ver: &str, project_path: PathBuf) -> Result<()> { - let split: Vec = self.split_current_ver(&project_path)?; - let new_version = String::from(format!("{}-{}-{}", split[0], ver, split[1])); - self.log - .info(format!("Changing mvn project version to {}", &new_version).as_str()); +pub fn reset_version(project_path: PathBuf) -> Result<()> { + let split: Vec = split_current_ver(&project_path)?; + let new_version = format!("{}-{}", split[0], "SNAPSHOT"); + info!("Changing mvn project version to {}", &new_version); - self.change_version(&new_version, &project_path) - } - - pub fn reset_version(&self, project_path: PathBuf) -> Result<()> { - let split: Vec = self.split_current_ver(&project_path)?; - let new_version = format!("{}-{}", split[0], "SNAPSHOT"); - self.log - .info(format!("Changing mvn project version to {}", &new_version).as_str()); + change_version(&new_version, &project_path) +} - self.change_version(&new_version, &project_path) - } +fn split_current_ver(project_path: &PathBuf) -> Result> { + let current_ver = find_version(&project_path)?; + info!("Current mvn project version {}", current_ver); + let split: Vec = current_ver.split("-").map(|s| s.to_string()).collect(); - fn split_current_ver(&self, project_path: &PathBuf) -> Result> { - let current_ver = self.find_version(&project_path)?; - self.log - .info(format!("Current mvn project version {}", current_ver).as_str()); - let split: Vec = current_ver.split("-").map(|s| s.to_string()).collect(); + Ok(split) +} - Ok(split) +fn change_version(new_version: &str, project_path: &PathBuf) -> Result<()> { + let out = Command::new("mvn") + .args(&[ + "-f", + format!("{}/{}", project_path.to_str().unwrap(), POM_FILE).as_str(), + "versions:set", + format!("-DnewVersion={}", new_version).as_str(), + ]) + .output() + .expect("Failed to change mvn version!"); + + let out_str = String::from_utf8(out.stdout).unwrap(); + if !out_str.contains(SUCCESS) { + return Err(Error::new( + ErrorKind::InvalidData, + "Never thought this was gonna work anyway", + )); } - fn change_version(&self, new_version: &str, project_path: &PathBuf) -> Result<()> { - let out = Command::new("mvn") - .args(&[ - "-f", - format!("{}/{}", project_path.to_str().unwrap(), POM_FILE).as_str(), - "versions:set", - format!("-DnewVersion={}", new_version).as_str(), - ]) - .output() - .expect("Failed to change mvn version!"); - - let out_str = String::from_utf8(out.stdout).unwrap(); - if !out_str.contains(SUCCESS) { - return Err(Error::new( - ErrorKind::InvalidData, - "Never thought this was gonna work anyway", - )); - } - - Ok(()) - } + Ok(()) +} - fn find_version(&self, project_path: &PathBuf) -> Result { - if let Some(res) = file_utils::find_in_dir(&project_path, POM_FILE) { - let c = file_utils::read_file_content(&res)?; - self.parse_pom_ver(c).ok_or(Error::new( - ErrorKind::InvalidData, - "Failed to parse project version", - )) - } else { - Err(Error::new(ErrorKind::InvalidData, "Pom file not found")) - } +fn find_version(project_path: &PathBuf) -> Result { + if let Some(res) = file_utils::find_in_dir(&project_path, POM_FILE) { + let c = file_utils::read_file_content(&res)?; + parse_pom_ver(c).ok_or(Error::new( + ErrorKind::InvalidData, + "Failed to parse project version", + )) + } else { + Err(Error::new(ErrorKind::InvalidData, "Pom file not found")) } +} - fn parse_pom_ver(&self, pom_xml: String) -> Option { - let doc = match Document::parse(pom_xml.as_str()) { - Ok(r) => r, - Err(_) => return None, - }; - - for n in doc.root().descendants() { - if n.is_element() - && n.has_tag_name("version") - && !n.parent().unwrap().has_tag_name("parent") - && n.text().is_some() - { - if let Some(txt) = n.text() { - return Some(txt.into()); - } +fn parse_pom_ver(pom_xml: String) -> Option { + let doc = match Document::parse(pom_xml.as_str()) { + Ok(r) => r, + Err(_) => return None, + }; + + for n in doc.root().descendants() { + if n.is_element() + && n.has_tag_name("version") + && !n.parent().unwrap().has_tag_name("parent") + && n.text().is_some() + { + if let Some(txt) = n.text() { + return Some(txt.into()); } } - - None } + + None }