git_global/subcommands/
info.rs

1//! The `info` subcommand: shows metadata about the git-global installation.
2
3use clap::crate_version;
4
5use std::env;
6use std::path::PathBuf;
7use std::time::SystemTime;
8
9use crate::config::Config;
10use crate::errors::Result;
11use crate::report::Report;
12
13/// Returns the age of a file in terms of days, hours, minutes, and seconds.
14fn get_age(filename: PathBuf) -> Option<String> {
15    filename
16        .metadata()
17        .ok()
18        .and_then(|metadata| metadata.modified().ok())
19        .and_then(|mtime| SystemTime::now().duration_since(mtime).ok())
20        .map(|dur| {
21            let ts = dur.as_secs();
22            let days = ts / (24 * 60 * 60);
23            let hours = (ts / (60 * 60)) - (days * 24);
24            let mins = (ts / 60) - (days * 24 * 60) - (hours * 60);
25            let secs =
26                ts - (days * 24 * 60 * 60) - (hours * 60 * 60) - (mins * 60);
27            format!("{}d, {}h, {}m, {}s", days, hours, mins, secs)
28        })
29}
30
31/// Gathers metadata about the git-global installation.
32pub fn execute(mut config: Config) -> Result<Report> {
33    let repos = config.get_repos();
34    let mut report = Report::new(&repos);
35    let version = crate_version!().to_string();
36    // beginning of underline:   git-global x.x.x
37    let mut underline = "===========".to_string();
38    for _ in 0..version.len() {
39        underline.push('=');
40    }
41    report.add_message(format!("git-global {}", version));
42    report.add_message(underline);
43    report.add_message(format!("Number of repos: {}", repos.len()));
44    report.add_message(format!("Base directory: {}", config.basedir.display()));
45    report.add_message("Ignored patterns:".to_string());
46    for pat in config.ignored_patterns.iter() {
47        report.add_message(format!("  {}", pat));
48    }
49    report.add_message(format!("Default command: {}", config.default_cmd));
50    report.add_message(format!("Verbose: {}", config.verbose));
51    report.add_message(format!("Show untracked: {}", config.show_untracked));
52    if let Some(cache_file) = config.cache_file {
53        report.add_message(format!("Cache file: {}", cache_file.display()));
54        if let Some(age) = get_age(cache_file) {
55            report.add_message(format!("Cache file age: {}", age));
56        }
57    } else {
58        report.add_message("Cache file: <none>".to_string());
59    }
60    if let Some(manpage_file) = config.manpage_file {
61        report.add_message(format!("Manpage file: {}", manpage_file.display()));
62    } else {
63        report.add_message("Manpage file: <none>".to_string());
64    }
65    report.add_message(format!("Detected OS: {}", env::consts::OS));
66    Ok(report)
67}