[tools]
go = "latest"
"go:golang.org/x/vuln/cmd/govulncheck" = "latest"
"go:mvdan.cc/gofumpt" = "latest"
golangci-lint = "latest"

[tasks.build]
run = "go build -o keld ."

[tasks.install]
run = "go install ."

[tasks.test]
run = "go test -v ./..."

[tasks.fmt]
run = "gofumpt -w ."

[tasks."fmt:check"]
run = """
output=$(gofumpt -d .)
if [ -n "$output" ]; then
  echo "$output"
  echo "Files unformatted; execute 'mise run fmt'"
  exit 1
fi
"""

[tasks.fix]
run = "jj --config 'fix.tools.gofumpt.command=[\"gofumpt\"]' --config 'fix.tools.gofumpt.patterns=[\"glob:**/*.go\"]' fix"

[tasks.lint]
run = "golangci-lint run"

[tasks.vuln]
run = "govulncheck ./..."

[tasks.vet]
run = "go vet ./..."

[tasks."test:quiet"]
run = """
output=$(go test ./... 2>&1)
if [ $? -eq 0 ]; then
  echo "✓"
else
  echo "$output"
  exit 1
fi
"""

[tasks.sync]
run = "go generate ./internal/restic/..."

[tasks."sync:check"]
run = """
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
cp internal/restic/commands_gen.go "$tmpdir/before.go" 2>/dev/null || true
go generate ./internal/restic/...
if ! diff -q internal/restic/commands_gen.go "$tmpdir/before.go" >/dev/null 2>&1; then
  diff -u "$tmpdir/before.go" internal/restic/commands_gen.go || true
  echo "restic commands out of sync; run 'mise run sync'"
  exit 1
fi
"""

[tasks.check]
depends = ["fmt", "vet", "lint", "vuln", "build", "test:quiet"]
