github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/version/version.go (about) 1 package version 2 3 import ( 4 "runtime/debug" 5 "strings" 6 ) 7 8 // Default is the default version value used when none was found. 9 const Default = "dev" 10 11 // version holds the current version from the go.mod of downstream users or set by ldflag for wazero CLI. 12 var version string 13 14 // GetWazeroVersion returns the current version of wazero either in the go.mod or set by ldflag for wazero CLI. 15 // 16 // If this is not CLI, this assumes that downstream users of wazero imports wazero as "github.com/tetratelabs/wazero". 17 // To be precise, the returned string matches the require statement there. 18 // For example, if the go.mod has "require github.com/tetratelabs/wazero 0.1.2-12314124-abcd", 19 // then this returns "0.1.2-12314124-abcd". 20 // 21 // Note: this is tested in ./testdata/main_test.go with a separate go.mod to pretend as the wazero user. 22 func GetWazeroVersion() (ret string) { 23 if len(version) != 0 { 24 return version 25 } 26 27 info, ok := debug.ReadBuildInfo() 28 if ok { 29 for _, dep := range info.Deps { 30 // Note: here's the assumption that wazero is imported as github.com/tetratelabs/wazero. 31 if strings.Contains(dep.Path, "github.com/tetratelabs/wazero") { 32 ret = dep.Version 33 } 34 } 35 36 // In wazero CLI, wazero is a main module, so we have to get the version info from info.Main. 37 if versionMissing(ret) { 38 ret = info.Main.Version 39 } 40 } 41 if versionMissing(ret) { 42 return Default // don't return parens 43 } 44 45 // Cache for the subsequent calls. 46 version = ret 47 return ret 48 } 49 50 func versionMissing(ret string) bool { 51 return ret == "" || ret == "(devel)" // pkg.go defaults to (devel) 52 }