Skip to content

Commit bf89e2f

Browse files
committed
Add basic license detection.
Using a library, and a path heuristic. (check files starting with LICENSE) This can be improved in the future to include other heuristics, but this should be a good start.
1 parent 604b987 commit bf89e2f

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Diff for: Cargo.lock

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ authors = ["o2sh <[email protected]>"]
77
colored= "1.6.1"
88
git2 = {version = "0.7.5", default-features = false}
99
tokei = "8.0"
10-
10+
license = "0.7.1"

Diff for: src/main.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
extern crate colored;
22
extern crate git2;
33
extern crate tokei;
4+
extern crate license;
45

56
use colored::*;
67
use git2::Error;
78
use git2::Repository;
89
use std::fmt;
10+
use std::fs;
911
use std::process::{Command, Stdio};
12+
use std::str::FromStr;
13+
use license::License;
14+
use std::ffi::OsStr;
1015

1116
struct Info {
1217
project_name: String,
@@ -159,13 +164,15 @@ fn main() {
159164
Err(_) => panic!("Could not retrieve git configuration data"),
160165
};
161166

167+
168+
162169
let info = Info {
163170
project_name: config.repository_name,
164171
language: language,
165172
authors: authors,
166173
repo: config.repository_url,
167174
number_of_lines: get_total_loc(&tokei_langs),
168-
license: String::from("MIT"),
175+
license: project_license(),
169176
};
170177

171178
println!("{}", info);
@@ -178,6 +185,28 @@ fn project_languages() -> tokei::Languages {
178185
languages
179186
}
180187

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+
181210
fn is_git_installed() -> bool {
182211
Command::new("git")
183212
.arg("--version")

0 commit comments

Comments
 (0)