Skip to content

Commit ff6e899

Browse files
committed
config: add CRATER_CONFIG env var to change config.toml path
1 parent 62c04aa commit ff6e899

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/config.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ use errors::*;
33
use regex::Regex;
44
use serde_regex;
55
use std::collections::{HashMap, HashSet};
6+
use std::env;
7+
use std::ffi::OsStr;
68
use std::fs::File;
79
use std::io::Read;
10+
use std::path::PathBuf;
811
use utils::size::Size;
912

10-
static CONFIG_FILE: &'static str = "config.toml";
13+
fn default_config_file() -> PathBuf {
14+
env::var_os("CRATER_CONFIG")
15+
.unwrap_or_else(|| OsStr::new("config.toml").to_os_string())
16+
.into()
17+
}
1118

1219
#[derive(Clone, Serialize, Deserialize)]
1320
#[serde(rename_all = "kebab-case")]
@@ -69,12 +76,12 @@ pub struct Config {
6976

7077
impl Config {
7178
pub fn load() -> Result<Self> {
72-
let buffer = Self::load_as_string(CONFIG_FILE)?;
79+
let buffer = Self::load_as_string(default_config_file())?;
7380

7481
Ok(::toml::from_str(&buffer)?)
7582
}
7683

77-
fn load_as_string(filename: &str) -> Result<String> {
84+
fn load_as_string(filename: PathBuf) -> Result<String> {
7885
let mut buffer = String::new();
7986
File::open(filename)?.read_to_string(&mut buffer)?;
8087

@@ -117,13 +124,13 @@ impl Config {
117124

118125
pub fn check(file: &Option<String>) -> Result<()> {
119126
if let Some(file) = file {
120-
Self::check_all(&file)
127+
Self::check_all(file.into())
121128
} else {
122-
Self::check_all(CONFIG_FILE)
129+
Self::check_all(default_config_file())
123130
}
124131
}
125132

126-
fn check_all(filename: &str) -> Result<()> {
133+
fn check_all(filename: PathBuf) -> Result<()> {
127134
use experiments::CrateSelect;
128135

129136
let buffer = Self::load_as_string(filename)?;

0 commit comments

Comments
 (0)