feat: Default env variables
This commit is contained in:
21
README.md
21
README.md
@@ -185,6 +185,27 @@ let command = cmd!(
|
||||
assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" TEST="test" "echo" "test""#);
|
||||
```
|
||||
|
||||
#### Conditional
|
||||
You can have a default value set for an environment variable.
|
||||
|
||||
```rust
|
||||
use comlexr::cmd;
|
||||
|
||||
const NEW_VAR: &str = "NEW_VAR";
|
||||
std::env::set_var("TEST", "realvalue");
|
||||
|
||||
let command = cmd!(
|
||||
env {
|
||||
"TEST":? "test",
|
||||
NEW_VAR: "new_var"
|
||||
};
|
||||
"echo",
|
||||
"test",
|
||||
);
|
||||
|
||||
assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" "echo" "test""#);
|
||||
```
|
||||
|
||||
#### Current Directory and Env Variable Order Matters
|
||||
Environment variable declarations **MUST** come after the current directory declaration.
|
||||
|
||||
|
||||
@@ -437,25 +437,51 @@ impl ToTokens for EnvVars {
|
||||
struct EnvVar {
|
||||
key: Value,
|
||||
value: Value,
|
||||
conditional: bool,
|
||||
}
|
||||
|
||||
impl Parse for EnvVar {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let key = input.parse()?;
|
||||
|
||||
_ = input.parse::<Token![:]>()?;
|
||||
|
||||
let conditional = if input.lookahead1().peek(Token![?]) {
|
||||
input.parse::<Token![?]>()?;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let value = input.parse()?;
|
||||
|
||||
Ok(Self { key, value })
|
||||
Ok(Self {
|
||||
key,
|
||||
value,
|
||||
conditional,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for EnvVar {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
let Self { key, value } = self;
|
||||
let Self {
|
||||
key,
|
||||
value,
|
||||
conditional,
|
||||
} = self;
|
||||
|
||||
if *conditional {
|
||||
tokens.extend(quote! {
|
||||
if ::std::env::var(#key).ok().is_none() {
|
||||
_c.env(#key, #value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
tokens.extend(quote! { _c.env(#key, #value); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CurrentDir(Option<Value>);
|
||||
|
||||
|
||||
@@ -56,6 +56,26 @@ mod pipe;
|
||||
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" TEST="test" "echo" "test""#);
|
||||
/// ```
|
||||
///
|
||||
/// #### Conditional
|
||||
/// You can have a default value set for an environment variable.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use comlexr_macro::cmd;
|
||||
/// const NEW_VAR: &str = "NEW_VAR";
|
||||
/// std::env::set_var("TEST", "realvalue");
|
||||
///
|
||||
/// let command = cmd!(
|
||||
/// env {
|
||||
/// "TEST":? "test",
|
||||
/// NEW_VAR: "new_var"
|
||||
/// };
|
||||
/// "echo",
|
||||
/// "test",
|
||||
/// );
|
||||
///
|
||||
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" "echo" "test""#);
|
||||
/// ```
|
||||
///
|
||||
/// ### Current Directory and Environment Variable Order
|
||||
/// ```
|
||||
/// # use comlexr_macro::cmd;
|
||||
|
||||
33
tests/cmd.rs
33
tests/cmd.rs
@@ -1,6 +1,8 @@
|
||||
extern crate comlexr;
|
||||
extern crate rstest;
|
||||
|
||||
use std::env;
|
||||
|
||||
use comlexr::cmd;
|
||||
use rstest::rstest;
|
||||
|
||||
@@ -35,6 +37,37 @@ fn env_vars() {
|
||||
assert_eq!(format!("{command:?}"), r#"TEST="test" "echo" "test""#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conditional_env_vars() {
|
||||
env::set_var("TEST", "realvalue");
|
||||
env::set_var("TEST2", "won't see");
|
||||
|
||||
let command = cmd!(
|
||||
env {
|
||||
"TEST":? "test",
|
||||
"TEST2": "test2"
|
||||
};
|
||||
"echo",
|
||||
"test",
|
||||
);
|
||||
|
||||
assert_eq!(format!("{command:?}"), r#"TEST2="test2" "echo" "test""#);
|
||||
|
||||
let command = cmd!(
|
||||
env {
|
||||
"TEST": "test",
|
||||
"TEST2": "test2"
|
||||
};
|
||||
"echo",
|
||||
"test",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
format!("{command:?}"),
|
||||
r#"TEST="test" TEST2="test2" "echo" "test""#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cd_env_vars() {
|
||||
let command = cmd!(
|
||||
|
||||
Reference in New Issue
Block a user