maintainer="Michał Polański <michal@polanski.me>"
pkgname=cmctl
pkgver=2.1.1
pkgrel=5
pkgdesc="Command-line tool for interacting with cert-manager"
url="https://cert-manager.io/"
license="Apache-2.0"
arch="all"
makedepends="go"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	kubectl-cert-manager:kubectl_plugin:noarch
	"
source="https://github.com/cert-manager/cmctl/archive/v$pkgver/cmctl-$pkgver.tar.gz"
options="net" # download Go modules

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	go build \
		-ldflags "-X github.com/cert-manager/cert-manager/pkg/util.AppVersion=v$pkgver"

	./cmctl completion bash >$pkgname.bash
	./cmctl completion fish >$pkgname.fish
	./cmctl completion zsh >$pkgname.zsh
}

check() {
	# run unit tests
	go test ./cmd/... ./internal/... ./pkg/...
}

package() {
	install -Dm755 cmctl -t "$pkgdir"/usr/bin/

	install -Dm644 $pkgname.bash "$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish "$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh "$pkgdir"/usr/share/zsh/site-functions/_$pkgname
}

kubectl_plugin() {
	pkgdesc="Use cmctl as a kubectl plugin"
	depends="kubectl"

	mkdir -p "$subpkgdir"/usr/bin
	ln -s cmctl "$subpkgdir"/usr/bin/kubectl-cert_manager

	install -Dm755 /dev/stdin "$subpkgdir"/usr/bin/kubectl_complete-cert_manager <<-'EOF'
		#!/bin/sh
		kubectl cert-manager __complete "$@"
	EOF
}

sha512sums="
f305a808eb4953506c648031ac3dc48190d6728e2bf4a42c703487f6703d5ed8f1b1f2d625fc957047b20a8c8c7eefe5279390ff28e362bad2ca4b013010117c  cmctl-2.1.1.tar.gz
"
