@@ -3,7 +3,9 @@ use crate::filter::Filter;
33use crate :: github;
44use anyhow:: Result ;
55use clap:: Parser ;
6+ use prettytable:: { Table , format, row} ;
67use rayon:: prelude:: * ;
8+ use serde_json:: json;
79
810#[ derive( Debug , Parser ) ]
911/// Get topics for all repositories that match a regex
@@ -19,6 +21,14 @@ pub struct TopicGetArgs {
1921 #[ arg( long, short) ]
2022 /// Run command against all owners, not just the default one
2123 pub all_owners : bool ,
24+ #[ arg( long, short) ]
25+ /// Output as JSON
26+ pub json : bool ,
27+ }
28+
29+ struct RepoTopics {
30+ repo_name : String ,
31+ topics : Vec < String > ,
2232}
2333
2434impl TopicGetArgs {
@@ -50,24 +60,56 @@ impl TopicGetArgs {
5060 . map ( |repo| {
5161 let result = github:: get_topics ( repo, & user_token) ;
5262 match result {
53- Ok ( topics) => {
54- println ! ( "List of topics for {} is: {:?}" , repo. name, topics ) ;
55- true
56- }
63+ Ok ( topics) => Ok ( RepoTopics {
64+ repo_name : repo. name . clone ( ) ,
65+ topics ,
66+ } ) ,
5767 Err ( e) => {
5868 println ! (
5969 "Failed to get topics for repo {} because {:?}" ,
6070 repo. name, e
6171 ) ;
62- false
72+ Err ( ( ) )
6373 }
6474 }
6575 } )
6676 . collect ( ) ;
6777
68- let successful = results. iter ( ) . filter ( |& & success| success ) . count ( ) ;
78+ let successful = results. iter ( ) . filter ( |r| r . is_ok ( ) ) . count ( ) ;
6979 let failed = results. len ( ) - successful;
7080
81+ if self . json {
82+ let json_output: Vec < _ > = results
83+ . iter ( )
84+ . flatten ( )
85+ . map ( |rt| json ! ( { "repository" : rt. repo_name, "topics" : rt. topics} ) )
86+ . collect ( ) ;
87+ println ! ( "{}" , serde_json:: to_string_pretty( & json_output) ?) ;
88+ } else {
89+ let mut table = Table :: new ( ) ;
90+ table. set_format ( * format:: consts:: FORMAT_BORDERS_ONLY ) ;
91+ table. set_titles ( row ! [ "Repository" , "Topic" ] ) ;
92+
93+ let mut topic_count = 0 ;
94+ for repo_topics in results. iter ( ) . flatten ( ) {
95+ for ( i, topic) in repo_topics. topics . iter ( ) . enumerate ( ) {
96+ let repo_col = if i == 0 { & repo_topics. repo_name } else { "" } ;
97+ table. add_row ( row ! [ repo_col, topic] ) ;
98+ topic_count += 1 ;
99+ }
100+ }
101+
102+ if topic_count > 0 {
103+ table. printstd ( ) ;
104+ println ! (
105+ "{} topics across {} repos in {}" ,
106+ topic_count, successful, owner
107+ ) ;
108+ } else {
109+ println ! ( "No topics found for repos in {}" , owner) ;
110+ }
111+ }
112+
71113 Ok ( OrgResult {
72114 org_name : owner. to_string ( ) ,
73115 total_repos : results. len ( ) ,
0 commit comments