mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 03:55:54 +00:00
fix(helpers/goversion): get from go.mod
This commit is contained in:
parent
43b8b75d88
commit
551361539e
1 changed files with 36 additions and 7 deletions
43
helpers.go
43
helpers.go
|
@ -15,12 +15,15 @@
|
|||
package gosec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
@ -493,19 +496,45 @@ func RootPath(root string) (string, error) {
|
|||
return filepath.Abs(root)
|
||||
}
|
||||
|
||||
// GoVersion returns parsed version of Go from runtime
|
||||
// GoVersion returns parsed version of Go mod version and fallback to runtime version if not found.
|
||||
func GoVersion() (int, int, int) {
|
||||
return parseGoVersion(runtime.Version())
|
||||
goVersion, err := goModVersion()
|
||||
if err != nil {
|
||||
return parseGoVersion(strings.TrimPrefix(runtime.Version(), "go"))
|
||||
}
|
||||
|
||||
return parseGoVersion(goVersion)
|
||||
}
|
||||
|
||||
type goListOutput struct {
|
||||
GoVersion string `json:"GoVersion"`
|
||||
}
|
||||
|
||||
func goModVersion() (string, error) {
|
||||
cmd := exec.Command("go", "list", "-m", "-json")
|
||||
|
||||
raw, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("command go list: %w: %s", err, string(raw))
|
||||
}
|
||||
|
||||
var v goListOutput
|
||||
err = json.NewDecoder(bytes.NewBuffer(raw)).Decode(&v)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
|
||||
}
|
||||
|
||||
return v.GoVersion, nil
|
||||
}
|
||||
|
||||
// parseGoVersion parses Go version.
|
||||
// example:
|
||||
// - go1.19rc2
|
||||
// - go1.19beta2
|
||||
// - go1.19.4
|
||||
// - go1.19
|
||||
// - 1.19rc2
|
||||
// - 1.19beta2
|
||||
// - 1.19.4
|
||||
// - 1.19
|
||||
func parseGoVersion(version string) (int, int, int) {
|
||||
exp := regexp.MustCompile(`go(\d+).(\d+)(?:.(\d+))?.*`)
|
||||
exp := regexp.MustCompile(`(\d+).(\d+)(?:.(\d+))?.*`)
|
||||
parts := exp.FindStringSubmatch(version)
|
||||
if len(parts) <= 1 {
|
||||
return 0, 0, 0
|
||||
|
|
Loading…
Reference in a new issue