Skip to content

Commit

Permalink
Merge pull request #16 from kitlith/license-detection
Browse files Browse the repository at this point in the history
Add basic license detection.
  • Loading branch information
o2sh authored Oct 31, 2018
2 parents 604b987 + bf89e2f commit 8864385
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["o2sh <ossama-hjaji@live.fr>"]
colored= "1.6.1"
git2 = {version = "0.7.5", default-features = false}
tokei = "8.0"

license = "0.7.1"
31 changes: 30 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
extern crate colored;
extern crate git2;
extern crate tokei;
extern crate license;

use colored::*;
use git2::Error;
use git2::Repository;
use std::fmt;
use std::fs;
use std::process::{Command, Stdio};
use std::str::FromStr;
use license::License;
use std::ffi::OsStr;

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



let info = Info {
project_name: config.repository_name,
language: language,
authors: authors,
repo: config.repository_url,
number_of_lines: get_total_loc(&tokei_langs),
license: String::from("MIT"),
license: project_license(),
};

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

fn project_license() -> String {
let output = fs::read_dir(".").unwrap()
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|entry| entry.is_file()
&& entry.file_name()
.map(OsStr::to_string_lossy)
.unwrap_or("".into())
.starts_with("LICENSE") // TODO: multiple prefixes, like COPYING?
)
.map(|entry| license::Kind::from_str(&fs::read_to_string(entry).unwrap_or("".into())))
.filter_map(Result::ok)
.map(|license| license.name().to_string())
.collect::<Vec<_>>().join(", ");

if output == "" {
"Unknown".into()
} else {
output
}
}

fn is_git_installed() -> bool {
Command::new("git")
.arg("--version")
Expand Down

0 comments on commit 8864385

Please sign in to comment.