github.com/Finschia/finschia-sdk@v0.48.1/version/version.go (about) 1 // Package version is a convenience utility that provides SDK 2 // consumers with a ready-to-use version command that 3 // produces apps versioning information based on flags 4 // passed at compile time. 5 // 6 // # Configure the version command 7 // 8 // The version command can be just added to your cobra root command. 9 // At build time, the variables Name, Version, Commit, and BuildTags 10 // can be passed as build flags as shown in the following example: 11 // 12 // go build -X github.com/Finschia/finschia-sdk/version.Name=gaia \ 13 // -X github.com/Finschia/finschia-sdk/version.AppName=gaiad \ 14 // -X github.com/Finschia/finschia-sdk/version.Version=1.0 \ 15 // -X github.com/Finschia/finschia-sdk/version.Commit=f0f7b7dab7e36c20b757cebce0e8f4fc5b95de60 \ 16 // -X "github.com/Finschia/finschia-sdk/version.BuildTags=linux darwin amd64" 17 package version 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "runtime" 23 "runtime/debug" 24 ) 25 26 var ( 27 // Name is application's name 28 Name = "" 29 // AppName is application binary name 30 AppName = "<appd>" 31 // Version is application's version string 32 Version = "" 33 // Commit is commit 34 Commit = "" 35 // BuildTags is build tags 36 BuildTags = "" 37 ) 38 39 func getSDKVersion() string { 40 deps, ok := debug.ReadBuildInfo() 41 if !ok { 42 return "unable to read deps" 43 } 44 var sdkVersion string 45 for _, dep := range deps.Deps { 46 if dep.Path == "github.com/Finschia/finschia-sdk" { 47 sdkVersion = dep.Version 48 } 49 } 50 51 return sdkVersion 52 } 53 54 // Info defines the application version information. 55 type Info struct { 56 Name string `json:"name" yaml:"name"` 57 AppName string `json:"server_name" yaml:"server_name"` 58 Version string `json:"version" yaml:"version"` 59 GitCommit string `json:"commit" yaml:"commit"` 60 BuildTags string `json:"build_tags" yaml:"build_tags"` 61 GoVersion string `json:"go" yaml:"go"` 62 BuildDeps []buildDep `json:"build_deps" yaml:"build_deps"` 63 LbmSdkVersion string `json:"lbm_sdk_version" yaml:"lbm_sdk_version"` 64 } 65 66 func NewInfo() Info { 67 sdkVersion := getSDKVersion() 68 return Info{ 69 Name: Name, 70 AppName: AppName, 71 Version: Version, 72 GitCommit: Commit, 73 BuildTags: BuildTags, 74 GoVersion: fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH), 75 BuildDeps: depsFromBuildInfo(), 76 LbmSdkVersion: sdkVersion, 77 } 78 } 79 80 func (vi Info) String() string { 81 return fmt.Sprintf(`%s: %s 82 git commit: %s 83 build tags: %s 84 %s`, 85 vi.Name, vi.Version, vi.GitCommit, vi.BuildTags, vi.GoVersion, 86 ) 87 } 88 89 func depsFromBuildInfo() (deps []buildDep) { 90 buildInfo, ok := debug.ReadBuildInfo() 91 if !ok { 92 return nil 93 } 94 95 for _, dep := range buildInfo.Deps { 96 deps = append(deps, buildDep{dep}) 97 } 98 99 return 100 } 101 102 type buildDep struct { 103 *debug.Module 104 } 105 106 func (d buildDep) String() string { 107 if d.Replace != nil { 108 return fmt.Sprintf("%s@%s => %s@%s", d.Path, d.Version, d.Replace.Path, d.Replace.Version) 109 } 110 111 return fmt.Sprintf("%s@%s", d.Path, d.Version) 112 } 113 114 func (d buildDep) MarshalJSON() ([]byte, error) { return json.Marshal(d.String()) } 115 func (d buildDep) MarshalYAML() (interface{}, error) { return d.String(), nil }