From 1dce744caeb60728ee231239543274ec63f03e51 Mon Sep 17 00:00:00 2001 From: gitworkflows <118260833+gitworkflows@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:50:34 +0600 Subject: [PATCH] Fix ioutil deprecation (#5) * Update Makefile Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update finder.go Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update classifier.go Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update find.go Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update pipeline.yaml (#3) * Update pipeline.yaml Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update pipeline.yaml Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update Makefile Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update pipeline.yaml Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> --------- Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Gitworkflows patch 1 (#4) * Update pipeline.yaml Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update pipeline.yaml Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update Makefile Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> * Update pipeline.yaml Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> --------- Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> --------- Signed-off-by: gitworkflows <118260833+gitworkflows@users.noreply.github.com> --- .github/workflows/pipeline.yaml | 82 +++++++++++++++++++------------ Makefile | 35 +++++-------- golicenses/finder.go | 4 +- golicenses/licenses/classifier.go | 4 +- golicenses/licenses/find.go | 4 +- 5 files changed, 68 insertions(+), 61 deletions(-) diff --git a/.github/workflows/pipeline.yaml b/.github/workflows/pipeline.yaml index abd9442..67bf8e8 100644 --- a/.github/workflows/pipeline.yaml +++ b/.github/workflows/pipeline.yaml @@ -1,7 +1,9 @@ name: 'app-pipeline' + on: push: pull_request: + jobs: test: strategy: @@ -10,16 +12,17 @@ jobs: platform: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.platform }} steps: + - name: Checkout code + uses: actions/checkout@v2 - - uses: actions/setup-go@v1 + - name: Set up Go + uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} - - uses: actions/checkout@v1 - - - name: Cache go dependencies - id: unit-cache-go-dependencies - uses: actions/cache@v1 + - name: Cache Go dependencies + id: cache-go-dependencies + uses: actions/cache@v2 with: path: | ~/go/pkg/mod @@ -28,11 +31,11 @@ jobs: restore-keys: | ${{ runner.os }}-go-${{ matrix.go-version }}- - - name: Install go dependencies - if: steps.unit-cache-go-dependencies.outputs.cache-hit != 'true' + - name: Install Go dependencies + if: steps.cache-go-dependencies.outputs.cache-hit != 'true' run: make bootstrap - - name: Run Static Analyses + - name: Run Static Analysis run: make lint - name: Run Tests @@ -41,15 +44,17 @@ jobs: build-artifacts: runs-on: ubuntu-latest steps: - - uses: actions/setup-go@v1 + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 with: go-version: '1.18.x' - - uses: actions/checkout@v1 - - - name: Cache go dependencies + - name: Cache Go dependencies id: package-cache-go-dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: | ~/go/pkg/mod @@ -65,53 +70,68 @@ jobs: - name: Build snapshot artifacts run: make ci-build-snapshot-packages - - uses: actions/upload-artifact@master + - name: Upload artifacts + uses: actions/upload-artifact@v3 with: name: artifacts path: dist - test-linux-artifacts: - needs: [ build-artifacts ] + needs: build-artifacts runs-on: ubuntu-latest steps: - - uses: actions/checkout@master - - uses: actions/download-artifact@master + - name: Checkout code + uses: actions/checkout@v2 + + - name: Download artifacts + uses: actions/download-artifact@v3 with: name: artifacts path: dist - - name: Test linux run + + - name: Test Linux run run: make ci-test-linux-run - - name: Test docker run + + - name: Test Docker run run: make ci-plugs-out-test test-mac-artifacts: - needs: [ build-artifacts ] + needs: build-artifacts runs-on: macos-latest steps: - - uses: actions/checkout@master - - uses: actions/download-artifact@master + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.20' # Specify the Go version you need + + - name: Download artifacts + uses: actions/download-artifact@v3 with: name: artifacts path: dist - - name: Test darwin run + + - name: Test Darwin run run: make ci-test-mac-run release: - needs: [ test, build-artifacts, test-linux-artifacts, test-mac-artifacts ] + needs: [test, build-artifacts, test-linux-artifacts, test-mac-artifacts] runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/v') steps: + - name: Checkout code + uses: actions/checkout@v2 - - uses: actions/setup-go@v1 + - name: Set up Go + uses: actions/setup-go@v2 with: go-version: '1.18.x' - - uses: actions/checkout@v1 - - - name: Cache go dependencies + - name: Cache Go dependencies id: release-cache-go-dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: | ~/go/pkg/mod diff --git a/Makefile b/Makefile index c98092b..902b4bf 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,11 @@ TMP = ./.tmp RESULTS = $(TMP)/results ASSETS = assets DBASSET = $(ASSETS)/licenses.db -# note: go tools requires an absolute path BIN = $(abspath $(TMP)/bin) COVER_REPORT = $(RESULTS)/cover.report COVER_TOTAL = $(RESULTS)/cover.total LINTCMD = $(BIN)/golangci-lint run --tests=false --config .golangci.yaml + BOLD := $(shell tput -T linux bold) PURPLE := $(shell tput -T linux setaf 5) GREEN := $(shell tput -T linux setaf 2) @@ -15,7 +15,6 @@ RED := $(shell tput -T linux setaf 1) RESET := $(shell tput -T linux sgr0) TITLE := $(BOLD)$(PURPLE) SUCCESS := $(BOLD)$(GREEN) -# the quality gate lower threshold for unit test total % coverage (by function statements) COVERAGE_THRESHOLD := 34 RELEASE_CMD=$(BIN)/goreleaser --rm-dist @@ -44,25 +43,18 @@ help: bootstrap: ## Download and install all project dependencies (+ prep tooling in the ./.tmp dir) $(call title,Downloading dependencies) - # prep temp dirs - mkdir -p $(TMP) || exit 1 - mkdir -p $(RESULTS) || exit 1 - mkdir -p $(BIN) || exit 1 - # download install project dependencies + tooling + @mkdir -p $(TMP) $(RESULTS) $(BIN) || exit 1 go mod download || exit 1 cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % env GOBIN=$(BIN) go install % || exit 1 - # install golangci-lint - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BIN) v1.47.2 || exit 1 - # install pkger + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BIN) v1.50.1 || exit 1 cd $(TMP) && curl -sLO https://github.com/markbates/pkger/releases/download/v0.17.0/pkger_0.17.0_$(shell uname)_x86_64.tar.gz && \ tar -xzvf pkger_0.17.0_$(shell uname)_x86_64.tar.gz pkger && \ mv pkger $(BIN) || exit 1 - # install goreleaser GOBIN=$(BIN) go install github.com/goreleaser/goreleaser@v1.3.1 || exit 1 $(DBASSET): $(call title,Building assets) - mkdir -p $(ASSETS) || exit 1 + @mkdir -p $(ASSETS) || exit 1 $(BIN)/license_serializer -output $(ASSETS) || exit 1 pkged.go: $(DBASSET) @@ -89,22 +81,19 @@ lint-fix: ## Auto-format all source code + run golangci-lint fixers unit: ## Run unit tests (with coverage) $(call title,Running unit tests) go test -coverprofile $(COVER_REPORT) ./... - @go tool cover -func $(COVER_REPORT) | grep total | awk '{print substr($$3, 1, length($$3)-1)}' > $(COVER_TOTAL) + @go tool cover -func $(COVER_REPORT) | grep total | awk '{print substr($$3, 1, length($$3)-1)}' > $(COVER_TOTAL) @echo "Coverage: $$(cat $(COVER_TOTAL))" - @if [ $$(echo "$$(cat $(COVER_TOTAL)) >= $(COVERAGE_THRESHOLD)" | bc -l) -ne 1 ]; then echo "$(RED)$(BOLD)Failed coverage quality gate (> $(COVERAGE_THRESHOLD)%)$(RESET)" && false; fi - -# The following targets are all CI related + @if [ $$(echo "$$(cat $(COVER_TOTAL)) >= $(COVERAGE_THRESHOLD)" | bc -l) -ne 1 ]; then \ + echo "$(RED)$(BOLD)Failed coverage quality gate (> $(COVERAGE_THRESHOLD)%)$(RESET)" && false; \ + fi ci-build-snapshot-packages: pkged.go - $(RELEASE_CMD) \ - --snapshot \ - --skip-publish + $(RELEASE_CMD) --snapshot --skip-publish -# note: since google's licenseclassifier requires the go tooling ('go list' from x/tools/go/packages) we need to use a golang image ci-plugs-out-test: docker run \ -v //var/run/docker.sock://var/run/docker.sock \ - -v /${PWD}://src \ + -v ${PWD}://src \ -w //src \ golang:latest \ /bin/bash -x -c "\ @@ -137,6 +126,4 @@ ci-release: pkged.go $(BIN)/goreleaser --rm-dist clean: ## Clean build artifacts - rm -rf dist - rm -rf .tmp - rm -rf $(RESULTS) + rm -rf dist .tmp $(RESULTS) diff --git a/golicenses/finder.go b/golicenses/finder.go index b8206e7..5c4dd2c 100644 --- a/golicenses/finder.go +++ b/golicenses/finder.go @@ -4,7 +4,7 @@ import ( "context" "flag" "fmt" - "io/ioutil" + "io" "strings" "github.com/google/licenseclassifier" @@ -35,7 +35,7 @@ func licenseDBArchiveFetcher() ([]byte, error) { } defer f.Close() - return ioutil.ReadAll(f) + return io.ReadAll(f) } func (r LicenseFinder) Find() (<-chan LicenseResult, error) { diff --git a/golicenses/licenses/classifier.go b/golicenses/licenses/classifier.go index cd1bee1..cd37466 100644 --- a/golicenses/licenses/classifier.go +++ b/golicenses/licenses/classifier.go @@ -16,7 +16,7 @@ package licenses import ( "fmt" - "io/ioutil" + "os" "github.com/google/licenseclassifier" ) @@ -88,7 +88,7 @@ func (c *googleClassifier) Identify(licensePath string) (string, Type, error) { if licensePath == "" { return "", Unknown, nil } - content, err := ioutil.ReadFile(licensePath) + content, err := os.ReadFile(licensePath) if err != nil { return "", "", err } diff --git a/golicenses/licenses/find.go b/golicenses/licenses/find.go index ad32af5..973ba4a 100644 --- a/golicenses/licenses/find.go +++ b/golicenses/licenses/find.go @@ -17,7 +17,7 @@ package licenses import ( "fmt" "go/build" - "io/ioutil" + "os" "path/filepath" "regexp" ) @@ -57,7 +57,7 @@ func findUpwards(dir string, r *regexp.Regexp, stopAt []*regexp.Regexp, predicat start := dir // Stop once dir matches a stopAt regexp or dir is the filesystem root for !matchAny(stopAt, dir) { - dirContents, err := ioutil.ReadDir(dir) + dirContents, err := os.ReadDir(dir) if err != nil { return "", err }