#!/bin/bash

set -e

CMD_DOCKER=docker
CMD_NERDCTL=nerdctl
CMD=""
IMG="ghcr.io/imuxin/kubectl-watch:latest"
# IMG="docker.io/slic/kubectl-watch:latest" # docker hub image
KUBECONF="$HOME/.kube/config"
MOUNTKUBECONF="/root/.kube/config"

function cmd_exists() {
	if ! command -v $1 &> /dev/null
	then
		return 1
	fi
	return 0
}


function kubectl_watch() {
	if cmd_exists $CMD_DOCKER
	then
		CMD=$CMD_DOCKER
	elif cmd_exists $CMD_NERDCTL
	then
		CMD=$CMD_NERDCTL
	else
		echo "container tool docker and nerdctl not found, please install one of them first."
		exit -1
	fi
	$CMD run -ti --rm -v $KUBECONF:$MOUNTKUBECONF $IMG $@
}

kubectl_watch $@
