1
0
Fork 0
This commit is contained in:
Lukas Wurzinger 2024-12-15 16:48:38 +01:00
parent 4545f636ab
commit 111c621643
3 changed files with 78 additions and 78 deletions

102
get.go
View file

@ -1,6 +1,7 @@
package binfo
import (
"errors"
"fmt"
"runtime/debug"
"strconv"
@ -10,63 +11,66 @@ import (
)
func Get() (Binfo, error) {
o, ok := debug.ReadBuildInfo()
if !ok {
return Binfo{}, errors.New("unable to read build info")
}
var merr *multierror.Error
b := Binfo{}
if o, ok := debug.ReadBuildInfo(); ok {
b.Orig = o
b.Orig = o
b.Module.Version = o.Main.Version
b.Module.Path = o.Main.Path
b.Module.Sum = o.Main.Sum
b.Module.Version = o.Main.Version
b.Module.Path = o.Main.Path
b.Module.Sum = o.Main.Sum
for _, setting := range o.Settings {
switch setting.Key {
case "-buildmode":
b.Build.Mode = setting.Value
case "-compiler":
b.Build.Compiler = setting.Value
case "GOARCH":
b.Build.Arch = setting.Value
case "GOOS":
b.Build.OS = setting.Value
for _, setting := range o.Settings {
switch setting.Key {
case "-buildmode":
b.Build.Mode = setting.Value
case "-compiler":
b.Build.Compiler = setting.Value
case "GOARCH":
b.Build.Arch = setting.Value
case "GOOS":
b.Build.OS = setting.Value
case "CGO_ENABLED":
switch setting.Value {
case "1":
b.CGO.Enabled = true
case "0":
b.CGO.Enabled = false
default:
merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key))
}
case "CGO_CFLAGS":
b.CGO.Flags.C = setting.Value
case "CGO_CPPFLAGS":
b.CGO.Flags.CPP = setting.Value
case "CGO_CXXFLAGS":
b.CGO.Flags.CXX = setting.Value
case "CGO_LDFLAGS":
b.CGO.Flags.LD = setting.Value
case "vcs":
b.VCS.Name = setting.Value
case "vcs.revision":
b.VCS.Revision = setting.Value
case "vcs.time":
v, err := time.Parse(time.RFC3339, setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS time: %w", err))
}
b.VCS.Time = v
case "vcs.modified":
v, err := strconv.ParseBool(setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS modified: %w", err))
}
b.VCS.Modified = v
case "CGO_ENABLED":
switch setting.Value {
case "1":
b.CGO.Enabled = true
case "0":
b.CGO.Enabled = false
default:
merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key))
}
case "CGO_CFLAGS":
b.CGO.Flags.C = setting.Value
case "CGO_CPPFLAGS":
b.CGO.Flags.CPP = setting.Value
case "CGO_CXXFLAGS":
b.CGO.Flags.CXX = setting.Value
case "CGO_LDFLAGS":
b.CGO.Flags.LD = setting.Value
case "vcs":
b.VCS.Name = setting.Value
case "vcs.revision":
b.VCS.Revision = setting.Value
case "vcs.time":
v, err := time.Parse(time.RFC3339, setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS time: %w", err))
}
b.VCS.Time = v
case "vcs.modified":
v, err := strconv.ParseBool(setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS modified: %w", err))
}
b.VCS.Modified = v
}
}