diff --git a/cmd/gf/internal/cmd/cmd_version.go b/cmd/gf/internal/cmd/cmd_version.go index fbaa79a7406..5a44eac4793 100644 --- a/cmd/gf/internal/cmd/cmd_version.go +++ b/cmd/gf/internal/cmd/cmd_version.go @@ -29,8 +29,8 @@ type cVersionOutput struct{} func (c cVersion) Index(ctx context.Context, in cVersionInput) (*cVersionOutput, error) { info := gbuild.Info() - if info["git"] == "" { - info["git"] = "none" + if info.Git == "" { + info.Git = "none" } mlog.Printf(`GoFrame CLI Tool %s, https://goframe.org`, gf.VERSION) gfVersion, err := c.getGFVersionOfCurrentProject() @@ -41,7 +41,7 @@ func (c cVersion) Index(ctx context.Context, in cVersionInput) (*cVersionOutput, } mlog.Printf(`GoFrame Version: %s`, gfVersion) mlog.Printf(`CLI Installed At: %s`, gfile.SelfPath()) - if info["gf"] == "" { + if info.GoFrame == "" { mlog.Print(`Current is a custom installed version, no installation information.`) return nil, nil } @@ -52,7 +52,7 @@ CLI Built Detail: GF Version: %s Git Commit: %s Build Time: %s -`, info["go"], info["gf"], info["git"], info["time"]))) +`, info.Golang, info.GoFrame, info.Git, info.Time))) return nil, nil } diff --git a/os/gbuild/gbuild.go b/os/gbuild/gbuild.go index 91908ab3fad..d78d1906b9d 100644 --- a/os/gbuild/gbuild.go +++ b/os/gbuild/gbuild.go @@ -18,6 +18,22 @@ import ( "github.com/gogf/gf/v2/internal/json" ) +// BuildInfo maintains the built info of current binary. +type BuildInfo struct { + GoFrame string // Built used GoFrame version. + Golang string // Built used Golang version. + Git string // Built used git repo. commit id and datetime. + Time string // Built datetime. + Data map[string]interface{} // All custom built data key-value pairs. +} + +const ( + gfVersion = `gfVersion` + goVersion = `goVersion` + builtGit = `builtGit` + builtTime = `builtTime` +) + var ( builtInVarStr = "" // Raw variable base64 string, which is injected by go build flags. builtInVarMap = map[string]interface{}{} // Binary custom variable map decoded. @@ -30,8 +46,8 @@ func init() { if err != nil { intlog.Errorf(context.TODO(), `%+v`, err) } - builtInVarMap["gfVersion"] = gf.VERSION - builtInVarMap["goVersion"] = runtime.Version() + builtInVarMap[gfVersion] = gf.VERSION + builtInVarMap[goVersion] = runtime.Version() intlog.Printf(context.TODO(), "build variables: %+v", builtInVarMap) } else { intlog.Print(context.TODO(), "no build variables") @@ -41,12 +57,13 @@ func init() { // Info returns the basic built information of the binary as map. // Note that it should be used with gf-cli tool "gf build", // which automatically injects necessary information into the binary. -func Info() map[string]string { - return map[string]string{ - "gf": Get("gfVersion").String(), - "go": Get("goVersion").String(), - "git": Get("builtGit").String(), - "time": Get("builtTime").String(), +func Info() BuildInfo { + return BuildInfo{ + GoFrame: Get(gfVersion).String(), + Golang: Get(goVersion).String(), + Git: Get(builtGit).String(), + Time: Get(builtTime).String(), + Data: Data(), } } @@ -61,7 +78,7 @@ func Get(name string, def ...interface{}) *gvar.Var { return nil } -// Map returns the custom build-in variable map. -func Map() map[string]interface{} { +// Data returns the custom build-in variables as map. +func Data() map[string]interface{} { return builtInVarMap } diff --git a/os/gbuild/gbuild_z_unit_test.go b/os/gbuild/gbuild_z_unit_test.go index 0f8e8e89bed..35d8504d12d 100644 --- a/os/gbuild/gbuild_z_unit_test.go +++ b/os/gbuild/gbuild_z_unit_test.go @@ -7,18 +7,22 @@ package gbuild_test import ( + "testing" + + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gbuild" "github.com/gogf/gf/v2/test/gtest" - "testing" + "github.com/gogf/gf/v2/util/gconv" ) func Test_Info(t *testing.T) { gtest.C(t, func(t *gtest.T) { - t.Assert(gbuild.Info(), map[string]string{ - "gf": "", - "go": "", - "git": "", - "time": "", + t.Assert(gconv.Map(gbuild.Info()), g.Map{ + "GoFrame": "", + "Golang": "", + "Git": "", + "Time": "", + "Data": g.Map{}, }) }) } @@ -34,6 +38,6 @@ func Test_Get(t *testing.T) { func Test_Map(t *testing.T) { gtest.C(t, func(t *gtest.T) { - t.Assert(gbuild.Map(), map[string]interface{}{}) + t.Assert(gbuild.Data(), map[string]interface{}{}) }) } diff --git a/version.go b/version.go index c6740c9c8b7..bca0c3b582e 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package gf -const VERSION = "v2.0.0-rc2" +const VERSION = "v2.0.0-rc3" const AUTHORS = "john<john@goframe.org>"