Manually parse environment variables

This commit is contained in:
Ajeet D'Souza 2020-03-28 00:38:36 +05:30
parent 5547fb4b80
commit d4fb1a05cf
11 changed files with 69 additions and 71 deletions

10
Cargo.lock generated
View File

@ -136,14 +136,6 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "envy"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "getrandom"
version = "0.1.14"
@ -421,7 +413,6 @@ dependencies = [
"bstr 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"envy 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"indoc 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
@ -446,7 +437,6 @@ dependencies = [
"checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
"checksum envy 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f938a4abd5b75fe3737902dbc2e79ca142cc1526827a9e40b829a086758531a9"
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
"checksum hermit-abi 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8"

View File

@ -17,7 +17,6 @@ anyhow = "1.0.27"
bincode = "1.2.1"
clap = "2.33.0"
dirs = "2.0.2"
envy = "0.4.1"
indoc = "0.3.5"
serde = { version = "1.0.105", features = ["derive"] }
structopt = "0.3.12"

41
src/config.rs Normal file
View File

@ -0,0 +1,41 @@
use crate::types::Rank;
use anyhow::{bail, Context, Result};
use std::env;
use std::fs;
use std::path::PathBuf;
pub fn zo_data() -> Result<PathBuf> {
let path = match env::var_os("_ZO_DATA") {
Some(data_osstr) => PathBuf::from(data_osstr),
None => {
if let Some(mut cache_dir) = dirs::cache_dir() {
cache_dir.push("zoxide");
cache_dir
} else if let Some(mut home_dir) = dirs::home_dir() {
home_dir.push(".zoxide");
home_dir
} else {
bail!("could not generate default directory, please set _ZO_DATA manually");
}
}
};
fs::create_dir_all(&path).context("could not create _ZO_DATA directory")?;
Ok(path)
}
pub fn zo_maxage() -> Result<Rank> {
match env::var_os("_ZO_MAXAGE") {
Some(maxage_osstr) => match maxage_osstr.to_str() {
Some(maxage_str) => {
let maxage = maxage_str
.parse::<i64>()
.context("unable to parse _ZO_MAXAGE as integer")?;
Ok(maxage as Rank)
}
None => bail!("invalid Unicode in _ZO_MAXAGE"),
},
None => Ok(1000.0),
}
}

View File

@ -163,11 +163,11 @@ impl DB {
for dir in &mut self.dirs {
dir.rank *= factor;
}
self.dirs.retain(|dir| dir.rank >= 1.0);
}
self.dirs.retain(|dir| dir.rank >= 1.0);
self.modified = true;
Ok(())
}
@ -214,7 +214,7 @@ impl DB {
fn get_path_tmp(&self) -> PathBuf {
let mut path_tmp = self.path.clone();
path_tmp.set_file_name(".zo.tmp");
path_tmp.set_file_name("db.zo.tmp");
path_tmp
}

View File

@ -1,21 +0,0 @@
use serde::Deserialize;
use std::path::PathBuf;
#[derive(Deserialize, Debug)]
pub struct Env {
#[serde(default = "default_maxage")]
pub maxage: i64,
#[serde(default = "default_data")]
pub data: Option<PathBuf>,
}
fn default_maxage() -> i64 {
1000
}
fn default_data() -> Option<PathBuf> {
let mut path = dirs::home_dir()?;
path.push(".zo");
Some(path)
}

View File

@ -1,13 +1,11 @@
mod config;
mod db;
mod dir;
mod env;
mod subcommand;
mod types;
mod util;
use crate::env::Env;
use anyhow::{Context, Result};
use anyhow::Result;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
@ -22,16 +20,13 @@ enum Zoxide {
pub fn main() -> Result<()> {
let opt = Zoxide::from_args();
let env = envy::prefixed("_ZO_")
.from_env::<Env>()
.context("could not parse environment variables")?;
match opt {
Zoxide::Add(add) => add.run(&env)?,
Zoxide::Import(import) => import.run(&env)?,
Zoxide::Add(add) => add.run()?,
Zoxide::Import(import) => import.run()?,
Zoxide::Init(init) => init.run()?,
Zoxide::Query(query) => query.run(&env)?,
Zoxide::Remove(remove) => remove.run(&env)?,
Zoxide::Query(query) => query.run()?,
Zoxide::Remove(remove) => remove.run()?,
};
Ok(())

View File

@ -1,5 +1,4 @@
use crate::env::Env;
use crate::types::Rank;
use crate::config;
use crate::util;
use anyhow::{Context, Result};
@ -13,10 +12,10 @@ pub struct Add {
}
impl Add {
pub fn run(&self, env: &Env) -> Result<()> {
let mut db = util::get_db(env)?;
pub fn run(&self) -> Result<()> {
let mut db = util::get_db()?;
let now = util::get_current_time()?;
let maxage = env.maxage as Rank;
let maxage = config::zo_maxage()?;
match &self.path {
Some(path) => db.add(path, maxage, now),

View File

@ -1,4 +1,3 @@
use crate::env::Env;
use crate::util;
use anyhow::Result;
@ -14,7 +13,7 @@ pub struct Import {
}
impl Import {
pub fn run(&self, env: &Env) -> Result<()> {
util::get_db(env)?.import(&self.path, self.merge)
pub fn run(&self) -> Result<()> {
util::get_db()?.import(&self.path, self.merge)
}
}

View File

@ -1,4 +1,3 @@
use crate::env::Env;
use crate::util;
use anyhow::{bail, Result};
@ -15,11 +14,11 @@ pub struct Query {
}
impl Query {
pub fn run(mut self, env: &Env) -> Result<()> {
pub fn run(mut self) -> Result<()> {
let path_opt = if self.interactive {
self.query_interactive(env)?
self.query_interactive()?
} else {
self.query(env)?
self.query()?
};
match path_opt {
@ -36,7 +35,7 @@ impl Query {
Ok(())
}
fn query(&mut self, env: &Env) -> Result<Option<Vec<u8>>> {
fn query(&mut self) -> Result<Option<Vec<u8>>> {
if let [path] = self.keywords.as_slice() {
if Path::new(path).is_dir() {
return Ok(Some(path.as_bytes().to_vec()));
@ -49,7 +48,7 @@ impl Query {
*keyword = keyword.to_lowercase();
}
if let Some(dir) = util::get_db(env)?.query(&self.keywords, now) {
if let Some(dir) = util::get_db()?.query(&self.keywords, now) {
// `path_to_bytes` is guaranteed to succeed here since
// the path has already been queried successfully
let path_bytes = util::path_to_bytes(&dir.path).unwrap();
@ -59,14 +58,14 @@ impl Query {
}
}
fn query_interactive(&mut self, env: &Env) -> Result<Option<Vec<u8>>> {
fn query_interactive(&mut self) -> Result<Option<Vec<u8>>> {
let now = util::get_current_time()?;
for keyword in &mut self.keywords {
*keyword = keyword.to_lowercase();
}
let dirs = util::get_db(env)?.query_all(&self.keywords);
let dirs = util::get_db()?.query_all(&self.keywords);
util::fzf_helper(now, dirs)
}
}

View File

@ -1,4 +1,3 @@
use crate::env::Env;
use crate::util;
use anyhow::Result;
@ -11,7 +10,7 @@ pub struct Remove {
}
impl Remove {
pub fn run(&self, env: &Env) -> Result<()> {
util::get_db(env)?.remove(&self.path)
pub fn run(&self) -> Result<()> {
util::get_db()?.remove(&self.path)
}
}

View File

@ -1,6 +1,6 @@
use crate::config;
use crate::db::DB;
use crate::dir::Dir;
use crate::env::Env;
use crate::types::Epoch;
use anyhow::{anyhow, bail, Context, Result};
@ -21,11 +21,9 @@ pub fn path_to_bytes<P: AsRef<Path>>(path: &P) -> Option<&[u8]> {
Some(path.as_ref().to_str()?.as_bytes())
}
pub fn get_db(env: &Env) -> Result<DB> {
let path = env
.data
.as_ref()
.ok_or_else(|| anyhow!("could not locate database file"))?;
pub fn get_db() -> Result<DB> {
let mut path = config::zo_data()?;
path.push("db.zo");
DB::open(path)
}