use ../lib_provisioning/config/accessor.nu *

# Manage provisioning servers or instances
export def "main status" [
  target?: string      # server (s) | taskserv (t) | cluster (c)
  name?: string       # Target name in settings
  ...args                 # Args for create command
  --serverpos (-p): int   # Server position in settings
  --check (-c)            # Only check mode no servers will be created
  --wait (-w)             # Wait servers to be created
  --infra (-i): string    # Infra path
  --settings (-s): string # Settings path
  --outfile (-o): string  # Output file
  --debug (-x)            # Use Debug mode
  --xm             # Debug with PROVISIONING_METADATA
  --xc             # Debuc for task and services locally PROVISIONING_DEBUG_CHECK
  --xr             # Debug for remote servers PROVISIONING_DEBUG_REMOTE
  --xld            # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug
  --metadata       # Error with metadata (-xm)
  --notitles       # not tittles
  --out: string    # Print Output format: json, yaml, text (default)
] {
  let str_out = if ($out | is-not-empty) {
    $env.PROVISIONING_OUT = $out
    $env.PROVISIONING_NO_TERMINAL = true
    $"--out ($out)"
  } else {
    ""
  }
  parse_help_command "status" --end
  if $debug { $env.PROVISIONING_DEBUG = true }
  let use_debug = if $debug or (is-debug-enabled) { "-x" } else { "" }
  match ($target | default "") {
    "server"| "servers" | "s" => {
      ^$"((get-provisioning-name))" $use_debug -mod "server" ($env.PROVISIONING_ARGS | str replace $target '') $str_out --yes --notitles
    },
    "taskserv" | "taskservs" | "t" => {
      ^$"((get-provisioning-name))" $use_debug -mod "tasksrv" ($env.PROVISIONING_ARGS | str replace $target '') _out --yes --notitles
    },
    "clusters"| "clusters" | "c" => {
      ^$"((get-provisioning-name))" $use_debug -mod "cluster" ($env.PROVISIONING_ARGS | str replace $target '') $str_out --yes --notitles
    },
    #""  => {
    #    ^$"((get-provisioning-name))" $use_debug "server" ($env.PROVISIONING_ARGS) $str_out
    #},
    _ => {
        invalid_task "status" ($target | default "") --end
    },
  }
}
