|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// Expect that golangci-lint is installed. |
| 16 | +// Get the current version of golangci-lint and compare it with the minium |
| 17 | +// version. Exit -1 if it's equal or higher than the minium version. |
| 18 | +// exit 0 if it's lower than the minium version. |
| 19 | + |
| 20 | +// validVersion, checks if the version only contains dots and digits. |
| 21 | +// also that it doesn't contain 2 dots after each other. Also cannot |
| 22 | +// end with a dot. |
| 23 | +func validVersion(version string) bool { |
| 24 | + if version == "" { |
| 25 | + return false |
| 26 | + } |
| 27 | + |
| 28 | + wasPreviousDot := false |
| 29 | + for _, cRune := range version { |
| 30 | + switch cRune { |
| 31 | + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 32 | + wasPreviousDot = false |
| 33 | + case '.': |
| 34 | + if wasPreviousDot { |
| 35 | + // Cannot have `..` within a version. |
| 36 | + return false |
| 37 | + } |
| 38 | + wasPreviousDot = true |
| 39 | + default: |
| 40 | + return false |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Simplified from if wasPreviousDot { return false } else { return true } |
| 45 | + return !wasPreviousDot |
| 46 | +} |
| 47 | + |
| 48 | +// compareVersions compares 2 given versions. |
| 49 | +// It will return true if the second version is equal or higher than |
| 50 | +// the first version. It will return false if the second version is |
| 51 | +// lower than the first version. |
| 52 | +func compareVersions(firstVersion, secondVersion string) bool { |
| 53 | + firstVersionDigits := strings.Split(firstVersion, ".") |
| 54 | + secondVersionDigits := strings.Split(secondVersion, ".") |
| 55 | + |
| 56 | + lenSecondVersionDigits := len(secondVersionDigits) - 1 |
| 57 | + |
| 58 | + for idx := range firstVersionDigits { |
| 59 | + if idx > lenSecondVersionDigits { |
| 60 | + return false |
| 61 | + } |
| 62 | + |
| 63 | + firstNumber, _ := strconv.Atoi(firstVersionDigits[idx]) |
| 64 | + secondNumber, _ := strconv.Atoi(secondVersionDigits[idx]) |
| 65 | + if firstNumber != secondNumber { |
| 66 | + return firstNumber >= secondNumber |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return true |
| 71 | +} |
| 72 | + |
| 73 | +func main() { |
| 74 | + // The minium version should be given by the the first argument. |
| 75 | + if len(os.Args) != 2 { |
| 76 | + log.Fatal("Incorrect amount of arguments was passed, expected 1 argument") |
| 77 | + } |
| 78 | + |
| 79 | + miniumVersion := os.Args[1] |
| 80 | + if !validVersion(miniumVersion) { |
| 81 | + log.Fatal("Given miniumVersion isn't a valid version") |
| 82 | + } |
| 83 | + |
| 84 | + // Get the version from golangci-lint |
| 85 | + cmd := exec.Command("golangci-lint", "--version") |
| 86 | + |
| 87 | + // Run the command and get the output. |
| 88 | + bytesOutput, err := cmd.Output() |
| 89 | + if err != nil { |
| 90 | + log.Fatalf("Running \"golangci-lint --version\" ran into a error: %v", err) |
| 91 | + } |
| 92 | + output := string(bytesOutput) |
| 93 | + |
| 94 | + // Extract the version from output. |
| 95 | + // Assuming they won't change this in the future |
| 96 | + // We will assume the version starts from 27th character. |
| 97 | + // We shouldn't assume the length of the version, so get the first |
| 98 | + // index of a whitespace after the 27th character. |
| 99 | + if len(output) < 28 { |
| 100 | + log.Fatalf("Output of \"golangci-lint --version\" hasn't the correct length") |
| 101 | + } |
| 102 | + |
| 103 | + whitespaceAfterVersion := strings.Index(output[27:], " ") |
| 104 | + if whitespaceAfterVersion == -1 { |
| 105 | + log.Fatalf("Couldn't get the whitespace after the version from the output of \"golangci-lint --version\"") |
| 106 | + } |
| 107 | + |
| 108 | + // Get the version from the output at the correct indexes. |
| 109 | + installedVersion := string(output[27 : 27+whitespaceAfterVersion]) |
| 110 | + |
| 111 | + // Check if it's a valid version. |
| 112 | + if !validVersion(installedVersion) { |
| 113 | + log.Fatal("installedVersion isn't a valid version") |
| 114 | + } |
| 115 | + |
| 116 | + // If the installedVersion is higher or equal to miniumVersion |
| 117 | + // than it's all fine and thus we will exit with a 1 code. |
| 118 | + // Such that the code will only exit with a 0 code when the |
| 119 | + // installedVerion is lower than miniumVersion. |
| 120 | + if compareVersions(miniumVersion, installedVersion) { |
| 121 | + os.Exit(-1) |
| 122 | + } |
| 123 | +} |
0 commit comments