expander icon indicating copy to clipboard operation
expander copied to clipboard

ExpandProperty env. default value.

Open williamroboly opened this issue 8 months ago • 0 comments

I have a proposed patch for the Expander method expandProperty() which currently checks and interpolates for env. variables. I found a need where certain processes require a default value in my yaml files for certain environment variables depending on whether I am working in a remote environment or local environment and whether a particular case has the variable defined or not. That being said, I rolled up a patch that does this:

if (str_starts_with($property_name, "env.")) {
  $env_spec = substr($property_name, 4);
  $parts = explode(':', $env_spec, 2);
  $env_key = $parts[0];
  $default = $parts[1] ?? null;
  
  if (!$data->has($property_name)) {
    $env_value = $_SERVER[$env_key] ?? getenv($env_key) ?: false;
    if ($env_value !== false && $env_value !== null && $env_value !== '') {
      $data->set($property_name, $env_value);
    } elseif ($default !== null) {
      $property_name = 'env.' . $env_key;
      $data->set($property_name, $default);
    }
  }
}

gramash-expander-property-default.patch

If its agreeable, I can create a PR for this and suggest a test as well.

I thought others might find it useful.

williamroboly avatar May 29 '25 02:05 williamroboly