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]]
name = "giterator"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"ansi_term",
"clap",

View file

@ -1,9 +1,14 @@
[package]
name = "giterator"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
authors = ["JMARyA <jmarya@hydrar.de>"]
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]
ansi_term = "0.12.1"

View file

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

View file

@ -171,6 +171,7 @@ impl OutMode {
fn main() {
let args = args::get_args();
let streaming = args.get_flag("streaming");
let repo = args.get_one::<String>("repository").unwrap();
let allow_dirty = args.get_flag("allow-dirty");
let command = if args.get_flag("script_file") {
@ -184,41 +185,65 @@ fn main() {
if is_repository_clean(repo) || allow_dirty {
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 {
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();
match outmode {
OutMode::Text => {
for i in out {
i.print_text();
if !streaming {
match outmode {
OutMode::Text => {
for i in out {
i.print_text();
}
}
}
OutMode::Json => {
let json: Vec<_> = out.into_iter().map(|x| x.as_json()).collect();
println!(
"{}",
serde_json::to_string(&serde_json::to_value(json).unwrap()).unwrap()
);
}
OutMode::Csv => {
let csv: Vec<Vec<String>> = out.into_iter().map(|x| x.as_csv()).collect();
let mut wtr = csv::Writer::from_writer(std::io::stdout());
wtr.write_record([
"repo", "hash", "datetime", "name", "status", "stdout", "stderr",
])
.unwrap();
for record in csv {
wtr.write_record(&record).unwrap();
OutMode::Json => {
let json: Vec<_> = out.into_iter().map(|x| x.as_json()).collect();
println!(
"{}",
serde_json::to_string(&serde_json::to_value(json).unwrap()).unwrap()
);
}
OutMode::Csv => {
let csv: Vec<Vec<String>> = out.into_iter().map(|x| x.as_csv()).collect();
let mut wtr = csv::Writer::from_writer(std::io::stdout());
wtr.flush().unwrap();
wtr.write_record([
"repo", "hash", "datetime", "name", "status", "stdout", "stderr",
])
.unwrap();
for record in csv {
wtr.write_record(&record).unwrap();
}
wtr.flush().unwrap();
}
}
}
} 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`"));
}
}