Commit 2044b700 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

refactor(script): retrieve function params directly from env

parent 93705bfe
Loading
Loading
Loading
Loading
+17 −18
Original line number Diff line number Diff line
@@ -372,8 +372,8 @@ stages:
  }

  function tf_init() {
    opts=$1
    extra_opts=$2
    opts=${ENV_INIT_OPTS:-$TF_INIT_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}

    log_info "--- \\e[32minit\\e[0m"
    log_info "--- Positioned environment (can also be used as TF vars):"
@@ -463,7 +463,7 @@ stages:
  }

  function tf_workspace_select() {
    workspace=$1
    workspace=${ENV_WORKSPACE:-$TF_WORKSPACE}

    # select workspace if configured
    if [[ -n "$workspace" ]]
@@ -480,10 +480,10 @@ stages:
  }

  tf_plan() {
    opts=$1
    extra_opts=$2
    tf_plan=$3
    gitlab_report=$4
    opts=${ENV_PLAN_OPTS:-$TF_PLAN_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}
    tf_plan=${ENV_TYPE}.tfplan
    gitlab_report=${ENV_TYPE}-plan.json

    # shellcheck disable=SC2154
    log_info "--- \\e[32mplan\\e[0m"
@@ -512,11 +512,10 @@ stages:
    terraform show --json "$tf_plan" | jq -r '([.resource_changes[]?.change.actions?]|flatten)|{"create":(map(select(.=="create"))|length),"update":(map(select(.=="update"))|length),"delete":(map(select(.=="delete"))|length)}' > "$gitlab_report"
  }

  # $tf_plan might be propagated by dotenv artifact (optional)
  function tf_apply() {
    opts=$1
    extra_opts=$2
    # optional arg
    tf_plan=$3
    opts=${ENV_APPLY_OPTS:-$TF_APPLY_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}

    # shellcheck disable=SC2154
    log_info "--- \\e[32mapply\\e[0m"
@@ -565,8 +564,8 @@ stages:
  }

  function tf_destroy() {
    opts=$1
    extra_opts=$2
    opts=${ENV_DESTROY_OPTS:-$TF_DESTROY_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}

    # shellcheck disable=SC2154
    log_info "--- \\e[32mdestroy\\e[0m"
@@ -631,11 +630,11 @@ stages:

.tf-commands:
  default:
    init: tf_init "${ENV_INIT_OPTS:-$TF_INIT_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}"
    select_workspace: tf_workspace_select "${ENV_WORKSPACE:-$TF_WORKSPACE}"
    plan: tf_plan "${ENV_PLAN_OPTS:-$TF_PLAN_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}" "${ENV_TYPE}.tfplan" "${ENV_TYPE}-plan.json"
    apply: tf_apply "${ENV_APPLY_OPTS:-$TF_APPLY_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}" "$tfplan"
    destroy: tf_destroy "${ENV_DESTROY_OPTS:-$TF_DESTROY_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}"
    init: tf_init
    select_workspace: tf_workspace_select
    plan: tf_plan
    apply: tf_apply
    destroy: tf_destroy
  init:
    - !reference [ .tf-commands, default, init ]
  select_workspace: