Skip to content

Commit

Permalink
add env wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
EricDriussi committed Apr 6, 2024
1 parent c988ede commit 41847de
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/authors/csv/reader.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use super::super::author::{Author, AuthorsProvider};
use super::mapper;
use crate::authors::err::AuthorsError;
use crate::common::conf;
use crate::common::err::SystemError;
use crate::common::fs::file_reader::{Lines, Reader};
use crate::common::{conf, env};
use crate::Result;
use std::env;
use std::path::{Path, PathBuf};

pub enum LoadMode<'a> {
Expand Down Expand Up @@ -35,7 +33,7 @@ impl CSVReader {
fn from_cwd_fallback_home(file_reader: &dyn Reader) -> Result<Self> {
let file_path = &conf::authors_file();
let dir_path = &conf::authors_dir();
let cwd = env::current_dir().map_err(|_| SystemError::EnvVar("CWD".to_string()))?;
let cwd = env::cwd()?;

file_reader
.read_non_empty_lines(&cwd.join(file_path))
Expand All @@ -48,12 +46,12 @@ impl CSVReader {
}

fn xdg_or_home_fallback(file_reader: &dyn Reader, authors_dir: &str, file_path: &str) -> Result<Lines> {
let home = PathBuf::from(env::var("XDG_CONFIG_HOME")?);
let home = PathBuf::from(env::xdg_home()?);
file_reader.read_non_empty_lines(&home.join(authors_dir).join(file_path))
}

fn home_fallback(file_reader: &dyn Reader, authors_dir: &str, file_path: &str) -> Result<Lines> {
let home = env::var("HOME")?;
let home = env::home()?;
file_reader
.read_non_empty_lines(&PathBuf::from(format!("{home}/.config/{authors_dir}/{file_path}")))
.or_else(|_| file_reader.read_non_empty_lines(&PathBuf::from(format!("{home}/.{authors_dir}/{file_path}"))))
Expand Down
19 changes: 19 additions & 0 deletions src/common/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::err::SystemError;
use crate::Result;
use std::path::PathBuf;

pub fn cwd() -> Result<PathBuf> {
Ok(std::env::current_dir().map_err(|_| SystemError::EnvVar("CWD".to_string()))?)
}

pub fn home() -> Result<String> {
Ok(std::env::var("HOME").map_err(|_| SystemError::EnvVar("HOME".to_string()))?)
}

pub fn xdg_home() -> Result<String> {
Ok(std::env::var("XDG_CONFIG_HOME").map_err(|_| SystemError::EnvVar("XDG_CONFIG_HOME".to_string()))?)
}

pub fn editor() -> Result<String> {
Ok(std::env::var("EDITOR").map_err(|_| SystemError::EnvVar("EDITOR".to_string()))?)
}
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod conf;
pub mod env;
pub mod err;
pub mod runner;

Expand Down
5 changes: 2 additions & 3 deletions src/git/core/editor/file_editor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
common::runner::Runner,
common::{env, runner::Runner},
git::{core::conf_provider::ConfProvider, err::GitError},
Result,
};
use std::env;

#[cfg_attr(test, mockall::automock)]
pub trait Editor {
Expand All @@ -30,7 +29,7 @@ impl<R: Runner, C: ConfProvider> FileEditor<R, C> {
}

fn env_fallback(&self, path: &str) -> Result<()> {
match env::var("EDITOR") {
match env::editor() {
Err(_) => self.vim_fallback(path),
Ok(editor) => Ok(self.runner.spawn(&editor, path).map_err(|_| GitError::Editor)?),
}
Expand Down
4 changes: 2 additions & 2 deletions src/git/di.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::core::conf_provider::GitConfProvider;
use super::core::editor::file_editor::FileEditor;
use super::core::hook::Hook;
use super::core::libgit::wrapper::LibGitWrapper;
use super::err::GitError;
use super::service::GitService;
use crate::common::env;
use crate::common::fs::file_reader::SimpleReader;
use crate::common::fs::file_writer::SimpleWriter;
use crate::common::runner::CommandRunner;
Expand All @@ -14,7 +14,7 @@ type GitHook = Hook<CommandRunner>;
pub type Service = GitService<LibGitWrapper<SimpleReader>, GitHook, Editor, SimpleWriter>;

pub fn init() -> Result<Service> {
let cwd = std::env::current_dir().map_err(|_| GitError::InvalidRepo)?;
let cwd = env::cwd()?;

let wrapper = LibGitWrapper::from(&cwd, SimpleReader::new())?;
Ok(GitService::new(
Expand Down

0 comments on commit 41847de

Please sign in to comment.