github.com/gogf/gf@v1.16.9/os/gbuild/gbuild.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gbuild manages the build-in variables from "gf build". 8 package gbuild 9 10 import ( 11 "context" 12 "github.com/gogf/gf" 13 "github.com/gogf/gf/container/gvar" 14 "github.com/gogf/gf/encoding/gbase64" 15 "github.com/gogf/gf/internal/intlog" 16 "github.com/gogf/gf/internal/json" 17 "github.com/gogf/gf/util/gconv" 18 "runtime" 19 ) 20 21 var ( 22 builtInVarStr = "" // Raw variable base64 string. 23 builtInVarMap = map[string]interface{}{} // Binary custom variable map decoded. 24 ) 25 26 func init() { 27 if builtInVarStr != "" { 28 err := json.UnmarshalUseNumber(gbase64.MustDecodeString(builtInVarStr), &builtInVarMap) 29 if err != nil { 30 intlog.Error(context.TODO(), err) 31 } 32 builtInVarMap["gfVersion"] = gf.VERSION 33 builtInVarMap["goVersion"] = runtime.Version() 34 intlog.Printf(context.TODO(), "build variables: %+v", builtInVarMap) 35 } else { 36 intlog.Print(context.TODO(), "no build variables") 37 } 38 } 39 40 // Info returns the basic built information of the binary as map. 41 // Note that it should be used with gf-cli tool "gf build", 42 // which injects necessary information into the binary. 43 func Info() map[string]string { 44 return map[string]string{ 45 "gf": GetString("gfVersion"), 46 "go": GetString("goVersion"), 47 "git": GetString("builtGit"), 48 "time": GetString("builtTime"), 49 } 50 } 51 52 // Get retrieves and returns the build-in binary variable with given name. 53 func Get(name string, def ...interface{}) interface{} { 54 if v, ok := builtInVarMap[name]; ok { 55 return v 56 } 57 if len(def) > 0 { 58 return def[0] 59 } 60 return nil 61 } 62 63 // GetVar retrieves and returns the build-in binary variable of given name as gvar.Var. 64 func GetVar(name string, def ...interface{}) *gvar.Var { 65 return gvar.New(Get(name, def...)) 66 } 67 68 // GetString retrieves and returns the build-in binary variable of given name as string. 69 func GetString(name string, def ...interface{}) string { 70 return gconv.String(Get(name, def...)) 71 } 72 73 // Map returns the custom build-in variable map. 74 func Map() map[string]interface{} { 75 return builtInVarMap 76 }