plugins:
  reconcile-flux:
    scopes: &flux_scopes
      - kustomizations
      - helmreleases
      - gitrepositories
      - ocirepositories
      - helmrepositories
      - imagerepositories
      - imageupdateautomations
    shortCut: f
    background: true
    description: Reconcile flux resource
    command: bash
    args:
      - -c
      - >
        kubectl annotate
        --overwrite
        --kubeconfig "$KUBECONFIG"
        --context "$CONTEXT"
        --namespace "$NAMESPACE"
        ${RESOURCE_NAME}.${RESOURCE_GROUP}
        $NAME
        reconcile.fluxcd.io/requestedAt=$(date -Iseconds)
        && echo "Reconiliation requested"
  toggle-flux:
    scopes: *flux_scopes
    shortCut: s
    background: true
    description: Toggle flux
    command: bash
    args:
      - -c
      - |
        set -eo pipefail
        trap "echo Something failed" ERR
        ku() {
            local verb="$1"
            shift
            kubectl --kubeconfig "$KUBECONFIG" --context "$CONTEXT" --namespace "$NAMESPACE" $verb ${RESOURCE_NAME}.${RESOURCE_GROUP} $NAME "$@"
        }

        S="$(ku get -o jsonpath='{.spec.suspend }')";

        if [[ "$S" == true ]] ; then
            ku patch --type=json -p '[{ "op": "remove", "path": "/spec/suspend"}]';
            echo "$RESOURCE_NAME resumed"
        else
            ku patch --type=json -p '[{ "op": "replace", "path": "/spec/suspend", "value": true}]';
            echo "$RESOURCE_NAME suspended"
        fi

  trace:
    shortCut: Ctrl-T
    confirm: false
    description: Flux trace
    scopes:
      - all
    command: bash
    background: false
    args:
      - -c
      - >
        resource=$(echo $RESOURCE_NAME | sed -E 's/ies$/y/' | sed -E 's/ses$/se/' | sed -E 's/(s|es)$//g') ; flux trace --kubeconfig "$KUBECONFIG" --context $CONTEXT --kind $resource --api-version
        $RESOURCE_GROUP/$RESOURCE_VERSION --namespace $NAMESPACE $NAME | less -K
  stern:
    shortCut: Ctrl-L
    confirm: false
    description: Logs <Stern>
    scopes:
      - pods
    command: stern
    background: false
    args:
      - --kubeconfig
      - $KUBECONFIG
      - --tail
      - 50
      - $FILTER
      - -n
      - $NAMESPACE
      - --context
      - $CONTEXT
  node-shell:
    shortCut: s
    confirm: false
    description: Node Shell
    scopes:
      - nodes
    command: kubectl
    background: false
    args:
      - node-shell
      - --context
      - $CONTEXT
      - $NAME
