Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Feature/add logger #20

Merged
merged 4 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
ci/Cargo.lock
cli/Cargo.lock
git/Cargo.lock
logger/Cargo.lock
utils/Cargo.lock
target/
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition = "2018"
ci = { path = "ci" }
cli = { path = "cli" }
git = { path = "git" }
logger = { path= "logger" }

[dev-dependencies]
rand = "0.7"
3 changes: 3 additions & 0 deletions ci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ name = "ci"
version = "0.1.0"
authors = ["KNP Labs"]
edition = "2018"

[dependencies]
log = "0.4.8"
12 changes: 11 additions & 1 deletion ci/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
extern crate log;

use std::process::Command;
use log::debug;

pub fn run_stop_cmd(cmd: &String) -> i32 {
// As the cmd to run is a single string, we run it though a shell
// otherwise rust will look for an executable which *is* the whole
// `cmd` string.
let result = Command::new("/bin/sh")
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg(cmd)
;

debug!("Running `{:?}`", command);

let result = command
.output()
.expect("Unable to run the stop command.")
;


return result.status.code().unwrap();
}

Expand Down
9 changes: 9 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ struct RawCli {

#[structopt(long = "cmd", help = "The command to use to skip the build.")]
cmd: String,

// The number of occurrences of the `v/verbose` flag
/// Verbose mode (-v, -vv, -vvv, etc.)
#[structopt(short, long, parse(from_occurrences), help = "Verbosity mode : -v, -vv")]
verbosity: u8,
}

// The Cli struct represents the resolved CLI args and options.
Expand Down Expand Up @@ -57,4 +62,8 @@ impl Cli {
pub fn cmd(&self) -> &String {
return &self.raw_cli.cmd;
}

pub fn verbosity(&self) -> &u8 {
return &self.raw_cli.verbosity;
}
}
1 change: 1 addition & 0 deletions git/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2018"

[dependencies]
utils = { path = "../utils" }
log = "0.4.8"
27 changes: 24 additions & 3 deletions git/src/branch.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
extern crate log;

use std::path::PathBuf;
use std::process::Command;
use log::debug;

use utils::assert_or_panic;

pub fn get_current_branch(
working_directory: &PathBuf,
) -> String {
let result = Command::new("git")
let mut cmd = Command::new("git");
cmd
.arg("rev-parse")
.arg("--abbrev-ref")
.arg("HEAD")
.current_dir(&working_directory)
;

debug!("Running `{:?}`", cmd);

let result = cmd
.output()
.expect("Failed to determine current branch.")
;
Expand All @@ -25,10 +34,16 @@ pub fn get_current_branch(
pub fn get_current_remote(
working_directory: &PathBuf,
) -> String {
let result = Command::new("git")
let mut cmd = Command::new("git");
cmd
.arg("remote")
.arg("show")
.current_dir(&working_directory)
;

debug!("Running `{:?}`", cmd);

let result = cmd
.output()
.expect("Failed to determine current remote.")
;
Expand All @@ -45,7 +60,8 @@ pub fn get_merge_base_commit(
remote: &String,
base_branch: &String,
) -> String {
let result = Command::new("git")
let mut cmd = Command::new("git");
cmd
.arg("merge-base")
.arg(format!(
"{}/{}",
Expand All @@ -54,6 +70,11 @@ pub fn get_merge_base_commit(
))
.arg("HEAD")
.current_dir(&working_directory)
;

debug!("Running `{:?}`", cmd);

let result = cmd
.output()
.expect("Failed to determine merge base.")
;
Expand Down
12 changes: 6 additions & 6 deletions git/src/commits_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ pub struct CommitsRange {
}

impl CommitsRange {
pub fn to_str(&self) -> String {
return format!(
"{}..{}",
&self.from,
&self.to,
);
pub fn from(&self) -> &String {
return &self.from;
}

pub fn to(&self) -> &String {
return &self.to;
}
}

Expand Down
26 changes: 22 additions & 4 deletions git/src/path_inspector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
extern crate log;

use std::path::PathBuf;
use std::process::Command;
use log::{debug, info};

use utils::assert_or_panic;
use crate::commits_range::CommitsRange;
Expand All @@ -9,16 +12,31 @@ pub fn has_changes_in_paths(
commits_range: &CommitsRange,
paths: &Vec<PathBuf>
) -> bool {
let result = Command::new("git")
.arg("log")
.arg(commits_range.to_str())
let mut cmd = Command::new("git");
cmd
.arg("diff")
.arg("--stat")
.arg(commits_range.from())
.arg(commits_range.to())
.args(paths)
.current_dir(&working_directory)
;

debug!("Running `{:?}`", cmd);

let result = cmd
.output()
.expect("Failed to run git log command.")
;

assert_or_panic(&result, &String::from("git log"));
assert_or_panic(&result, &String::from("git diff"));

info!(
"Detected diff from {} to {} :\n{}",
commits_range.from(),
commits_range.to(),
String::from_utf8(result.stdout.to_vec()).unwrap()
);

return !result.stdout.is_empty();
}
9 changes: 9 additions & 0 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "logger"
version = "0.1.0"
authors = ["KNP Labs"]
edition = "2018"

[dependencies]
log = "0.4.8"
simple_logger = { version = "1.6.0", default-features = false }
16 changes: 16 additions & 0 deletions logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extern crate log;
extern crate simple_logger;

use log::Level;

pub fn configure_logger(verbosity: &u8) {
let level: Level;

match *verbosity {
1 => level = Level::Info,
2 => level = Level::Debug,
_ => level = Level::Warn,
}

simple_logger::init_with_level(level).unwrap();
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use std::process::exit;

use cli::Cli;
use lib::should_skip_ci;
use logger::configure_logger;

fn main() {
let cli: Cli = Cli::new();

configure_logger(cli.verbosity());

let status_code: i32 = should_skip_ci(
&current_dir().unwrap(),
cli.paths(),
Expand Down