Start JSON debug output for context

This commit is contained in:
southerntofu 2020-11-28 20:36:11 +01:00
parent 78b5ebcd64
commit b639d1d82b
4 changed files with 41 additions and 23 deletions

15
Cargo.lock generated
View File

@ -48,6 +48,7 @@ dependencies = [
"glob",
"hostname",
"lazy_static",
"serde",
"serde_json",
"structopt",
]
@ -166,6 +167,20 @@ name = "serde"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"

View File

@ -14,3 +14,5 @@ hostname = "0.3"
# Translations
serde_json = "1.0"
structopt = "0.3"
# Debug Context for translations
serde = { version = "1.0", features = ["derive"] }

View File

@ -13,11 +13,11 @@ lazy_static! {
/// 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
/// JsonContext: debug output for Context as JSON
pub enum Lang {
None,
Replace,
JsonContext,
Some(String)
}
@ -25,9 +25,10 @@ impl Lang {
fn from_env() -> Lang {
let lang =
env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
// TODO: do lowercase matching
match lang.as_str() {
"NONE" => Lang::None,
"REPLACE" => Lang::Replace,
"JSON" => Lang::JsonContext,
_ => Lang::Some(lang[0..2].to_string())
}
}
@ -92,22 +93,27 @@ fn load_translations() -> HashMap<String, String> {
fn trans(key: &str) -> String {
match &*LANG {
Lang::Some(lang) => match TRANSLATIONS.get(key) {
Some(t) => t.to_string(),
None => {
panic!("Unknown translation string in lang {}: {}", lang, key);
Lang::Some(lang) => {
if let Some(t) = TRANSLATIONS.get(key) {
t.to_string()
} else {
panic!("Missing translation for {} in lang {}", key, lang)
}
},
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");
}
Lang::JsonContext => String::new(),
Lang::None => key.to_string(),
}
}
fn trans_context(key: &str, context: &Context) -> String {
match &*LANG {
Lang::JsonContext => {
// Serialize the context to JSON for debugging
serde_json::to_string(context).expect("Failed to serialize to JSON")
},
_ => expand(&trans(key), context)
}
}
struct LogLevel {
info: bool,
@ -158,28 +164,24 @@ fn expand(msg: &str, vars: &Context) -> String {
pub fn info(msg: &str, vars: &Context) {
if LOGLEVEL.info {
let t_msg = expand(&trans(msg), vars);
println!("[git-build] {}", t_msg);
println!("[git-build] {}", trans_context(msg, vars));
}
}
pub fn error(msg: &str, vars: &Context) {
if LOGLEVEL.error {
let t_msg = expand(&trans(msg), vars);
eprintln!("{}{}", trans("error"), t_msg);
eprintln!("{}{}", trans("error"), trans_context(msg, vars));
}
}
pub fn warn(msg: &str, vars: &Context) {
if LOGLEVEL.error {
let t_msg = expand(&trans(msg), vars);
eprintln!("{}{}", trans("warning"), t_msg);
eprintln!("{}{}", trans("warning"), trans_context(msg, vars));
}
}
pub fn debug(msg: &str, vars: &Context) {
if LOGLEVEL.debug {
let t_msg = expand(&trans(msg), vars);
println!("{}{}", trans("debug"), t_msg);
println!("{}{}", trans("debug"), trans_context(msg, vars));
}
}

View File

@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::env::{set_current_dir as cd, set_var};
use structopt::StructOpt;
// For UNIX extended metadata