Skip to content

Commit

Permalink
Merge pull request #11 from jesseduffield/env-var
Browse files Browse the repository at this point in the history
allow configuring env var for config directory
  • Loading branch information
jesseduffield authored Jun 2, 2021
2 parents ea0c4f7 + ce54f89 commit be632b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
50 changes: 41 additions & 9 deletions src/config/storage.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{error::Error, fs::File, io::prelude::*, path::PathBuf};
use std::{env, error::Error, fs::File, io::prelude::*, path::PathBuf};

use super::Config;

use std::fs;
use std::io;
use std::path::Path;
use std::{fs, io, path::Path};

extern crate directories;
use directories::ProjectDirs;

pub const CONFIG_DIR_ENV_VAR: &str = "LAZYCLI_CONFIG_DIR";

// adapted from xdg crate
fn write_file<P>(home: &PathBuf, path: P) -> io::Result<PathBuf>
where
Expand All @@ -21,15 +21,22 @@ where
Ok(PathBuf::from(home.join(path.as_ref())))
}

pub fn config_path() -> Result<PathBuf, Box<dyn Error>> {
let config_dir = ProjectDirs::from("", "", "lazycli")
.unwrap()
.config_dir()
.to_owned();
pub fn config_path(dir_env_var: Option<String>) -> Result<PathBuf, Box<dyn Error>> {
let config_dir = config_dir(dir_env_var);

Ok(write_file(&PathBuf::from(config_dir), "config.yml")?)
}

fn config_dir(dir_env_var: Option<String>) -> PathBuf {
match dir_env_var {
Some(value) => PathBuf::from(value),
None => ProjectDirs::from("", "", "lazycli")
.unwrap()
.config_dir()
.to_owned(),
}
}

pub fn prepare_config(config_path: &PathBuf) -> Result<Config, Box<dyn Error>> {
if config_path.exists() {
let mut file = File::open(config_path)?;
Expand All @@ -44,3 +51,28 @@ pub fn prepare_config(config_path: &PathBuf) -> Result<Config, Box<dyn Error>> {
Ok(config)
}
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_use_config_dir_when_provided() {
assert_eq!(
config_dir(Some(String::from("/mydir"))),
PathBuf::from("/mydir")
)
}

#[test]
fn test_fallback_to_default_config_dir() {
let result = config_dir(None);
println!("{:?}", result);
assert!(
// not asserting on the whole path given that it's platform-dependent
result.ends_with("lazycli"),
"should end in 'lazycli'!"
);
}
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
#[allow(dead_code)]
use std::error::Error;

Expand All @@ -16,10 +17,11 @@ mod ui;
use app::App;
use args::Args;
use config::storage;
use storage::CONFIG_DIR_ENV_VAR;

fn main() -> Result<(), Box<dyn Error>> {
let args = Args::new();
let config_path = storage::config_path()?;
let config_path = storage::config_path(env::var(CONFIG_DIR_ENV_VAR).ok())?;
let config = storage::prepare_config(&config_path)?;

let app = App::new(&config, config_path, args);
Expand Down

0 comments on commit be632b3

Please sign in to comment.