maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=flux
pkgver=2.5.1
pkgrel=3
pkgdesc="Open and extensible continuous delivery solution for Kubernetes"
url="https://fluxcd.io/"
# blocked by kubernetes
arch="all !armhf"
license="Apache-2.0"
makedepends="go bash kustomize"
checkdepends="etcd kube-apiserver"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/fluxcd/flux2/archive/v$pkgver/flux-$pkgver.tar.gz"
builddir="$srcdir/flux2-$pkgver"
options="net" # download Go modules

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

# secfixes:
#   0.36.0-r0:
#     - CVE-2022-39272

build() {
	bash ./manifests/scripts/bundle.sh

	go build -v \
		-ldflags "-X main.VERSION=$pkgver" \
		./cmd/flux

	for shell in bash fish zsh; do
		./$pkgname completion $shell > $pkgname.$shell
	done
}

check() {
	case "$CARCH" in
		# etcd defines only these architectures as supported
		# Ref: https://github.com/etcd-io/etcd/blob/ac3d5d77ea5fdbc12ef07a6f6fe1722f06d75b24/server/etcdmain/etcd.go#L264
		x86_64|aarch64|ppc64le|s390x) ;;
		*)
			# shellcheck disable=2155
			export ETCD_UNSUPPORTED_ARCH="$(go env GOARCH)" ;;
	esac

	# e2e tests are slow, and require an actual Kubernetes cluster
	KUBEBUILDER_ASSETS=/usr/bin \
		KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT=true \
		go test -tags=unit ./...
}

package() {
	install -Dm755 $pkgname -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
}

sha512sums="
7217338ccafe3673b739ddf978279ef9ab3e355b63d68f77c2158899288f2ef65d0b3791eba9256e3365b87bba340629b8838d278e966aa921043583ca920ca2  flux-2.5.1.tar.gz
"
