mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-02 14:46:31 +00:00
82fd65665f
bidichk checks for dangerous unicode character sequences (https://github.com/golangci/golangci-lint/pull/2330)
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package ruleguard
|
|
|
|
import (
|
|
"fmt"
|
|
"go/token"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type GoVersion struct {
|
|
Major int
|
|
Minor int
|
|
}
|
|
|
|
func (ver GoVersion) IsAny() bool { return ver.Major == 0 }
|
|
|
|
func ParseGoVersion(version string) (GoVersion, error) {
|
|
var result GoVersion
|
|
if version == "" {
|
|
return GoVersion{}, nil
|
|
}
|
|
parts := strings.Split(version, ".")
|
|
if len(parts) != 2 {
|
|
return result, fmt.Errorf("invalid format: %s", version)
|
|
}
|
|
major, err := strconv.Atoi(parts[0])
|
|
if err != nil {
|
|
return result, fmt.Errorf("invalid major version part: %s: %s", parts[0], err)
|
|
}
|
|
minor, err := strconv.Atoi(parts[1])
|
|
if err != nil {
|
|
return result, fmt.Errorf("invalid minor version part: %s: %s", parts[1], err)
|
|
}
|
|
result.Major = major
|
|
result.Minor = minor
|
|
return result, nil
|
|
}
|
|
|
|
func versionCompare(x GoVersion, op token.Token, y GoVersion) bool {
|
|
switch op {
|
|
case token.EQL: // ==
|
|
return x.Major == y.Major && x.Minor == y.Minor
|
|
case token.NEQ: // !=
|
|
return !versionCompare(x, token.EQL, y)
|
|
|
|
case token.GTR: // >
|
|
return x.Major > y.Major || (x.Major == y.Major && x.Minor > y.Minor)
|
|
case token.GEQ: // >=
|
|
return x.Major > y.Major || (x.Major == y.Major && x.Minor >= y.Minor)
|
|
case token.LSS: // <
|
|
return x.Major < y.Major || (x.Major == y.Major && x.Minor < y.Minor)
|
|
case token.LEQ: // <=
|
|
return x.Major < y.Major || (x.Major == y.Major && x.Minor <= y.Minor)
|
|
|
|
default:
|
|
panic("unexpected version compare op")
|
|
}
|
|
}
|