@@ -16,7 +16,11 @@ use std::{
1616#[ derive( Subcommand , Debug ) ]
1717enum 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 }
0 commit comments