Skip to content

Commit

Permalink
make sccache_dir() return proper Result instead of Option and pass it…
Browse files Browse the repository at this point in the history
… through sccache_dir()
  • Loading branch information
matthiaskrgr committed Jul 22, 2020
1 parent e891c9b commit 2012d0e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/commands/sccache.rs
Expand Up @@ -17,6 +17,7 @@ use chrono::prelude::*;
use humansize::{file_size_opts, FileSize};
use walkdir::WalkDir;

use crate::library;
use crate::tables::format_table;

#[derive(Debug, Clone)]
Expand All @@ -35,26 +36,24 @@ fn percentage_of_as_string(fraction: u64, total: u64) -> String {
}

/// get the location of a local sccache path
fn sccache_dir() -> Option<PathBuf> {
fn sccache_dir() -> Result<PathBuf, library::Error> {
if let Some(path) = env::var_os("SCCACHE_DIR").map(PathBuf::from) {
Some(path)
Ok(path)
} else {
// if SCCACHE_DIR variable is not present, get the cache dir from "dirs" crate
let mut cache_dir: Option<PathBuf> = dirs::cache_dir();

if let Some(cache_dir) = cache_dir.as_mut() {
cache_dir.push("sccache");
Some(cache_dir.to_path_buf())
Ok(cache_dir.to_path_buf())
} else {
cache_dir
Err(library::Error::NoSccacheDir)
}
}
}

pub(crate) fn sccache_stats() {
let sccache_path: PathBuf = sccache_dir()
.expect("Failed to get a valid sccache cache dir such as \"~/.cache/sccache\"");
//@TODO ^ turn this into a proper error message ^ !
pub(crate) fn sccache_stats() -> Result<(), library::Error> {
let sccache_path: PathBuf = sccache_dir()?;

// of all the files inside the sccache cache, gather last access time and path
let files = WalkDir::new(sccache_path.display().to_string())
Expand Down Expand Up @@ -176,4 +175,5 @@ pub(crate) fn sccache_stats() {
let table = format_table(&table_vec, 1); // need so strip whitespaces added by the padding
let table_trimmed = table.trim();
println!("{}", table_trimmed);
Ok(())
}
4 changes: 4 additions & 0 deletions src/library.rs
Expand Up @@ -90,6 +90,8 @@ pub(crate) enum Error {
DateParseFailure(String, String),
// cargo metadata failed to parse a cargo manifest
UnparsableManifest(PathBuf, cargo_metadata::Error),
// could not find sccache cache dir
NoSccacheDir,
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -183,6 +185,8 @@ impl fmt::Display for Error {
),
Self::UnparsableManifest(path, error) => write!(f,
"Failed to parse Cargo.toml at '{}': '{:?}'", path.display(), error),
Self::NoSccacheDir => write!(f,
"Could not find sccache cache directory at ~/.cache/sccache or ${{SCCACHE_DIR}}"),
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Expand Up @@ -119,8 +119,15 @@ fn main() {
};

if config.is_present("sc") || config.is_present("sccache") {
sccache::sccache_stats();
process::exit(0);
match sccache::sccache_stats() {
Ok(()) => {
process::exit(0);
}
Err(e) => {
eprintln!("{}", e);
process::exit(1);
}
}
}

// indicates if size changed and whether we should print a before/after size diff
Expand Down

0 comments on commit 2012d0e

Please sign in to comment.