1
1
extern crate colored;
2
2
extern crate git2;
3
3
extern crate tokei;
4
+ extern crate license;
4
5
5
6
use colored:: * ;
6
7
use git2:: Error ;
7
8
use git2:: Repository ;
8
9
use std:: fmt;
10
+ use std:: fs;
9
11
use std:: process:: { Command , Stdio } ;
12
+ use std:: str:: FromStr ;
13
+ use license:: License ;
14
+ use std:: ffi:: OsStr ;
10
15
11
16
struct Info {
12
17
project_name : String ,
@@ -159,13 +164,15 @@ fn main() {
159
164
Err ( _) => panic ! ( "Could not retrieve git configuration data" ) ,
160
165
} ;
161
166
167
+
168
+
162
169
let info = Info {
163
170
project_name : config. repository_name ,
164
171
language : language,
165
172
authors : authors,
166
173
repo : config. repository_url ,
167
174
number_of_lines : get_total_loc ( & tokei_langs) ,
168
- license : String :: from ( "MIT" ) ,
175
+ license : project_license ( ) ,
169
176
} ;
170
177
171
178
println ! ( "{}" , info) ;
@@ -178,6 +185,28 @@ fn project_languages() -> tokei::Languages {
178
185
languages
179
186
}
180
187
188
+ fn project_license ( ) -> String {
189
+ let output = fs:: read_dir ( "." ) . unwrap ( )
190
+ . filter_map ( Result :: ok)
191
+ . map ( |entry| entry. path ( ) )
192
+ . filter ( |entry| entry. is_file ( )
193
+ && entry. file_name ( )
194
+ . map ( OsStr :: to_string_lossy)
195
+ . unwrap_or ( "" . into ( ) )
196
+ . starts_with ( "LICENSE" ) // TODO: multiple prefixes, like COPYING?
197
+ )
198
+ . map ( |entry| license:: Kind :: from_str ( & fs:: read_to_string ( entry) . unwrap_or ( "" . into ( ) ) ) )
199
+ . filter_map ( Result :: ok)
200
+ . map ( |license| license. name ( ) . to_string ( ) )
201
+ . collect :: < Vec < _ > > ( ) . join ( ", " ) ;
202
+
203
+ if output == "" {
204
+ "Unknown" . into ( )
205
+ } else {
206
+ output
207
+ }
208
+ }
209
+
181
210
fn is_git_installed ( ) -> bool {
182
211
Command :: new ( "git" )
183
212
. arg ( "--version" )
0 commit comments