Start implementing output tests, don't panic on missing basedir
This commit is contained in:
parent
ae043f5cd2
commit
9ae8eaf3c2
|
@ -19,11 +19,12 @@ pub struct Cli {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cli {
|
impl Cli {
|
||||||
|
// Returns a PathBuf to the basedir. If it's a relative link,
|
||||||
|
// it's not expanded here! Panics if no basedir is provided and $HOME isn't defined
|
||||||
pub fn basedir(&self) -> PathBuf {
|
pub fn basedir(&self) -> PathBuf {
|
||||||
if let Some(basedir) = &self.basedir {
|
if let Some(basedir) = &self.basedir {
|
||||||
|
// Returns an error when the path doesn't exist
|
||||||
PathBuf::from(basedir)
|
PathBuf::from(basedir)
|
||||||
.canonicalize()
|
|
||||||
.expect("failed to expand relative path")
|
|
||||||
} else {
|
} else {
|
||||||
let mut home_path = PathBuf::from(env::var("HOME").expect("No $HOME in env"));
|
let mut home_path = PathBuf::from(env::var("HOME").expect("No $HOME in env"));
|
||||||
home_path.push(".forgebuild");
|
home_path.push(".forgebuild");
|
||||||
|
|
42
src/log.rs
42
src/log.rs
|
@ -7,10 +7,20 @@ use lazy_static::lazy_static;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref LOGLEVEL: LogLevel = LogLevel::from_env();
|
static ref LOGLEVEL: LogLevel = LogLevel::from_env();
|
||||||
static ref LANG: String = lang_from_env();
|
static ref LANG: Lang = lang_from_env();
|
||||||
static ref TRANSLATIONS: HashMap<String, String> = load_translations();
|
static ref TRANSLATIONS: HashMap<String, String> = load_translations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Lang configures how to deal with translations. It has three possible values:
|
||||||
|
/// None: translations return their own key name
|
||||||
|
/// Replace: wtf to do with this?
|
||||||
|
/// Some(code): where code is the language code for the JSON translation file
|
||||||
|
pub enum Lang {
|
||||||
|
None,
|
||||||
|
Replace,
|
||||||
|
Some(String)
|
||||||
|
}
|
||||||
|
|
||||||
pub type Context<'a> = &'a HashMap<&'a str, &'a str>;
|
pub type Context<'a> = &'a HashMap<&'a str, &'a str>;
|
||||||
|
|
||||||
/// Returns a PathBuf containing the translations. Does not mean
|
/// Returns a PathBuf containing the translations. Does not mean
|
||||||
|
@ -36,11 +46,13 @@ fn find_translations() -> PathBuf {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_translations() -> HashMap<String, String> {
|
fn load_translations() -> HashMap<String, String> {
|
||||||
|
match &*LANG {
|
||||||
|
Lang::Some(lang) => {
|
||||||
let mut path = find_translations();
|
let mut path = find_translations();
|
||||||
if !path.is_dir() {
|
if !path.is_dir() {
|
||||||
panic!("Could not find translations in {:?}", path);
|
panic!("Could not find translations in {:?}", path);
|
||||||
}
|
}
|
||||||
path.push(format!("{}.json", *LANG));
|
path.push(format!("{}.json", lang));
|
||||||
|
|
||||||
if !path.is_file() {
|
if !path.is_file() {
|
||||||
panic!("Could not find translation file: {:?}", path);
|
panic!("Could not find translation file: {:?}", path);
|
||||||
|
@ -59,21 +71,39 @@ fn load_translations() -> HashMap<String, String> {
|
||||||
panic!("IO ERROR: {}", e);
|
panic!("IO ERROR: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
HashMap::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trans(key: &str) -> String {
|
fn trans(key: &str) -> String {
|
||||||
match TRANSLATIONS.get(key) {
|
match &*LANG {
|
||||||
|
Lang::Some(lang) => match TRANSLATIONS.get(key) {
|
||||||
Some(t) => t.to_string(),
|
Some(t) => t.to_string(),
|
||||||
None => {
|
None => {
|
||||||
panic!("Unknown translation string in lang {}: {}", *LANG, key);
|
panic!("Unknown translation string in lang {}: {}", lang, key);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Lang::None => {
|
||||||
|
// Return the requested key itself (for output tests)
|
||||||
|
key.to_string()
|
||||||
|
},
|
||||||
|
Lang::Replace => {
|
||||||
|
unimplemented!("Don't know what to do with this yet but we need a way to test variable substitution");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lang_from_env() -> String {
|
fn lang_from_env() -> Lang {
|
||||||
let lang =
|
let lang =
|
||||||
env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
|
env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
|
||||||
lang[0..2].to_string()
|
match lang.as_str() {
|
||||||
|
"NONE" => Lang::None,
|
||||||
|
"REPLACE" => Lang::Replace,
|
||||||
|
_ => Lang::Some(lang[0..2].to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct LogLevel {
|
struct LogLevel {
|
||||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -11,7 +11,18 @@ mod task;
|
||||||
|
|
||||||
fn main() -> Result<(), std::io::Error> {
|
fn main() -> Result<(), std::io::Error> {
|
||||||
let cmd = cli::Cli::from_args();
|
let cmd = cli::Cli::from_args();
|
||||||
let base_dir: String = cmd.basedir().to_str().unwrap().into();
|
|
||||||
|
let mut context = HashMap::new();
|
||||||
|
|
||||||
|
let base_dir = cmd.basedir();
|
||||||
|
let base_dir = match base_dir.canonicalize() {
|
||||||
|
Ok(p) => p.to_str().unwrap().to_string(),
|
||||||
|
Err(_) => {
|
||||||
|
context.insert("i18n_basedir", base_dir.to_str().unwrap());
|
||||||
|
log::error("missing_basedir", Some(&context));
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
set_var("GITBUILDDIR", &base_dir);
|
set_var("GITBUILDDIR", &base_dir);
|
||||||
|
|
||||||
|
@ -36,7 +47,7 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut context = HashMap::new();
|
let mut context = context.clone();
|
||||||
context.insert("$i18n_task", task.name.as_str());
|
context.insert("$i18n_task", task.name.as_str());
|
||||||
log::debug("found_task", Some(&context));
|
log::debug("found_task", Some(&context));
|
||||||
// Maybe the task has a source we should clone?
|
// Maybe the task has a source we should clone?
|
||||||
|
|
Loading…
Reference in New Issue