#!/usr/bin/env bash
# nerf-kubectl-rollout-restart -- Trigger a rolling restart of a deployment, daemonset, or statefulset.
# Generated from kubectl manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=remote

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-kubectl-rollout-restart [--namespace|-n <namespace>] <target>

Options:
  --namespace, -n <namespace>
      Namespace
      Must match: ^[a-z0-9-]+$

Arguments:
  <target> (required)
      Resource to restart (e.g. deployment/<name>)
      Must match: ^(deployment|deploy|daemonset|ds|statefulset|sts)/[a-z0-9]([-a-z0-9.]*[a-z0-9])?$

Maps to: kubectl rollout restart <namespace> <target>

Trigger a rolling restart of a deployment, daemonset, or statefulset.
EOF
  exit 1
}

NAMESPACE=""
_NAMESPACE_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --namespace|-n) if [[ -n "${_NAMESPACE_SET}" ]]; then echo "error: --namespace can only be specified once" >&2; exit 1; fi; NAMESPACE="$2"; _NAMESPACE_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_TARGET_SET=""
if [[ $# -gt 0 ]]; then
  TARGET="$1"
  _TARGET_SET=true
  shift
else
  TARGET=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-kubectl-rollout-restart: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

_NERF_PATTERN='^[a-z0-9-]+$'
if [[ -n "${_NAMESPACE_SET}" ]] && ! [[ "${NAMESPACE}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-kubectl-rollout-restart: option --namespace does not match required pattern" >&2
  echo "  value:   \"${NAMESPACE}\"" >&2
  echo "  pattern: ^[a-z0-9-]+$" >&2
  echo "  hint: value must match ^[a-z0-9-]+$" >&2
  exit 1
fi

if [[ -n "${_TARGET_SET}" ]] && [[ "${TARGET}" == -* ]]; then
  echo "error: nerf-kubectl-rollout-restart: <target> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${TARGET}" ]]; then
  echo "error: nerf-kubectl-rollout-restart: missing required argument <target>" >&2
  echo "  hint: provide a value for <target>" >&2
  usage
fi

_NERF_PATTERN='^(deployment|deploy|daemonset|ds|statefulset|sts)/[a-z0-9]([-a-z0-9.]*[a-z0-9])?$'
if [[ -n "${_TARGET_SET}" ]] && ! [[ "${TARGET}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-kubectl-rollout-restart: argument <target> does not match required pattern" >&2
  echo "  value:   \"${TARGET}\"" >&2
  echo "  pattern: ^(deployment|deploy|daemonset|ds|statefulset|sts)/[a-z0-9]([-a-z0-9.]*[a-z0-9])?$" >&2
  echo "  hint: value must match ^(deployment|deploy|daemonset|ds|statefulset|sts)/[a-z0-9]([-a-z0-9.]*[a-z0-9])?$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(kubectl rollout restart ${_NAMESPACE_SET:+"--namespace"} ${_NAMESPACE_SET:+"$NAMESPACE"} "${TARGET}")
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec kubectl rollout restart ${_NAMESPACE_SET:+"--namespace"} ${_NAMESPACE_SET:+"$NAMESPACE"} "${TARGET}"
