#![allow(clippy::ptr_arg)]
mod parser;
use clap::ArgEnum;
use indexmap::IndexMap;
use std::string::String;
use crate::cli::Shell;
use crate::convert::parser::ValuePartKind;
use crate::structure::{EnvVariableOption, EnvVariableValue, EnvironmentVariables};
pub(crate) fn to_shell_source(vars: &EnvironmentVariables, shell: &Shell) -> String {
let mut output = String::new();
for (name, variable_option) in vars {
let raw_value = match variable_option {
EnvVariableOption::General(v) => v,
EnvVariableOption::Specific(map) => match value_for_specific(shell, map) {
Some(v) => v, None => continue, },
};
let (value, is_path) = &match raw_value {
EnvVariableValue::Set(false) => {
add_script_line::unset_variable(&mut output, shell, name);
continue;
}
EnvVariableValue::String(string) => (expand_value(string.as_str(), shell), false),
EnvVariableValue::Set(true) => (String::from("1"), false),
EnvVariableValue::Array(array) => {
let v_expanded: Vec<String> = array
.iter()
.map(|value| expand_value(value, shell))
.collect();
(v_expanded.join(":"), true)
}
};
add_script_line::set_variable(&mut output, shell, name, value, is_path);
}
output
}
mod add_script_line {
use super::Shell;
pub fn set_variable(
output: &mut String,
shell: &Shell,
name: &str,
value: &str,
is_path: &bool,
) {
if log_enabled!(log::Level::Trace) {
let variable_log_header = match is_path {
true => "[Set]",
false => "'Set'",
};
trace!("{}: {} -> {}", variable_log_header, name, value);
};
*output += &match shell {
Shell::Bash | Shell::Zsh => {
format!("export {}=", name)
}
Shell::Fish => {
let path = match is_path {
true => " --path",
false => "",
};
format!("set -gx{path} {} ", name, path = path)
}
};
*output += value;
*output += "\n";
}
pub fn unset_variable(output: &mut String, shell: &Shell, name: &str) {
trace!("Unset: {}", name);
*output += &match shell {
Shell::Bash | Shell::Zsh => {
format!("unset {}", name)
}
Shell::Fish => {
format!("set -ge {}", name)
}
};
*output += "\n";
}
}
fn value_for_specific<'a>(
shell: &Shell,
map: &'a IndexMap<String, EnvVariableValue>,
) -> Option<&'a EnvVariableValue> {
let shell_name = shell.to_possible_value()?.get_name();
map.get(shell_name).or_else(|| map.get("_"))
}
fn expand_value(value: &str, shell: &Shell) -> String {
use Shell::*;
use ValuePartKind::*;
let value_parts = parser::parse_value(value);
let mut expanded_value = String::new();
for part in value_parts {
let parser::ValuePart { value, kind } = part;
expanded_value += &match (kind, shell) {
(Literal, _) => single_quote(value, shell),
(ShellVariable, Bash | Zsh | Fish) => format!(r#""${}""#, value),
(ShellCommand, Bash | Zsh) => format!("$(eval {})", single_quote(value, shell)),
(ShellCommand, Fish) => format!("(eval {})", single_quote(value, shell)),
(Home, Bash | Zsh) => {
format!("$(eval echo \"~{}\")", value)
}
(Home, Fish) => format!("(eval echo \"~{}\")", value),
}
}
expanded_value
}
fn single_quote(string: String, shell: &Shell) -> String {
use Shell::*;
match shell {
Bash | Zsh => string
.split('\'')
.map(|v| {
if v.is_empty() {
String::new()
} else {
format!("'{}'", v)
}
})
.collect::<Vec<String>>()
.join(r#""'""#),
Fish => format!("'{}'", string.replace('\\', "\\\\").replace('\'', "\\'")),
}
}
#[cfg(test)]
mod test_conversion {
use super::*;
use pretty_assertions::{assert_eq, assert_str_eq};
use crate::structure::*;
use EnvVariableOption::*;
use indexmap::indexmap;
use indoc::indoc;
use maplit::hashmap;
use std::collections::HashMap;
fn assert_convert(
toml_str: &str,
map: EnvironmentVariables,
shell_sources: HashMap<Shell, &str>,
) {
assert_eq!(
toml::from_str::<ConfigFile>(toml_str)
.expect("Invalid toml formatting")
.vars,
map,
"Compare toml data to its `EnvironmentVariables` representation",
);
for (shell, shell_source) in shell_sources {
assert_str_eq!(
to_shell_source(&map, &shell).trim_end_matches('\n'),
shell_source.trim_end_matches('\n'),
"Check for correct {:?} syntax",
&shell,
);
}
}
#[test]
fn test_convert_string() {
assert_convert(
r#"FOO = "Bar""#,
indexmap! {
"FOO".into() => General(EnvVariableValue::String("Bar".into())),
},
hashmap! {
Shell::Bash => r#"export FOO='Bar'"#,
Shell::Zsh => r#"export FOO='Bar'"#,
Shell::Fish => r#"set -gx FOO 'Bar'"#,
},
)
}
#[test]
fn test_convert_path() {
assert_convert(
r#"PATH = ["/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"]"#,
indexmap! {
"PATH".into() => General(EnvVariableValue::Array(vec![
"/usr/local/bin".into(),
"/usr/bin".into(),
"/bin".into(),
"/usr/sbin".into(),
"/sbin".into(),
])),
},
hashmap! {
Shell::Bash => r#"export PATH='/usr/local/bin':'/usr/bin':'/bin':'/usr/sbin':'/sbin'"#,
Shell::Zsh => r#"export PATH='/usr/local/bin':'/usr/bin':'/bin':'/usr/sbin':'/sbin'"#,
Shell::Fish => r#"set -gx --path PATH '/usr/local/bin':'/usr/bin':'/bin':'/usr/sbin':'/sbin'"#,
},
)
}
#[test]
fn test_convert_set() {
assert_convert(
"HOMEBREW_NO_ANALYTICS = true",
indexmap! {
"HOMEBREW_NO_ANALYTICS".into() => General(EnvVariableValue::Set(true)),
},
hashmap! {
Shell::Bash => r#"export HOMEBREW_NO_ANALYTICS=1"#,
Shell::Zsh => r#"export HOMEBREW_NO_ANALYTICS=1"#,
Shell::Fish => r#"set -gx HOMEBREW_NO_ANALYTICS 1"#,
},
)
}
#[test]
fn test_convert_unset() {
assert_convert(
"HOMEBREW_NO_ANALYTICS = false",
indexmap! {
"HOMEBREW_NO_ANALYTICS".into() => General(EnvVariableValue::Set(false)),
},
hashmap! {
Shell::Bash => r#"unset HOMEBREW_NO_ANALYTICS"#,
Shell::Zsh => r#"unset HOMEBREW_NO_ANALYTICS"#,
Shell::Fish => r#"set -ge HOMEBREW_NO_ANALYTICS"#,
},
)
}
#[test]
fn test_convert_specific() {
assert_convert(
r#"ONLY_FOR_BASH.bash = "Do people read test cases?""#,
indexmap! {
"ONLY_FOR_BASH".into() => Specific(indexmap! {
"bash".into() => EnvVariableValue::String("Do people read test cases?".into()),
}),
},
hashmap! {
Shell::Bash => r#"export ONLY_FOR_BASH='Do people read test cases?'"#,
Shell::Zsh => "",
Shell::Fish => "",
},
)
}
#[test]
fn test_convert_specific_other() {
assert_convert(
indoc! {r#"
SOME_VARIABLE.fish = "you're pretty"
SOME_VARIABLE._ = '[ACCESS DENIED]'
"#},
indexmap! {
"SOME_VARIABLE".into() => Specific(indexmap! {
"fish".into() => EnvVariableValue::String("you're pretty".into()),
"_".into() => EnvVariableValue::String("[ACCESS DENIED]".into()),
})
},
hashmap! {
Shell::Bash => r#"export SOME_VARIABLE='[ACCESS DENIED]'"#,
Shell::Zsh => r#"export SOME_VARIABLE='[ACCESS DENIED]'"#,
Shell::Fish => r#"set -gx SOME_VARIABLE 'you\'re pretty'"#,
},
)
}
#[test]
fn test_convert_specific_other_alt() {
assert_convert(
indoc! {r#"
[SOME_VARIABLE]
fish = "you're pretty"
_ = '[ACCESS DENIED]'
[ANOTHER_VARIABLE]
zsh = 'Zzz'
"#},
indexmap! {
"SOME_VARIABLE".into() => Specific(indexmap! {
"fish".into() => EnvVariableValue::String("you're pretty".into()),
"_".into() => EnvVariableValue::String("[ACCESS DENIED]".into()),
}),
"ANOTHER_VARIABLE".into() => Specific(indexmap! {
"zsh".into() => EnvVariableValue::String("Zzz".into()),
}),
},
hashmap! {
Shell::Bash => r#"export SOME_VARIABLE='[ACCESS DENIED]'"#,
Shell::Zsh => indoc! (r#"
export SOME_VARIABLE='[ACCESS DENIED]'
export ANOTHER_VARIABLE='Zzz'
"#),
Shell::Fish => r#"set -gx SOME_VARIABLE 'you\'re pretty'"#,
},
)
}
#[test]
fn test_shell_variables_inline() {
assert_convert(
r#"WHERE_THE_HEART_IS = "$HOME""#,
indexmap! {
"WHERE_THE_HEART_IS".into() => General(EnvVariableValue::String("$HOME".into())),
},
hashmap! {
Shell::Bash => r#"export WHERE_THE_HEART_IS="$HOME""#,
Shell::Zsh => r#"export WHERE_THE_HEART_IS="$HOME""#,
Shell::Fish => r#"set -gx WHERE_THE_HEART_IS "$HOME""#,
},
)
}
#[test]
fn test_shell_variables_block() {
assert_convert(
r#"AN_EXAMPLE = "${HOME}less""#,
indexmap! {
"AN_EXAMPLE".into() => General(EnvVariableValue::String("${HOME}less".into())),
},
hashmap! {
Shell::Bash => r#"export AN_EXAMPLE="$HOME"'less'"#,
Shell::Zsh => r#"export AN_EXAMPLE="$HOME"'less'"#,
Shell::Fish => r#"set -gx AN_EXAMPLE "$HOME"'less'"#,
},
)
}
#[test]
fn test_shell_commands() {
assert_convert(
r#"EDITOR = "$(which micro)""#,
indexmap! {
"EDITOR".into() => General(EnvVariableValue::String("$(which micro)".into())),
},
hashmap! {
Shell::Bash => r#"export EDITOR=$(eval 'which micro')"#,
Shell::Zsh => r#"export EDITOR=$(eval 'which micro')"#,
Shell::Fish => r#"set -gx EDITOR (eval 'which micro')"#,
},
)
}
#[test]
fn test_shell_home_tilde() {
assert_convert(
r#"HOME = "~superatomic""#,
indexmap! {
"HOME".into() => General(EnvVariableValue::String("~superatomic".into())),
},
hashmap! {
Shell::Bash => r#"export HOME=$(eval echo "~superatomic")"#,
Shell::Zsh => r#"export HOME=$(eval echo "~superatomic")"#,
Shell::Fish => r#"set -gx HOME (eval echo "~superatomic")"#,
},
)
}
#[test]
fn test_convert_character_escapes() {
assert_convert(
indoc! {r#"
MESSAGE = '''\$() is literal, and $(echo '\)') is escaped.'''
FAVORITE_CHARACTER = '\\'
"#},
indexmap! {
"MESSAGE".into() => General(EnvVariableValue::String(
r"\$() is literal, and $(echo '\)') is escaped.".into()
)),
"FAVORITE_CHARACTER".into() => General(EnvVariableValue::String(r"\\".into())),
},
hashmap! {
Shell::Bash => indoc!(r#"
export MESSAGE='$() is literal, and '$(eval 'echo '"'"')'"'")' is escaped.'
export FAVORITE_CHARACTER='\'
"#),
Shell::Zsh => indoc!(r#"
export MESSAGE='$() is literal, and '$(eval 'echo '"'"')'"'")' is escaped.'
export FAVORITE_CHARACTER='\'
"#),
Shell::Fish => indoc!(r#"
set -gx MESSAGE '$() is literal, and '(eval 'echo \')\'')' is escaped.'
set -gx FAVORITE_CHARACTER '\\'
"#),
},
)
}
#[test]
fn test_convert_escaping_quotes() {
assert_convert(
"MESSAGE = '''I 'love' books'''",
indexmap! {
"MESSAGE".into() => General(EnvVariableValue::String(r"I 'love' books".into())),
},
hashmap! {
Shell::Bash => r#"export MESSAGE='I '"'"'love'"'"' books'"#,
Shell::Zsh => r#"export MESSAGE='I '"'"'love'"'"' books'"#,
Shell::Fish => r#"set -gx MESSAGE 'I \'love\' books'"#,
},
)
}
#[test]
fn test_convert_everything() {
assert_convert(
indoc! {r#"
# A collection of random things for testing.
FOO = 'bar'
BAZ.zsh = 'zž'
BAZ.fish = ['gone', '$fishing']
BAZ._ = '~other'
TTY = '$(tty)'
THE_ECHO = '$(echo "\)")'
XSHE_IS_THE_BEST = true # look, idk.
# Return to poluting the home directory in bash
XDG_CONFIG_HOME.bash = false
"#},
indexmap! {
"FOO".into() => General(EnvVariableValue::String("bar".into())),
"BAZ".into() => Specific(indexmap! {
"zsh".into() => EnvVariableValue::String("zž".into()),
"fish".into() => EnvVariableValue::Array(vec!["gone".into(), "$fishing".into()]),
"_".into() => EnvVariableValue::String("~other".into()),
}),
"TTY".into() => General(EnvVariableValue::String("$(tty)".into())),
"THE_ECHO".into() => General(EnvVariableValue::String(r#"$(echo "\)")"#.into())),
"XSHE_IS_THE_BEST".into() => General(EnvVariableValue::Set(true)),
"XDG_CONFIG_HOME".into() => Specific(indexmap! {
"bash".into() => EnvVariableValue::Set(false),
}),
},
hashmap! {
Shell::Bash => indoc! (r#"
export FOO='bar'
export BAZ=$(eval echo "~other")
export TTY=$(eval 'tty')
export THE_ECHO=$(eval 'echo ")"')
export XSHE_IS_THE_BEST=1
unset XDG_CONFIG_HOME
"#),
Shell::Zsh => indoc! (r#"
export FOO='bar'
export BAZ='zž'
export TTY=$(eval 'tty')
export THE_ECHO=$(eval 'echo ")"')
export XSHE_IS_THE_BEST=1
"#),
Shell::Fish => indoc! (r#"
set -gx FOO 'bar'
set -gx --path BAZ 'gone':"$fishing"
set -gx TTY (eval 'tty')
set -gx THE_ECHO (eval 'echo ")"')
set -gx XSHE_IS_THE_BEST 1
"#),
},
)
}
}