add streaming flag

This commit is contained in:
JMARyA 2024-03-27 16:43:21 +01:00
parent f8a773fa63
commit f268a07764
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 67 additions and 30 deletions

2
Cargo.lock generated
View file

@ -115,7 +115,7 @@ dependencies = [
[[package]] [[package]]
name = "giterator" name = "giterator"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"clap", "clap",

View file

@ -1,9 +1,14 @@
[package] [package]
name = "giterator" name = "giterator"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
authors = ["JMARyA <jmarya@hydrar.de>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html description = "A versatile command-line tool for iterating over Git commit histories."
license = "MIT"
repository = "https://git.hydrar.de/jmarya/giterator"
homepage = "https://git.hydrar.de/jmarya/giterator"
documentation = "https://git.hydrar.de/jmarya/giterator/blob/main/README.md"
keywords = ["git", "iteration", "command-line", "tool"]
[dependencies] [dependencies]
ansi_term = "0.12.1" ansi_term = "0.12.1"

View file

@ -33,6 +33,13 @@ pub fn get_args() -> ArgMatches {
.num_args(0) .num_args(0)
.required(false), .required(false),
) )
.arg(
clap::Arg::new("streaming")
.long("streaming")
.help("Output results as soon as they are available")
.num_args(0)
.required(false),
)
.arg( .arg(
clap::Arg::new("csv") clap::Arg::new("csv")
.short('c') .short('c')

View file

@ -171,6 +171,7 @@ impl OutMode {
fn main() { fn main() {
let args = args::get_args(); let args = args::get_args();
let streaming = args.get_flag("streaming");
let repo = args.get_one::<String>("repository").unwrap(); let repo = args.get_one::<String>("repository").unwrap();
let allow_dirty = args.get_flag("allow-dirty"); let allow_dirty = args.get_flag("allow-dirty");
let command = if args.get_flag("script_file") { let command = if args.get_flag("script_file") {
@ -184,11 +185,34 @@ fn main() {
if is_repository_clean(repo) || allow_dirty { if is_repository_clean(repo) || allow_dirty {
let commits = get_commit_list(repo).unwrap(); let commits = get_commit_list(repo).unwrap();
if streaming && matches!(outmode, OutMode::Csv) {
let mut wtr = csv::Writer::from_writer(std::io::stdout());
wtr.write_record([
"repo", "hash", "datetime", "name", "status", "stdout", "stderr",
])
.unwrap();
}
for commit in commits { for commit in commits {
out.push(commit.run_command(&command)); let commit_out = commit.run_command(&command);
if streaming {
match outmode {
OutMode::Text => commit_out.print_text(),
OutMode::Json => println!("{}", commit_out.as_json()),
OutMode::Csv => {
let mut wtr = csv::Writer::from_writer(std::io::stdout());
wtr.write_record(&commit_out.as_csv()).unwrap();
wtr.flush().unwrap();
}
}
}
out.push(commit_out);
} }
checkout(repo, "main").unwrap(); checkout(repo, "main").unwrap();
if !streaming {
match outmode { match outmode {
OutMode::Text => { OutMode::Text => {
for i in out { for i in out {
@ -218,7 +242,8 @@ fn main() {
wtr.flush().unwrap(); wtr.flush().unwrap();
} }
} }
}
} else { } else {
eprintln!("Repository is not clean. If you want to allow operating over an unclean repository, pass the `--allow-dirty` flag."); eprintln!("{}: Repository is not clean. If you want to allow operating over an unclean repository, pass the {} flag.", Color::Red.paint("Error"), Color::Blue.paint("`--allow-dirty`"));
} }
} }