From 802ccc6d809ac6659d789573fb9cf1e19be820c3 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 13:50:20 +0100 Subject: [PATCH 1/3] feat(cli) Improve environment variables parsing. The following environment variables are considered incorrect: * `A`, no equal sign, * `A=`, value is missing, * `=A`, key is missing. The following environment variables are considered correct: * `A=B` with `A` for the name and `B` for the value, * `A=B=C` with `A` for the name and `B=C` for the value. --- lib/cli/src/utils.rs | 48 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/cli/src/utils.rs b/lib/cli/src/utils.rs index dfc693e91c1..4eaba96ed17 100644 --- a/lib/cli/src/utils.rs +++ b/lib/cli/src/utils.rs @@ -40,14 +40,50 @@ pub fn parse_mapdir(entry: &str) -> Result<(String, PathBuf)> { } } -/// Parses a mapdir from an env var +/// Parses an environment variable. pub fn parse_envvar(entry: &str) -> Result<(String, String)> { - if let [env_var, value] = entry.split('=').collect::>()[..] { - Ok((env_var.to_string(), value.to_string())) - } else { - bail!( - "Env vars must be of the form =. Found {}", + match entry.find('=') { + None => bail!( + "Environment variable must be of the form `=`; found `{}`", + &entry + ), + + Some(0) => bail!( + "Environment variable is not well formed, the `name` is missing in `=`; got `{}`", &entry + ), + + Some(position) if position == entry.len() - 1 => bail!( + "Environment variable is not well formed, the `value` is missing in `=`; got `{}`", + &entry + ), + + Some(position) => dbg!(Ok((entry[..position].into(), entry[position + 1..].into()))), + } +} + +#[cfg(test)] +mod tests { + use super::parse_envvar; + + #[test] + fn test_parse_envvar() { + assert_eq!( + parse_envvar("A").unwrap_err().to_string(), + "Environment variable must be of the form `=`; found `A`" + ); + assert_eq!( + parse_envvar("=A").unwrap_err().to_string(), + "Environment variable is not well formed, the `name` is missing in `=`; got `=A`" + ); + assert_eq!( + parse_envvar("A=").unwrap_err().to_string(), + "Environment variable is not well formed, the `value` is missing in `=`; got `A=`" + ); + assert_eq!(parse_envvar("A=B").unwrap(), ("A".into(), "B".into())); + assert_eq!( + parse_envvar("A=B=C=D").unwrap(), + ("A".into(), "B=C=D".into()) ); } } From ae7f9c28c1ef8f62854cd97f7628f48d67a49e59 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 13:56:08 +0100 Subject: [PATCH 2/3] doc(changelog) Add #2042. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 637b76e1945..7e7e71e2d57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#2026](https://github.com/wasmerio/wasmer/pull/2010) Expose trap code of a `RuntimeError`, if it's a `Trap`. ### Changed +- [#2042](https://github.com/wasmerio/wasmer/pull/2042) Parse more exotic environment variables in `wasmer run`. - [#2041](https://github.com/wasmerio/wasmer/pull/2041) Documentation diagrams now have a solid white background rather than a transparent background. ### Fixed From 0bf86c8d01babeee34cf05244afe4c693225e724 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 13:59:30 +0100 Subject: [PATCH 3/3] feat(cli) Trim environment variables before parsing them. --- lib/cli/src/utils.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/utils.rs b/lib/cli/src/utils.rs index 4eaba96ed17..a032f26f40b 100644 --- a/lib/cli/src/utils.rs +++ b/lib/cli/src/utils.rs @@ -42,6 +42,8 @@ pub fn parse_mapdir(entry: &str) -> Result<(String, PathBuf)> { /// Parses an environment variable. pub fn parse_envvar(entry: &str) -> Result<(String, String)> { + let entry = entry.trim(); + match entry.find('=') { None => bail!( "Environment variable must be of the form `=`; found `{}`", @@ -58,7 +60,7 @@ pub fn parse_envvar(entry: &str) -> Result<(String, String)> { &entry ), - Some(position) => dbg!(Ok((entry[..position].into(), entry[position + 1..].into()))), + Some(position) => Ok((entry[..position].into(), entry[position + 1..].into())), } } @@ -81,6 +83,7 @@ mod tests { "Environment variable is not well formed, the `value` is missing in `=`; got `A=`" ); assert_eq!(parse_envvar("A=B").unwrap(), ("A".into(), "B".into())); + assert_eq!(parse_envvar(" A=B\t").unwrap(), ("A".into(), "B".into())); assert_eq!( parse_envvar("A=B=C=D").unwrap(), ("A".into(), "B=C=D".into())