Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions parser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package parser

import (
"os"
"regexp"

"github.com/pkg/errors"
"golang.org/x/mod/modfile"
)

// Go mod 信息
Expand All @@ -25,21 +25,19 @@ func ParseGoModuleInfo(path string) (*GoModuleInfo, error) {
}

func parseGoModContent(content []byte) (*GoModuleInfo, error) {
var info GoModuleInfo
// TODO: simple parser
moduleExp := regexp.MustCompile(`module (.+)`)
goVersionExp := regexp.MustCompile(`go (.+)`)

if matches := moduleExp.FindSubmatch(content); len(matches) == 2 {
info.Name = string(matches[1])
} else {
mf, err := modfile.Parse("go.mod", content, nil)
if err != nil {
return nil, err
}
if mf.Module == nil {
return nil, errors.New("module name not found")
}

if matches := goVersionExp.FindSubmatch(content); len(matches) == 2 {
info.GoVersion = string(matches[1])
} else {
if mf.Go == nil {
return nil, errors.New("go version not found")
}
return &info, nil
info := &GoModuleInfo{
Name: mf.Module.Mod.Path,
GoVersion: mf.Go.Version,
}
return info, nil
}