From 0029dc8a5beccf15737d6ef48870153d48bc0838 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Mon, 25 Mar 2024 15:02:12 +0100 Subject: [PATCH 1/3] Bump golang to v1.21 Signed-off-by: Volker Theile --- .github/workflows/go.yml | 4 ++-- .golangci.yaml | 11 ++++++----- go.mod | 3 +-- go.sum | 2 -- slice.go | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c344342..952a2af 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -20,11 +20,11 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.19 + go-version: 1.21 - name: lint run: | - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.52.0 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.57.1 ./bin/golangci-lint run - name: Build diff --git a/.golangci.yaml b/.golangci.yaml index 82fbfe3..153b2dc 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -7,13 +7,14 @@ linters: - revive - gosec - prealloc -run: - skip-files: +issues: + exclude-files: - /zz_generated_ - _generated - skip-dirs: + exclude-dirs: - generated - deadline: 5m +run: + timeout: 5m tests: true build-tags: - - test \ No newline at end of file + - test diff --git a/go.mod b/go.mod index b3ada28..bc9fe71 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/harvester/go-common -go 1.20 +go 1.21 require ( github.com/coreos/go-systemd/v22 v22.5.0 @@ -8,7 +8,6 @@ require ( github.com/godbus/dbus/v5 v5.0.4 github.com/sirupsen/logrus v1.9.2 github.com/stretchr/testify v1.7.0 - golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 1d758d3..aa265da 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,6 @@ github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8= -golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/slice.go b/slice.go index 8e734cf..a9df4c1 100644 --- a/slice.go +++ b/slice.go @@ -1,6 +1,6 @@ package gocommon -import "golang.org/x/exp/slices" +import "slices" // SliceContentCmp compares two slices and returns true if they have the same content with any order. func SliceContentCmp[T comparable](x, y []T) bool { From ec43f3796fea8416f04cd146a52ce5750b4dc7d6 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Tue, 26 Mar 2024 09:54:54 +0100 Subject: [PATCH 2/3] Add scripts to validate and test code locally Signed-off-by: Volker Theile --- .gitignore | 1 + Dockerfile.dapper | 21 +++++++++++++++++++++ Makefile | 15 +++++++++++++++ scripts/entry | 11 +++++++++++ scripts/test | 7 +++++++ scripts/validate | 21 +++++++++++++++++++++ scripts/version | 27 +++++++++++++++++++++++++++ 7 files changed, 103 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile.dapper create mode 100644 Makefile create mode 100755 scripts/entry create mode 100755 scripts/test create mode 100755 scripts/validate create mode 100755 scripts/version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d80c3d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.dapper diff --git a/Dockerfile.dapper b/Dockerfile.dapper new file mode 100644 index 0000000..09b1799 --- /dev/null +++ b/Dockerfile.dapper @@ -0,0 +1,21 @@ +FROM registry.suse.com/bci/golang:1.21 + +ARG DAPPER_HOST_ARCH +ENV HOST_ARCH=${DAPPER_HOST_ARCH} ARCH=${DAPPER_HOST_ARCH} + +RUN zypper -n rm container-suseconnect && \ + zypper -n install git curl docker gzip tar wget awk + +## install golangci +RUN if [ "${ARCH}" == "amd64" ]; then \ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.57.1; \ + fi + +ENV DAPPER_ENV REPO TAG DRONE_TAG +ENV DAPPER_SOURCE /go/src/github.com/harvester/go-common/ +ENV DAPPER_DOCKER_SOCKET true +ENV HOME ${DAPPER_SOURCE} +WORKDIR ${DAPPER_SOURCE} + +ENTRYPOINT ["./scripts/entry"] +CMD ["ci"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5d7b47e --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +TARGETS := $(shell ls scripts) + +.dapper: + @echo Downloading dapper + @curl -sL https://releases.rancher.com/dapper/latest/dapper-$$(uname -s)-$$(uname -m) > .dapper.tmp + @@chmod +x .dapper.tmp + @./.dapper.tmp -v + @mv .dapper.tmp .dapper + +$(TARGETS): .dapper + ./.dapper $@ + +.DEFAULT_GOAL := default + +.PHONY: $(TARGETS) diff --git a/scripts/entry b/scripts/entry new file mode 100755 index 0000000..78fb567 --- /dev/null +++ b/scripts/entry @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +mkdir -p bin dist +if [ -e ./scripts/$1 ]; then + ./scripts/"$@" +else + exec "$@" +fi + +chown -R $DAPPER_UID:$DAPPER_GID . diff --git a/scripts/test b/scripts/test new file mode 100755 index 0000000..0866243 --- /dev/null +++ b/scripts/test @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +cd $(dirname $0)/.. + +echo Running tests +go test -cover -tags=test ./... diff --git a/scripts/validate b/scripts/validate new file mode 100755 index 0000000..7f98128 --- /dev/null +++ b/scripts/validate @@ -0,0 +1,21 @@ +#!/bin/bash +set -e + +cd $(dirname $0)/.. + +echo Running validation + +PACKAGES="$(go list ./...)" + +if ! command -v golangci-lint; then + echo Skipping validation: no golangci-lint available + exit +fi + +echo Running validation + +echo Running: golangci-lint +golangci-lint run + +echo Running: go fmt +test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)" diff --git a/scripts/version b/scripts/version new file mode 100755 index 0000000..48d9c23 --- /dev/null +++ b/scripts/version @@ -0,0 +1,27 @@ +#!/bin/bash + +if [ -n "$(git status --porcelain --untracked-files=no)" ]; then + DIRTY="-dirty" +fi + +COMMIT=$(git rev-parse --short HEAD) +GIT_TAG=${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)} + +if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then + VERSION=$GIT_TAG +else + VERSION="${COMMIT}${DIRTY}" +fi + +if [ -z "$ARCH" ]; then + ARCH=$(go env GOHOSTARCH) +fi + +SUFFIX="-${ARCH}" + +TAG=${TAG:-${VERSION}${SUFFIX}} +REPO=${REPO:-harvester} + +if echo $TAG | grep -q dirty; then + TAG=dev +fi From 98f21ce12a888622007af99c7b956a1bafb0442d Mon Sep 17 00:00:00 2001 From: Vicente Cheng Date: Tue, 9 Apr 2024 11:23:18 +0800 Subject: [PATCH 3/3] scripts: remove version - we should not need this for lint/test Signed-off-by: Vicente Cheng --- scripts/version | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100755 scripts/version diff --git a/scripts/version b/scripts/version deleted file mode 100755 index 48d9c23..0000000 --- a/scripts/version +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -if [ -n "$(git status --porcelain --untracked-files=no)" ]; then - DIRTY="-dirty" -fi - -COMMIT=$(git rev-parse --short HEAD) -GIT_TAG=${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)} - -if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then - VERSION=$GIT_TAG -else - VERSION="${COMMIT}${DIRTY}" -fi - -if [ -z "$ARCH" ]; then - ARCH=$(go env GOHOSTARCH) -fi - -SUFFIX="-${ARCH}" - -TAG=${TAG:-${VERSION}${SUFFIX}} -REPO=${REPO:-harvester} - -if echo $TAG | grep -q dirty; then - TAG=dev -fi