Skip to content

Commit 0320098

Browse files
Add support for --json to match ruby codeownership cli
1 parent 3b05bc1 commit 0320098

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

src/cli.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use std::{
1616
#[derive(Subcommand, Debug)]
1717
enum Command {
1818
#[clap(about = "Finds the owner of a given file.", visible_alias = "f")]
19-
ForFile { name: String },
19+
ForFile {
20+
name: String,
21+
#[arg(long, help = "Output result as JSON")]
22+
json: bool,
23+
},
2024

2125
#[clap(about = "Finds code ownership information for a given team ", visible_alias = "t")]
2226
ForTeam { name: String },
@@ -124,15 +128,41 @@ pub fn cli() -> Result<(), Error> {
124128
std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?;
125129
ownership.validate().change_context(Error::ValidationFailed)?
126130
}
127-
Command::ForFile { name } => {
131+
Command::ForFile { name, json } => {
128132
let file_owners = ownership.for_file(&name).change_context(Error::Io)?;
129-
match file_owners.len() {
130-
0 => println!("{}", FileOwner::default()),
131-
1 => println!("{}", file_owners[0]),
132-
_ => {
133-
println!("Error: file is owned by multiple teams!");
134-
for file_owner in file_owners {
135-
println!("\n{}", file_owner);
133+
if json {
134+
let output = match file_owners.len() {
135+
0 => serde_json::json!({
136+
"team_name": null,
137+
"team_yml": null
138+
}),
139+
1 => {
140+
let owner = &file_owners[0];
141+
serde_json::json!({
142+
"team_name": owner.team.name,
143+
"team_yml": owner.team_config_file_path
144+
})
145+
}
146+
_ => serde_json::json!({
147+
"error": "Multiple owners",
148+
"owners": file_owners.iter().map(|o| {
149+
serde_json::json!({
150+
"team_name": o.team,
151+
"team_yml": o.team_config_file_path
152+
})
153+
}).collect::<Vec<_>>()
154+
}),
155+
};
156+
println!("{}", serde_json::to_string(&output).unwrap());
157+
} else {
158+
match file_owners.len() {
159+
0 => println!("{}", FileOwner::default()),
160+
1 => println!("{}", file_owners[0]),
161+
_ => {
162+
println!("Error: file is owned by multiple teams!");
163+
for file_owner in file_owners {
164+
println!("\n{}", file_owner);
165+
}
136166
}
137167
}
138168
}

src/project.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
use error_stack::{Context, Result, ResultExt};
9+
use serde::Serialize;
910

1011
pub struct Project {
1112
pub base_path: PathBuf,
@@ -29,7 +30,7 @@ pub struct ProjectFile {
2930
pub path: PathBuf,
3031
}
3132

32-
#[derive(Clone, Debug, Default)]
33+
#[derive(Clone, Debug, Default, Serialize)]
3334
pub struct Team {
3435
pub path: PathBuf,
3536
pub name: String,

0 commit comments

Comments
 (0)