
# Made for prepare and postrun
use ../lib_provisioning/utils/ui.nu *
use ../lib_provisioning/sops *

export def log_debug [
   msg: string
]: nothing -> nothing {
   use std
   std log debug $msg
   # std assert (1 == 1)
}
export def check_env [
]: nothing -> nothing {
   if $env.PROVISIONING_VARS? == null {
      _print $"🛑 Error no values found for (_ansi red_bold)env.PROVISIONING_VARS(_ansi reset)"
      exit 1
   }
   if not ($env.PROVISIONING_VARS? | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($env.PROVISIONING_VARS)(_ansi reset) not found"
      exit 1
   }
   if $env.PROVISIONING_KLOUD_PATH? == null {
      _print $"🛑 Error no values found for (_ansi red_bold)env.PROVISIONING_KLOUD_PATH(_ansi reset)"
      exit 1
   }
   if not ($env.PROVISIONING_KLOUD_PATH? | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($env.PROVISIONING_KLOUD_PATH)(_ansi reset) not found"
      exit 1
   }
   if $env.PROVISIONING_WK_ENV_PATH? == null {
      _print $"🛑 Error no values found for (_ansi red_bold)env.PROVISIONING_WK_ENV_PATH(_ansi reset)"
      exit 1
   }
   if not ($env.PROVISIONING_WK_ENV_PATH? | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($env.PROVISIONING_WK_ENV_PATH)(_ansi reset) not found"
      exit 1
   }
}

export def sops_cmd [
   task: string
   source: string
   target?: string
   --error_exit   # error on exit
]: nothing -> nothing {
   if $env.PROVISIONING_SOPS? == null  {
     $env.CURRENT_INFRA_PATH = ($env.PROVISIONING_INFRA_PATH | path join $env.PROVISIONING_KLOUD )
     use sops_env.nu
   }
   #use sops/lib.nu on_sops
   if $error_exit {
      on_sops $task $source $target --error_exit
   } else {
      on_sops $task $source $target
   }
}

export def load_defs [
]: nothing -> record {
  if not ($env.PROVISIONING_VARS | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($env.PROVISIONING_VARS)(_ansi reset) not found"
      exit 1
   }
  (open $env.PROVISIONING_VARS)
}
