Skip to content

Commit

Permalink
Update golangci-lint in Makefile
Browse files Browse the repository at this point in the history
- Partially resolvess go-gitea#17596
- Download specific version(v1.43.0) by default.
- If current installed version is older than the minium version, it will
download the mininium required version.
- Update the install script to avoid deprecated error
`golangci/golangci-lint err this script is deprecated, please do not use
it anymore. check https://github.com/goreleaser/godownloader/issues/207`
  • Loading branch information
Gusted committed Nov 10, 2021
1 parent 43bbc54 commit e6b2794
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ COMMA := ,
XGO_VERSION := go-1.17.x
MIN_GO_VERSION := 001016000
MIN_NODE_VERSION := 012017000
MIN_GOLANGCI_LINT_VERSION = 1.43.0

DOCKER_IMAGE ?= gitea/gitea
DOCKER_TAG ?= latest
Expand Down Expand Up @@ -767,8 +768,13 @@ pr\#%: clean-all
.PHONY: golangci-lint
golangci-lint:
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
echo "Downloading golangci-lint"; \
export BINARY="golangci-lint"; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.37.0; \
curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \
elif $(GO) run contrib/golangci-lint/check-version.go $(MIN_GOLANGCI_LINT_VERSION) > /dev/null 2>&1; then \
echo "Downloading newer version of golangci-lint"; \
export BINARY="golangci-lint"; \
curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \
fi
golangci-lint run --timeout 10m

Expand Down
123 changes: 123 additions & 0 deletions contrib/golangci-lint/check-version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package main

import (
"log"
"os"
"os/exec"
"strconv"
"strings"
)

// Expect that golangci-lint is installed.
// Get the current version of golangci-lint and compare it with the minium
// version. Exit -1 if it's equal or higher than the minium version.
// exit 0 if it's lower than the minium version.

// validVersion, checks if the version only contains dots and digits.
// also that it doesn't contain 2 dots after each other. Also cannot
// end with a dot.
func validVersion(version string) bool {
if version == "" {
return false
}

wasPreviousDot := false
for _, cRune := range version {
switch cRune {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
wasPreviousDot = false
case '.':
if wasPreviousDot {
// Cannot have `..` within a version.
return false
}
wasPreviousDot = true
default:
return false
}
}

// Simplified from if wasPreviousDot { return false } else { return true }
return !wasPreviousDot
}

// compareVersions compares 2 given versions.
// It will return true if the second version is equal or higher than
// the first version. It will return false if the second version is
// lower than the first version.
func compareVersions(firstVersion, secondVersion string) bool {
firstVersionDigits := strings.Split(firstVersion, ".")
secondVersionDigits := strings.Split(secondVersion, ".")

lenSecondVersionDigits := len(secondVersionDigits) - 1

for idx := range firstVersionDigits {
if idx > lenSecondVersionDigits {
return false
}

firstNumber, _ := strconv.Atoi(firstVersionDigits[idx])
secondNumber, _ := strconv.Atoi(secondVersionDigits[idx])
if firstNumber != secondNumber {
return firstNumber >= secondNumber
}
}

return true
}

func main() {
// The minium version should be given by the the first argument.
if len(os.Args) != 2 {
log.Fatal("Incorrect amount of arguments was passed, expected 1 argument")
}

miniumVersion := os.Args[1]
if !validVersion(miniumVersion) {
log.Fatal("Given miniumVersion isn't a valid version")
}

// Get the version from golangci-lint
cmd := exec.Command("golangci-lint", "--version")

// Run the command and get the output.
bytesOutput, err := cmd.Output()
if err != nil {
log.Fatalf("Running \"golangci-lint --version\" ran into a error: %v", err)
}
output := string(bytesOutput)

// Extract the version from output.
// Assuming they won't change this in the future
// We will assume the version starts from 27th character.
// We shouldn't assume the length of the version, so get the first
// index of a whitespace after the 27th character.
if len(output) < 28 {
log.Fatalf("Output of \"golangci-lint --version\" hasn't the correct length")
}

whitespaceAfterVersion := strings.Index(output[27:], " ")
if whitespaceAfterVersion == -1 {
log.Fatalf("Couldn't get the whitespace after the version from the output of \"golangci-lint --version\"")
}

// Get the version from the output at the correct indexes.
installedVersion := string(output[27 : 27+whitespaceAfterVersion])

// Check if it's a valid version.
if !validVersion(installedVersion) {
log.Fatal("installedVersion isn't a valid version")
}

// If the installedVersion is higher or equal to miniumVersion
// than it's all fine and thus we will exit with a 1 code.
// Such that the code will only exit with a 0 code when the
// installedVerion is lower than miniumVersion.
if compareVersions(miniumVersion, installedVersion) {
os.Exit(-1)
}
}

0 comments on commit e6b2794

Please sign in to comment.