
# -> Create infrastructure and services (see TARGETS)
export def "main create" [
  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)
]: nothing -> nothing {
  if ($out | is-not-empty) {
    $env.PROVISIONING_OUT = $out
    $env.PROVISIONING_NO_TERMINAL = true
  }
  parse_help_command "create" --end
  if $debug { $env.PROVISIONING_DEBUG = true }
  let use_debug = if $debug or $env.PROVISIONING_DEBUG { "-x" } else { "" }

  match $target  {
    "server"| "servers" | "s" => {
      ^$"($env.PROVISIONING_NAME)" $use_debug -mod "server" ($env.PROVISIONING_ARGS | str replace $target '') --notitles
    },
    "taskserv" | "taskservs" | "task" | "tasks" | "t" => {
      let ops = ($env.PROVISIONING_ARGS | split row " ")
      let task = ($ops | get -o 0 | default "")
      ^$"($env.PROVISIONING_NAME)" $use_debug -mod "taskserv" $task ($env.PROVISIONING_ARGS | str replace $"($task) ($target)" '') --notitles
    },
    "clusters"| "clusters" | "cl" => {
      ^$"($env.PROVISIONING_NAME)" $use_debug -mod "cluster" ($env.PROVISIONING_ARGS | str replace $target '') --notitles
    },
     _ => {
        invalid_task "create" ($target | default "") --end
        exit
    },
  }
}
