Skip to content

Commit

Permalink
add json option for use with alfred/programmatic
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDavison committed Aug 28, 2023
1 parent 2e2e7d7 commit 7d3ba0b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
56 changes: 39 additions & 17 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use anyhow::{anyhow, Result};
use std::path::Path;
use std::process::Command;

use super::AS_JSON;

pub fn is_git_repo(p: &Path) -> bool {
let mut p = p.to_path_buf();
p.push(".git");
Expand All @@ -12,9 +14,9 @@ pub fn is_git_repo(p: &Path) -> bool {
fn command_output(dir: &Path, args: &[&str]) -> Result<Vec<String>> {
let out = Command::new("git").current_dir(dir).args(args).output()?;
Ok(std::str::from_utf8(&out.stdout)?
.lines()
.map(|x| x.to_string())
.collect())
.lines()
.map(|x| x.to_string())
.collect())
}

// Fetch all branches of a git repo
Expand Down Expand Up @@ -53,15 +55,15 @@ fn ahead_behind(p: &Path) -> Result<Option<String>> {
let response: String = command_output(
p,
&[
"for-each-ref",
"--format='%(refname:short) %(upstream:track)'",
"refs/heads",
"for-each-ref",
"--format='%(refname:short) %(upstream:track)'",
"refs/heads",
],
)?
.iter()
.map(|x| x.trim_matches('\'').trim())
.filter(|x| x.split(' ').nth(1).is_some())
.collect();
)?
.iter()
.map(|x| x.trim_matches('\'').trim())
.filter(|x| x.split(' ').nth(1).is_some())
.collect();
if !response.is_empty() {
Ok(Some(response))
} else {
Expand Down Expand Up @@ -114,7 +116,17 @@ pub fn branches(p: &Path) -> Result<Option<String>> {
.ok_or_else(|| anyhow!("No stem for dir"))?
.to_string_lossy();
let dirstr = format!("{}/{}", parentname, dirname);
Ok(Some(format!("{:40}\t{}", dirstr, branches)))
let mut output = String::new();
unsafe {
if AS_JSON {
output = format!("{{\"title\": \"{}\", \"subtitle\": \"{}\"}}",
dirstr, branches);
}
else {
output = format!("{:40}\t{}", dirstr, branches);
}
}
Ok(Some(output))
}

pub fn branchstat(p: &Path) -> Result<Option<String>> {
Expand All @@ -128,11 +140,21 @@ pub fn branchstat(p: &Path) -> Result<Option<String>> {
if outputs.is_empty() {
Ok(None)
} else {
let out = format!(
"{:20} | {}",
p.file_name().unwrap().to_string_lossy(),
outputs
);
let mut out = String::new();
unsafe {
if AS_JSON {
out = format!("{{\"title\": \"{}\", \"subtitle\": \"{}\"}}",
p.file_name().unwrap().to_string_lossy(),
outputs);
} else {
out = format!(
"{:20} | {}",
p.file_name().unwrap().to_string_lossy(),
outputs
);

}
}
Ok(Some(out))
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const USAGE: &str = "repoutil vVERSION_FROM_ENV
Operations on multiple git repos
usage:
repoutil <command>
repoutil <command> [-j|--json]
commands:
p|push Push commits
Expand All @@ -24,6 +24,8 @@ commands:
h|help Display this help message
v|version Show version";

static mut AS_JSON: bool = false;

fn main() {
let args: Vec<_> = std::env::args().skip(1).collect();

Expand Down Expand Up @@ -52,6 +54,10 @@ fn main() {
}
};

unsafe {
AS_JSON = args.iter().skip(1).any(|a| a == "-j" || a == "--json");
}

let dirs = match get_dirs_from_config() {
Ok(d) => d,
Err(e) => {
Expand Down Expand Up @@ -99,8 +105,15 @@ fn main() {
}
}
messages.sort();
for msg in messages.iter().filter(|msg| !msg.is_empty()) {
println!("{}", msg)
unsafe {
let messages = messages.iter().filter(|msg| !msg.is_empty());
if AS_JSON {
println!("{{\"items\": [{}]}}", messages.map(|x| x.to_string()).collect::<Vec<String>>().join(","));
} else {
for msg in messages {
println!("{}", msg)
}
}
}
}

Expand Down

0 comments on commit 7d3ba0b

Please sign in to comment.