
# Made for prepare and postrun
# Selective imports (ADR-025 Phase 3 Layer 2).
# config/accessor and utils/ui star-imports were dead — dropped.
use lib_provisioning/utils/init.nu [get-workspace-path get-provisioning-infra-path]
use lib_provisioning/sops/lib.nu [find-sops-key on_sops]

export def log_debug [
   msg: string
] {
   use std
   std log debug $msg
   # std assert (1 == 1)
}
export def check_env [
] {
   let vars_path = (get-provisioning-vars)
   if ($vars_path | is-empty) {
      _print $"🛑 Error no values found for (_ansi red_bold)PROVISIONING_VARS(_ansi reset)"
      exit 1
   }
   if not ($vars_path | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($vars_path)(_ansi reset) not found"
      exit 1
   }
   let workspace_path = (get-workspace-path)
   if ($workspace_path | is-empty) {
      _print $"🛑 Error no values found for (_ansi red_bold)PROVISIONING_WORKSPACE_PATH(_ansi reset)"
      exit 1
   }
   if not ($workspace_path | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($workspace_path)(_ansi reset) not found"
      exit 1
   }
   let wk_env_path = (get-provisioning-wk-env-path)
   if ($wk_env_path | is-empty) {
      _print $"🛑 Error no values found for (_ansi red_bold)PROVISIONING_WK_ENV_PATH(_ansi reset)"
      exit 1
   }
   if not ($wk_env_path | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($wk_env_path)(_ansi reset) not found"
      exit 1
   }
}

export def sops_cmd [
   task: string
   source: string
   target?: string
   --error_exit   # error on exit
] {
   let sops_key = (find-sops-key)
   if ($sops_key | is-empty) {
     $env.CURRENT_INFRA_PATH = ((get-provisioning-infra-path) | path join (get-workspace-path | path basename))
     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 [
] {
  let vars_path = (get-provisioning-vars)
  if not ($vars_path | path exists) {
      _print $"🛑 Error file (_ansi red_bold)($vars_path)(_ansi reset) not found"
      exit 1
   }
  (open $vars_path)
}
