#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/ssh-keys/functions"

cmd-ssh-keys-list() {
  declare desc="List ssh key hashes"
  declare cmd="ssh-keys:list"
  [[ "$1" == "$cmd" ]] && shift 1
  declare NAME="$1"
  local FORMAT=text
  if [[ "$NAME" == "--format" ]]; then
    FORMAT="$2"
    NAME="$3"
  fi

  if [[ "$FORMAT" != "text" ]] && [[ "$FORMAT" != "json" ]]; then
    dokku_log_fail "Invalid output format specified, supported formats: json, text"
  fi

  if [[ "$FORMAT" == "text" ]]; then
    FORMAT=""
  fi

  if [[ "$FORMAT" == "json" ]]; then
    if [[ -n "$NAME" ]]; then
      sshcommand list dokku "" "$FORMAT" | jq -cM "[.[] | select(.name == \"$NAME\")]"
    else
      sshcommand list dokku "" "$FORMAT"
    fi
  else
    sshcommand list dokku "$NAME"
  fi
}

cmd-ssh-keys-list "$@"
