github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/modx/version.go (about) 1 package modx 2 3 import ( 4 "github.com/pkg/errors" 5 "golang.org/x/mod/modfile" 6 ) 7 8 // FindVersion returns the version for a module given the contents of a go.mod file. 9 func FindVersion(gomod []byte, module string) (string, error) { 10 m, err := modfile.Parse("go.mod", gomod, nil) 11 if err != nil { 12 return "", err 13 } 14 15 for _, r := range m.Require { 16 if r.Mod.Path == module { 17 return r.Mod.Version, nil 18 } 19 } 20 21 return "", errors.Errorf("no go.mod entry found for: %s", module) 22 } 23 24 // MustFindVersion returns the version for a module given the contents of a go.mod file or panics. 25 func MustFindVersion(gomod []byte, module string) string { 26 v, err := FindVersion(gomod, module) 27 if err != nil { 28 panic(err) 29 } 30 return v 31 }