package debug
import (
"strings"
)
func modinfo () string
func ReadBuildInfo () (info *BuildInfo , ok bool ) {
return readBuildInfo (modinfo ())
}
type BuildInfo struct {
Path string
Main Module
Deps []*Module
}
type Module struct {
Path string
Version string
Sum string
Replace *Module
}
func readBuildInfo (data string ) (*BuildInfo , bool ) {
if len (data ) < 32 {
return nil , false
}
data = data [16 : len (data )-16 ]
const (
pathLine = "path\t"
modLine = "mod\t"
depLine = "dep\t"
repLine = "=>\t"
)
readEntryFirstLine := func (elem []string ) (Module , bool ) {
if len (elem ) != 2 && len (elem ) != 3 {
return Module {}, false
}
sum := ""
if len (elem ) == 3 {
sum = elem [2 ]
}
return Module {
Path : elem [0 ],
Version : elem [1 ],
Sum : sum ,
}, true
}
var (
info = &BuildInfo {}
last *Module
line string
ok bool
)
for len (data ) > 0 {
i := strings .IndexByte (data , '\n' )
if i < 0 {
break
}
line , data = data [:i ], data [i +1 :]
switch {
case strings .HasPrefix (line , pathLine ):
elem := line [len (pathLine ):]
info .Path = elem
case strings .HasPrefix (line , modLine ):
elem := strings .Split (line [len (modLine ):], "\t" )
last = &info .Main
*last , ok = readEntryFirstLine (elem )
if !ok {
return nil , false
}
case strings .HasPrefix (line , depLine ):
elem := strings .Split (line [len (depLine ):], "\t" )
last = new (Module )
info .Deps = append (info .Deps , last )
*last , ok = readEntryFirstLine (elem )
if !ok {
return nil , false
}
case strings .HasPrefix (line , repLine ):
elem := strings .Split (line [len (repLine ):], "\t" )
if len (elem ) != 3 {
return nil , false
}
if last == nil {
return nil , false
}
last .Replace = &Module {
Path : elem [0 ],
Version : elem [1 ],
Sum : elem [2 ],
}
last = nil
}
}
return info , true
}
The pages are generated with Golds v0.3.6 . (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .