github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  // Package gbuild manages the build-in variables from "gf build".
     8  package gbuild
     9  
    10  import (
    11  	"context"
    12  	"runtime"
    13  
    14  	"github.com/wangyougui/gf/v2"
    15  	"github.com/wangyougui/gf/v2/container/gvar"
    16  	"github.com/wangyougui/gf/v2/encoding/gbase64"
    17  	"github.com/wangyougui/gf/v2/internal/intlog"
    18  	"github.com/wangyougui/gf/v2/internal/json"
    19  )
    20  
    21  // BuildInfo maintains the built info of current binary.
    22  type BuildInfo struct {
    23  	GoFrame string                 // Built used GoFrame version.
    24  	Golang  string                 // Built used Golang version.
    25  	Git     string                 // Built used git repo. commit id and datetime.
    26  	Time    string                 // Built datetime.
    27  	Version string                 // Built version.
    28  	Data    map[string]interface{} // All custom built data key-value pairs.
    29  }
    30  
    31  const (
    32  	gfVersion    = `gfVersion`
    33  	goVersion    = `goVersion`
    34  	BuiltGit     = `builtGit`
    35  	BuiltTime    = `builtTime`
    36  	BuiltVersion = `builtVersion`
    37  )
    38  
    39  var (
    40  	builtInVarStr = ""                       // Raw variable base64 string, which is injected by go build flags.
    41  	builtInVarMap = map[string]interface{}{} // Binary custom variable map decoded.
    42  )
    43  
    44  func init() {
    45  	// The `builtInVarStr` is injected by go build flags.
    46  	if builtInVarStr != "" {
    47  		err := json.UnmarshalUseNumber(gbase64.MustDecodeString(builtInVarStr), &builtInVarMap)
    48  		if err != nil {
    49  			intlog.Errorf(context.TODO(), `%+v`, err)
    50  		}
    51  		builtInVarMap[gfVersion] = gf.VERSION
    52  		builtInVarMap[goVersion] = runtime.Version()
    53  		intlog.Printf(context.TODO(), "build variables: %+v", builtInVarMap)
    54  	} else {
    55  		intlog.Print(context.TODO(), "no build variables")
    56  	}
    57  }
    58  
    59  // Info returns the basic built information of the binary as map.
    60  // Note that it should be used with gf-cli tool "gf build",
    61  // which automatically injects necessary information into the binary.
    62  func Info() BuildInfo {
    63  	return BuildInfo{
    64  		GoFrame: Get(gfVersion).String(),
    65  		Golang:  Get(goVersion).String(),
    66  		Git:     Get(BuiltGit).String(),
    67  		Time:    Get(BuiltTime).String(),
    68  		Version: Get(BuiltVersion).String(),
    69  		Data:    Data(),
    70  	}
    71  }
    72  
    73  // Get retrieves and returns the build-in binary variable with given name.
    74  func Get(name string, def ...interface{}) *gvar.Var {
    75  	if v, ok := builtInVarMap[name]; ok {
    76  		return gvar.New(v)
    77  	}
    78  	if len(def) > 0 {
    79  		return gvar.New(def[0])
    80  	}
    81  	return nil
    82  }
    83  
    84  // Data returns the custom build-in variables as map.
    85  func Data() map[string]interface{} {
    86  	return builtInVarMap
    87  }