Skip to content

Add basic license detection. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>"]
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