Skip to content

Commit

Permalink
Merge pull request #1187 from ayodejiige/add-env-not-condition
Browse files Browse the repository at this point in the history
New env_not condition
  • Loading branch information
sagiegurari authored Jan 18, 2025
2 parents 6df7ae9 + 8d8bf79 commit 1e0969c
Show file tree
Hide file tree
Showing 10 changed files with 721 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,7 @@ The following condition types are available:
* **env_false** - List of environment variables that must be defined and set to any of the following (case insensitive): false, no, 0 or empty
* **env** - Map of environment variables that must be defined and equal to the provided values
* **env_contains** - Map of environment variables that must be defined and contain (case insensitive) the provided values
* **env_not** - Map of environment variables that must not be equal to the provided values
* **rust_version** - Optional definition of min, max, and/or specific rust version
* **files_exist** - List of absolute path files to check they exist. Environment substitution is supported so you can define relative paths such as **`${CARGO_MAKE_WORKING_DIRECTORY}/Cargo.toml`**
* **files_not_exist** - List of absolute path files to check they do not exist. Environment substitution is supported so you can define relative paths such as **`${CARGO_MAKE_WORKING_DIRECTORY}/Cargo.toml`**
Expand Down
1 change: 1 addition & 0 deletions docs/_includes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ The following condition types are available:
* **env_true** - List of environment variables that must be defined and must not be set to any of the following (case insensitive): false, no, 0 or empty
* **env_false** - List of environment variables that must be defined and set to any of the following (case insensitive): false, no, 0 or empty
* **env** - Map of environment variables that must be defined and equal to the provided values
* **env_not** - Map of environment variables that must not be equal to the provided values
* **env_contains** - Map of environment variables that must be defined and contain (case insensitive) the provided values
* **rust_version** - Optional definition of min, max, and/or specific rust version
* **files_exist** - List of absolute path files to check they exist. Environment substitution is supported so you can define relative paths such as **`${CARGO_MAKE_WORKING_DIRECTORY}/Cargo.toml`**
Expand Down
38 changes: 31 additions & 7 deletions src/lib/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,30 @@ use indexmap::IndexMap;
use rust_info::types::{RustChannel, RustInfo};
use std::path::Path;

/// Enum indicates what kind of env map we are validating. Used in `validate_env_map` function.
enum EnvMapType {
/// Validate map for `env` in `TaskCondition`.
Env,
/// Validate map for `env_contains` in `TaskCondition`.
EnvContains,
/// Validate map for `env_not` in `TaskCondition`.
EnvNot
}

fn validate_env_map(
env: Option<IndexMap<String, String>>,
equal: bool,
env_map_type: EnvMapType,
validate_any: bool,
) -> bool {
match env {
Some(env_vars) => {
let mut found_any = env_vars.is_empty();

for (key, current_value) in env_vars.iter() {
let valid = if equal {
envmnt::is_equal(key, current_value)
} else {
envmnt::contains_ignore_case(key, current_value)
let valid = match env_map_type {
EnvMapType::Env => envmnt::is_equal(key, current_value),
EnvMapType::EnvContains => envmnt::contains_ignore_case(key, current_value),
EnvMapType::EnvNot => !envmnt::is_equal(key, current_value),
};

if valid {
Expand All @@ -57,13 +67,18 @@ fn validate_env_map(
}

fn validate_env(condition: &TaskCondition, validate_any: bool) -> bool {
validate_env_map(condition.env.clone(), true, validate_any)
validate_env_map(condition.env.clone(), EnvMapType::Env, validate_any)
}

fn validate_env_not(condition: &TaskCondition, validate_any: bool) -> bool {
validate_env_map(condition.env_not.clone(), EnvMapType::EnvNot, validate_any)
}

fn validate_env_contains(condition: &TaskCondition, validate_any: bool) -> bool {
validate_env_map(condition.env_contains.clone(), false, validate_any)
validate_env_map(condition.env_contains.clone(), EnvMapType::EnvContains, validate_any)
}


fn validate_env_set(condition: &TaskCondition, validate_any: bool) -> bool {
let env = condition.env_set.clone();
match env {
Expand Down Expand Up @@ -505,6 +520,15 @@ fn validate_criteria(flow_info: Option<&FlowInfo>, condition: &Option<TaskCondit
} else if group_or_condition && !valid {
not_valid_found = true;
}

valid = validate_env_not(&condition_struct, validate_any);
if group_or_condition && valid && condition_struct.env_not.is_some() {
return true;
} else if !group_or_condition && !valid {
return false;
} else if group_or_condition && !valid {
not_valid_found = true;
}

valid = validate_env_set(&condition_struct, validate_any);
if group_or_condition && valid && condition_struct.env_set.is_some() {
Expand Down
Loading

0 comments on commit 1e0969c

Please sign in to comment.