Use already existing env var names

This commit is contained in:
Ulrich Kautz 2022-02-14 13:51:46 +00:00
parent ef036c3224
commit 87f0ac0aec
3 changed files with 19 additions and 7 deletions

View file

@ -52,8 +52,8 @@ jobs:
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
continue-on-error: false continue-on-error: false
env: env:
NAVI_CHEATS_DIRECTORY: /tmp/cheats-dir NAVI_PATH: /tmp/cheats-dir
NAVI_CONFIG_FILE: /tmp/config-file NAVI_CONFIG: /tmp/config-file
with: with:
command: test command: test

View file

@ -65,6 +65,13 @@ make install
# make BIN_DIR=/usr/local/bin 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 #### Other package managers
You can find **navi** for more package managers by clicking on the image below: 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 directories_next::BaseDirs;
use regex::Regex; use regex::Regex;
use std::collections::HashSet; use std::collections::HashSet;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use walkdir::WalkDir; use walkdir::WalkDir;
pub fn all_cheat_files(path: &Path) -> Vec<String> { pub fn all_cheat_files(path: &Path) -> Vec<String> {
@ -29,6 +29,11 @@ fn paths_from_path_param(env_var: &str) -> impl Iterator<Item = &str> {
fn compiled_default_path(path: Option<&str>) -> Option<PathBuf> { fn compiled_default_path(path: Option<&str>) -> Option<PathBuf> {
match path { match path {
Some(path) => { Some(path) => {
let path = if path.contains(MAIN_SEPARATOR) {
path.split(MAIN_SEPARATOR).next().unwrap()
} else {
path
};
let path = Path::new(path); let path = Path::new(path);
if path.exists() { if path.exists() {
Some(path.to_path_buf()) Some(path.to_path_buf())
@ -46,7 +51,7 @@ pub fn default_cheat_pathbuf() -> Result<PathBuf> {
pathbuf.push("navi"); pathbuf.push("navi");
pathbuf.push("cheats"); pathbuf.push("cheats");
if !pathbuf.exists() { if !pathbuf.exists() {
if let Some(path) = compiled_default_path(option_env!("NAVI_CHEATS_DIRECTORY")) { if let Some(path) = compiled_default_path(option_env!("NAVI_PATH")) {
pathbuf = path; pathbuf = path;
} }
} }
@ -60,7 +65,7 @@ pub fn default_config_pathbuf() -> Result<PathBuf> {
pathbuf.push("navi"); pathbuf.push("navi");
pathbuf.push("config.yaml"); pathbuf.push("config.yaml");
if !pathbuf.exists() { if !pathbuf.exists() {
if let Some(path) = compiled_default_path(option_env!("NAVI_CONFIG_FILE")) { if let Some(path) = compiled_default_path(option_env!("NAVI_CONFIG")) {
pathbuf = path; pathbuf = path;
} }
} }
@ -279,7 +284,7 @@ mod tests {
let mut expect = base_dirs.config_dir().to_path_buf(); let mut expect = base_dirs.config_dir().to_path_buf();
expect.push("navi"); expect.push("navi");
expect.push("config.yaml"); expect.push("config.yaml");
let expect = match option_env!("NAVI_CONFIG_FILE") { let expect = match option_env!("NAVI_CONFIG") {
Some(path) => path.to_string(), Some(path) => path.to_string(),
None => expect.to_string_lossy().to_string(), None => expect.to_string_lossy().to_string(),
}; };
@ -296,7 +301,7 @@ mod tests {
let mut expect = base_dirs.data_dir().to_path_buf(); let mut expect = base_dirs.data_dir().to_path_buf();
expect.push("navi"); expect.push("navi");
expect.push("cheats"); expect.push("cheats");
let expect = match option_env!("NAVI_CHEATS_DIRECTORY") { let expect = match option_env!("NAVI_PATH") {
Some(path) => path.to_string(), Some(path) => path.to_string(),
None => expect.to_string_lossy().to_string(), None => expect.to_string_lossy().to_string(),
}; };