forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.rs
More file actions
65 lines (58 loc) · 1.66 KB
/
Copy pathcache.rs
File metadata and controls
65 lines (58 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use camino::Utf8PathBuf;
use std::{env, path::PathBuf};
fn utf8_path_from_os(value: std::ffi::OsString) -> Option<Utf8PathBuf> {
Utf8PathBuf::from_path_buf(PathBuf::from(value)).ok()
}
fn env_path(var: &str) -> Option<Utf8PathBuf> {
env::var_os(var).and_then(utf8_path_from_os)
}
fn join_components(mut base: Utf8PathBuf, components: &[&str]) -> Utf8PathBuf {
for component in components {
base.push(component);
}
base
}
fn xdg_cache_dir() -> Option<Utf8PathBuf> {
env_path("XDG_CACHE_HOME").map(|path| join_components(path, &["fe", "git"]))
}
fn unix_home_cache() -> Option<Utf8PathBuf> {
#[cfg(unix)]
{
env_path("HOME").map(|path| join_components(path, &[".cache", "fe", "git"]))
}
#[cfg(not(unix))]
{
None
}
}
fn windows_local_appdata() -> Option<Utf8PathBuf> {
#[cfg(windows)]
{
env_path("LOCALAPPDATA")
.or_else(|| env_path("USERPROFILE"))
.map(|path| join_components(path, &["Fe", "cache", "git"]))
}
#[cfg(not(windows))]
{
None
}
}
pub fn remote_git_cache_dir() -> Option<Utf8PathBuf> {
env_path("FE_REMOTE_CACHE_DIR")
.or_else(|| {
env_path("FE_CACHE_DIR").map(|path| {
if path.ends_with("git") {
path
} else {
join_components(path, &["git"])
}
})
})
.or_else(xdg_cache_dir)
.or_else(unix_home_cache)
.or_else(windows_local_appdata)
.or_else(|| {
utf8_path_from_os(env::temp_dir().into())
.map(|path| join_components(path, &["fe", "git"]))
})
}