Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli) Improve environment variables parsing #2042

Merged
merged 4 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 45 additions & 6 deletions lib/cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,53 @@ 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::<Vec<&str>>()[..] {
Ok((env_var.to_string(), value.to_string()))
} else {
bail!(
"Env vars must be of the form <var_name>=<value>. Found {}",
let entry = entry.trim();

match entry.find('=') {
None => bail!(
"Environment variable must be of the form `<name>=<value>`; found `{}`",
&entry
),

Some(0) => bail!(
"Environment variable is not well formed, the `name` is missing in `<name>=<value>`; got `{}`",
&entry
),

Some(position) if position == entry.len() - 1 => bail!(
"Environment variable is not well formed, the `value` is missing in `<name>=<value>`; got `{}`",
&entry
),

Some(position) => Ok((entry[..position].into(), entry[position + 1..].into())),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual splitting can just be done with just https://doc.rust-lang.org/std/primitive.str.html#method.split_at ; but we won't have as many error cases to report.

Just a heads up, I'm fine keeping it as-is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to deal with empty string for the left- or right-end side of the =. I think the code would not be simplified that much. Thanks for the heads-up!

}
}

#[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 `<name>=<value>`; found `A`"
);
assert_eq!(
parse_envvar("=A").unwrap_err().to_string(),
"Environment variable is not well formed, the `name` is missing in `<name>=<value>`; got `=A`"
);
assert_eq!(
parse_envvar("A=").unwrap_err().to_string(),
"Environment variable is not well formed, the `value` is missing in `<name>=<value>`; 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())
);
}
}