Merge pull request #686 from ukautz/feature/linux-multi-tenant

Add shared paths for cheat directory and config file
This commit is contained in:
Denis Isidoro 2022-02-15 15:59:02 -03:00 committed by GitHub
commit 528178e219
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 2 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_PATH: /tmp/cheats-dir
NAVI_CONFIG: /tmp/config-file
with:
command: test
- name: Run cargo test
uses: actions-rs/cargo@v1
continue-on-error: false

View file

@ -65,6 +65,13 @@ make install
# make BIN_DIR=/usr/local/bin install
```
##### Compile time environment variables
**navi** supports environment variables at compile time that modify the behavior of the binary at runtime:
- `NAVI_PATH` (directory path value): If the `cheats` directory in the user's directory does not exist, **navi** uses this path (if it exists), as a fallback location to look for cheat files. Use case: system-wide installed, shared used cheatsheets folder.
- `NAVI_CONFIG` (file path value): If the `config.yaml` file in the user's directory does not exist, **navi** uses this path (if it exists), as a fallback location to look for a configuration file. Use case: system-wide installed, shared used configuration file.
#### Other package managers
You can find **navi** for more package managers by clicking on the image below:

View file

@ -9,7 +9,7 @@ use anyhow::Result;
use directories_next::BaseDirs;
use regex::Regex;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use walkdir::WalkDir;
pub fn all_cheat_files(path: &Path) -> Vec<String> {
@ -26,12 +26,35 @@ 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 = if path.contains(MAIN_SEPARATOR) {
path.split(MAIN_SEPARATOR).next().unwrap()
} else {
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_PATH")) {
pathbuf = path;
}
}
Ok(pathbuf)
}
@ -41,6 +64,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")) {
pathbuf = path;
}
}
Ok(pathbuf)
}
@ -247,4 +275,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") {
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_PATH") {
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())
}
}