Allow compile time default paths for cheats and config

This CL makes it easier to install navi on multi-tenant Linux systems, by allowing to provide pre-install cheatsheets in shared folders (and potentially pre-installed config in shared folders).

This does not break or change the existing behaviour. The shared directory / config file is only used if
- There is not already a directory / config file in the user XDG location
- Navi is compiled with NAVI_CONFIG_FILE and/or NAVI_CHEATS_DIRECTORY env var set
- The paths defined in above env vars exist
This commit is contained in:
Ulrich Kautz 2022-02-11 17:12:23 +00:00
parent 6a76afc535
commit ef036c3224
2 changed files with 72 additions and 1 deletions

View file

@ -43,6 +43,20 @@ jobs:
toolchain: stable
override: true
- name: Prep environment to test compiled-in paths
run: |
mkdir /tmp/cheats-dir
touch /tmp/config-file
- name: Run cargo test (with compiled-in paths)
uses: actions-rs/cargo@v1
continue-on-error: false
env:
NAVI_CHEATS_DIRECTORY: /tmp/cheats-dir
NAVI_CONFIG_FILE: /tmp/config-file
with:
command: test
- name: Run cargo test
uses: actions-rs/cargo@v1
continue-on-error: false

View file

@ -26,12 +26,30 @@ fn paths_from_path_param(env_var: &str) -> impl Iterator<Item = &str> {
env_var.split(':').filter(|folder| folder != &"")
}
fn compiled_default_path(path: Option<&str>) -> Option<PathBuf> {
match path {
Some(path) => {
let path = Path::new(path);
if path.exists() {
Some(path.to_path_buf())
} else {
None
}
}
None => None,
}
}
pub fn default_cheat_pathbuf() -> Result<PathBuf> {
let base_dirs = BaseDirs::new().ok_or_else(|| anyhow!("Unable to get base dirs"))?;
let mut pathbuf = PathBuf::from(base_dirs.data_dir());
pathbuf.push("navi");
pathbuf.push("cheats");
if !pathbuf.exists() {
if let Some(path) = compiled_default_path(option_env!("NAVI_CHEATS_DIRECTORY")) {
pathbuf = path;
}
}
Ok(pathbuf)
}
@ -41,6 +59,11 @@ pub fn default_config_pathbuf() -> Result<PathBuf> {
let mut pathbuf = PathBuf::from(base_dirs.config_dir());
pathbuf.push("navi");
pathbuf.push("config.yaml");
if !pathbuf.exists() {
if let Some(path) = compiled_default_path(option_env!("NAVI_CONFIG_FILE")) {
pathbuf = path;
}
}
Ok(pathbuf)
}
@ -247,4 +270,38 @@ mod tests {
assert_eq!(found, expected)
}
}
#[test]
fn test_default_config_pathbuf() {
let base_dirs = BaseDirs::new()
.ok_or(anyhow!("bad"))
.expect("could not determine base directories");
let mut expect = base_dirs.config_dir().to_path_buf();
expect.push("navi");
expect.push("config.yaml");
let expect = match option_env!("NAVI_CONFIG_FILE") {
Some(path) => path.to_string(),
None => expect.to_string_lossy().to_string(),
};
let config = default_config_pathbuf().expect("could not find default config path");
assert_eq!(expect, config.to_string_lossy().to_string())
}
#[test]
fn test_default_cheat_pathbuf() {
let base_dirs = BaseDirs::new()
.ok_or(anyhow!("bad"))
.expect("could not determine base directories");
let mut expect = base_dirs.data_dir().to_path_buf();
expect.push("navi");
expect.push("cheats");
let expect = match option_env!("NAVI_CHEATS_DIRECTORY") {
Some(path) => path.to_string(),
None => expect.to_string_lossy().to_string(),
};
let cheats = default_cheat_pathbuf().expect("could not find default config path");
assert_eq!(expect, cheats.to_string_lossy().to_string())
}
}