diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..640ea3f --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,39 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + pull_request: + branches: [main] + +jobs: + verify: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.22.3' + + - name: verify source + run: hack/verify.sh + + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.22.3' + + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.58 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 408d5eb..57c760c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,13 +22,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 - - - name: Unshallow - run: git fetch --prune --unshallow + uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: '1.22.3' diff --git a/go.sum b/go.sum index 09b53f7..099f5db 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,7 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5 github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= + github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= diff --git a/hack/verify.sh b/hack/verify.sh new file mode 100755 index 0000000..774eda0 --- /dev/null +++ b/hack/verify.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash + +set -euo pipefail +cd $(dirname $0)/.. + +EXIT_CODE=0 + +errcho() { + echo $@ >&2 +} + +try() { + local title="$1" + shift + + echo "====================================" + echo "$title" + echo "====================================" + echo + + startTime=$(date +%s) + + set +e + $@ + exitCode=$? + set -e + + elapsed=$(($(date +%s) - $startTime)) + + if [[ $exitCode -eq 0 ]]; then + echo -e "\n[${elapsed}s] SUCCESS :)" + else + echo -e "\n[${elapsed}s] FAILED." + EXIT_CODE=1 + fi + + git reset --hard --quiet + git clean --force + + echo +} + +function verify_go_mod_tidy() ( + set -e + + # bad formatting in go.sum is not automatically fixed by go, for some reason + (set -x; rm go.sum; go mod tidy) + + if ! git diff --exit-code; then + echo "::error::Please run go mod tidy." + return 1 + fi + + echo "go.mod is tidy." +) + +function ensure_gimps() { + location=gimps + + if ! [ -x "$(command -v gimps)" ]; then + version=0.6.0 + arch="$(go env GOARCH)" + os="$(go env GOOS)" + url="https://github.com/xrstf/gimps/releases/download/v${version}/gimps_${version}_${os}_${arch}.tar.gz" + location=/tmp/gimps + + errcho "Downloading gimps v$version..." + wget -qO- "$url" | tar xzOf - "gimps_${version}_${os}_${arch}/gimps" > $location + chmod +x $location + fi + + echo "$location" +} + +function verify_go_imports() ( + set -e + + gimps="$(ensure_gimps)" + + (set -x; $gimps .) + + if ! git diff --exit-code; then + echo "::error::Some import statements are not properly grouped. Please run https://github.com/xrstf/gimps or sort them manually." + return 1 + fi + + echo "Go import statements are properly sorted." +) + +function verify_go_build() ( + set -e + + if ! (set -x; make build); then + echo "::error::Code does not compile." + return 1 + fi + + echo "Code compiles." +) + +function verify_go_lint() ( + set -e + + if ! (set -x; golangci-lint run ./...); then + echo "::error::Computer says no." + return 1 + fi + + echo "Code looks sane." +) + +try "go.mod tidy?" verify_go_mod_tidy +try "gimpsed?" verify_go_imports +try "Go code builds?" verify_go_build + +exit $EXIT_CODE diff --git a/main.go b/main.go index 0c58e56..bff54ab 100644 --- a/main.go +++ b/main.go @@ -147,13 +147,13 @@ func main() { } if args[0] == "-" { - watchStdin(rootCtx, log, os.Stdin, printer) + watchStdin(log, os.Stdin, printer) } else { watchKubernetes(rootCtx, log, args, &opt, printer) } } -func watchStdin(ctx context.Context, log logrus.FieldLogger, input io.Reader, printer *diff.Printer) { +func watchStdin(log logrus.FieldLogger, input io.Reader, printer *diff.Printer) { decoder := yamlutil.NewYAMLOrJSONDecoder(input, 1024) for { @@ -213,10 +213,13 @@ func watchKubernetes(ctx context.Context, log logrus.FieldLogger, args []string, if err != nil { log.Fatalf("Unknown resource kind %q: %v", resourceKind, err) } + + //nolint:staticcheck if parsed == nil { log.Fatalf("Unknown resource kind %q", resourceKind) } + //nolint:staticcheck gvk := parsed.GroupVersionKind kinds[gvk.String()] = gvk diff --git a/pkg/maputil/util.go b/pkg/maputil/util.go deleted file mode 100644 index ae9300e..0000000 --- a/pkg/maputil/util.go +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Christoph Mewes -// SPDX-License-Identifier: MIT - -package maputil - -import "k8s.io/apimachinery/pkg/util/json" - -func toJSON(obj interface{}) string { - encoded, err := json.Marshal(obj) - if err != nil { - panic(err) - } - return string(encoded) -}