Skip to content

Commit aac411c

Browse files
committed
Specify path on the command line #27
1 parent dff457c commit aac411c

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/main.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
extern crate bytecount;
12
extern crate colored;
23
extern crate git2;
34
extern crate license;
45
extern crate tokei;
5-
extern crate bytecount;
66

77
use colored::Color;
88
use colored::*;
@@ -12,6 +12,7 @@ use std::{
1212
cmp,
1313
collections::HashMap,
1414
convert::From,
15+
env,
1516
ffi::OsStr,
1617
fmt,
1718
fmt::Write,
@@ -261,16 +262,32 @@ fn main() -> Result<()> {
261262
return Err(Error::GitNotInstalled);
262263
}
263264

264-
let tokei_langs = project_languages();
265+
let mut args = env::args();
266+
267+
if args.next().is_none() {
268+
return Err(Error::TooFewArgs);
269+
};
270+
271+
let dir = if let Some(arg) = args.next() {
272+
arg
273+
} else {
274+
String::from(".")
275+
};
276+
277+
if args.next().is_some() {
278+
return Err(Error::TooManyArgs);
279+
};
280+
281+
let tokei_langs = project_languages(&dir);
265282
let languages_stat = get_languages_stat(&tokei_langs).ok_or(Error::SourceCodeNotFound)?;
266283
let mut languages_stat_vec: Vec<(_, _)> = languages_stat.into_iter().collect();
267284
languages_stat_vec.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap().reverse());
268285
let dominant_language = languages_stat_vec[0].0.clone();
269286

270-
let authors = get_authors(3);
271-
let config = get_configuration()?;
272-
let version = get_version()?;
273-
let commits = get_commits()?;
287+
let authors = get_authors(&dir, 3);
288+
let config = get_configuration(&dir)?;
289+
let version = get_version(&dir)?;
290+
let commits = get_commits(&dir)?;
274291

275292
let info = Info {
276293
project_name: config.repository_name,
@@ -281,17 +298,17 @@ fn main() -> Result<()> {
281298
repo: config.repository_url,
282299
commits,
283300
number_of_lines: get_total_loc(&tokei_langs),
284-
license: project_license()?,
301+
license: project_license(&dir)?,
285302
};
286303

287304
println!("{}", info);
288305
Ok(())
289306
}
290307

291-
fn project_languages() -> tokei::Languages {
308+
fn project_languages(dir: &str) -> tokei::Languages {
292309
let mut languages = tokei::Languages::new();
293310
let required_languages = get_all_language_types();
294-
languages.get_statistics(&["."], vec![".git", "target"], Some(required_languages));
311+
languages.get_statistics(&[&dir], vec![".git", "target"], Some(required_languages));
295312
languages
296313
}
297314

@@ -314,8 +331,8 @@ fn get_languages_stat(languages: &tokei::Languages) -> Option<HashMap<Language,
314331
}
315332
}
316333

317-
fn project_license() -> Result<String> {
318-
let output = fs::read_dir(".")
334+
fn project_license(dir: &str) -> Result<String> {
335+
let output = fs::read_dir(dir)
319336
.map_err(|_| Error::ReadDirectory)?
320337
.filter_map(result::Result::ok)
321338
.map(|entry| entry.path())
@@ -344,8 +361,10 @@ fn project_license() -> Result<String> {
344361
}
345362
}
346363

347-
fn get_version() -> Result<String> {
364+
fn get_version(dir: &str) -> Result<String> {
348365
let output = Command::new("git")
366+
.arg("-C")
367+
.arg(dir)
349368
.arg("describe")
350369
.arg("--abbrev=0")
351370
.arg("--tags")
@@ -361,8 +380,10 @@ fn get_version() -> Result<String> {
361380
}
362381
}
363382

364-
fn get_commits() -> Result<String> {
383+
fn get_commits(dir: &str) -> Result<String> {
365384
let output = Command::new("git")
385+
.arg("-C")
386+
.arg(dir)
366387
.arg("rev-list")
367388
.arg("--count")
368389
.arg("HEAD")
@@ -392,8 +413,8 @@ struct Configuration {
392413
pub repository_url: String,
393414
}
394415

395-
fn get_configuration() -> Result<Configuration> {
396-
let repo = Repository::open("./").map_err(|_| Error::NotGitRepo)?;
416+
fn get_configuration(dir: &str) -> Result<Configuration> {
417+
let repo = Repository::open(dir).map_err(|_| Error::NotGitRepo)?;
397418
let config = repo.config().map_err(|_| Error::NoGitData)?;
398419
let mut remote_url = String::new();
399420
let mut repository_name = String::new();
@@ -432,9 +453,11 @@ fn get_configuration() -> Result<Configuration> {
432453
}
433454

434455
// Return first n most active commiters as authors within this project.
435-
fn get_authors(n: usize) -> Vec<String> {
456+
fn get_authors(dir: &str, n: usize) -> Vec<String> {
436457
use std::collections::HashMap;
437458
let output = Command::new("git")
459+
.arg("-C")
460+
.arg(dir)
438461
.arg("log")
439462
.arg("--format='%aN'")
440463
.output()
@@ -585,6 +608,10 @@ enum Error {
585608
ReadDirectory,
586609
/// Not in a Git Repo
587610
NotGitRepo,
611+
/// Too few arguments
612+
TooFewArgs,
613+
/// Too many arguments
614+
TooManyArgs,
588615
}
589616

590617
impl fmt::Debug for Error {
@@ -595,6 +622,8 @@ impl fmt::Debug for Error {
595622
Error::NoGitData => "Could not retrieve git configuration data",
596623
Error::ReadDirectory => "Could not read directory ./",
597624
Error::NotGitRepo => "You are not at the root of a Git Repo",
625+
Error::TooFewArgs => "Too few arguments. Expected program name and a single argument.",
626+
Error::TooManyArgs => "Too many arguments. Expected a single argument.",
598627
};
599628
write!(f, "{}", content)
600629
}

0 commit comments

Comments
 (0)