gbb
Yesterday ran well procedures suddenly out of the problem, check its version number, the machine coldly tell you 👇
$XXX --version XXX version 1.0.12Copy the code
If there is no detailed release record information, I think you must be broken at this time. Because we don’t really know when 1.0.12 was compiled, let alone from which source code, it is difficult to find bugs.
If the machine tells you the same information in the same scenario, is debug much easier? !
$XXX version - version XXX 1.0.12 date: 2016-12-18 T15:37:09 + 08:00 commit: db8b606cfc2b24a24e2e09acac24a52c47b68401Copy the code
If the above scene you also deja vu, then maybe GBB can help you, patience down 👀.
-
Pull source code
$ go get -u github.com/voidint/gbbCopy the code
-
Compile (go Get will compile by default)
$ cd $GOPATH/src/github.com/voidint/gbb && go install Copy the code
- Will be executable files
gbb
Put into the environment variablePATH
- perform
which gbb
Check whether the installation is successful - if
gbb
The same name, then it is recommended to set alias, such asalias gbb=gbb2
.
The basic use
GBB is bootstrap, in other words, the GBB executable binaries installed using the above steps can compile GBB source code. Similar 👇
$ cd $GOPATH/src/github.com/voidint/gbb && gbb --debug ==> go build -ldflags '-X "github.com/voidint/gbb/build.Date=2016-12-17T17:00:04+08:00" -X "github.com/voidint/gbb/build.Commit=db8b606cfc2b24a24e2e09acac24a52c47b68401"' $ ls -l ./gbb -rwxr-xr-x 1 voidint staff 4277032 12 17 17:00 ./gbbCopy the code
You can see that there is already an executable binary file in the current directory. Yes, this./ GBB is the result of compiling source code using the installed GBB.
If it’s a brand new project, how can YOU use GBB instead of go build/install or GB for routine code compilation? It’s easy. Try these steps and you’ll learn in no time.
Since you need to demonstrate how to use it, you need to have a GO project. I’ll take the GBB project as an example.
To start our demo from scratch, delete the gbb.json file in the source directory. The purpose of gbb.json and the meaning of the contents of the file will not be discussed below.
$ rm -f gbb.json
Copy the code
First, what does the GBB tool do? I know it’s a terrible phrase, but until there’s a better one, you’ll have to live with it.
Golang compilation tools such as Go Install /build and GB are packaged so that the version information of the compiled binary file contains information such as compilation time stamp and Git commit.
Second, look at the GBB version information 👇
$GBB version GBB version v0.0.1 date: 2016-12-17 T15:37:09 + 08:00 commit: db8b606cfc2b24a24e2e09acac24a52c47b68401Copy the code
This version information, in addition to regular V0.0.1, also includes the time when the GBB binary was compiled and the last commit number of git, the source code management tool used by the project. Is this version information more friendly than a simple V0.0.1? Rich version information also makes debugging easier because the binary can uniquely correspond to the source code in the repository.
step0
To display the key information of compile time and COMMIT number in the version information, you need to define two variables first (variables do not need to be assigned initial values).
package build
var (
Date string
Commit string
)
Copy the code
Then, format this information in the code where the version number is printed, similar to 👇
Package CMD import (" FMT "" github.com/spf13/cobra" "github.com/voidint/gbb/build") var (/ / Version Version Version = "V0.0.1") var versionCmd = &cobra.com mand{Use: "version", Short: "Print version information", Long: ", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("gbb version %s\n", Version) if build.Date ! = "" { fmt.Printf("date: %s\n", build.Date) } if build.Commit ! = "" { fmt.Printf("commit: %s\n", build.Commit) } }, } func init() { RootCmd.AddCommand(versionCmd) }Copy the code
Execute GBB init in the appropriate place in the project directory to generate the gbb.json file.
What is the right place? The general rule is this:
- If you’re using
go build/install
Tools compile code (gbb init
Filled in during executiontool
The value of the term), so thisThe right placeismain
The directory where the method resides. - If use
gb
Tools compile code, so thisThe right placeThis is the project root directory.
Fill in the information step by step as prompted by GBB init to generate the gbb.json file.
$ gbb init This utility will walk you through creating a gbb.json file. It only covers the most common items, Press ^C at any time to quit. Version: (0.0.1) tool: (go_install) go_build package: (main) github.com/voidint/gbb/build variable: Date value: {{.date}} Do you want to continue?[y/n] y variable: Commit value: {{.gitCommit}} Do you want to continue?[y/n] n About to write to / Users/voidint/cloud/workspace/go/projects/src/github.com/voidint/gbb/gbb.json: {" version ":" 0.0.1 ", "tool" : "go install", "package": "github.com/voidint/gbb/build", "variables": [ { "variable": "Date", "value": "{{.date}}" }, { "variable": "Commit", "value": "{{.gitCommit}}" } ] } Is this ok?[y/n] yCopy the code
For details on gbb.json, see detailed instructions below.
step2
Compile in the directory where the gbb.json file is located (GBB init will be called automatically if there is no gbb.json file).
$ gbb --debug
==> go build -ldflags '-X "github.com/voidint/gbb/build.Date=2016-12-17T22:18:32+08:00" -X "github.com/voidint/gbb/build.Commit=db8b606cfc2b24a24e2e09acac24a52c47b68401"'
Copy the code
After compiling, create an extra compiled binary file in the directory, and then print the version information to see if we have achieved the goal we set.
$. / GBB version GBB version v0.0.1 date: 2016-12-17 T22: oh + 08:00 commit: db8b606cfc2b24a24e2e09acac24a52c47b68401Copy the code
😊
Gbb. json can be thought of as a configuration file for the GBB tool, automatically created by GBB init (thanks NPM init). It is usually formatted like this:
{" version ":" 0.0.1 ", "tool" : "gb_build", "package" : "build", "variables" : [ { "variable":"Date", "value":"{{.date}}" }, { "variable":"Commit", "value":"{{.gitCommit}}" } ] }Copy the code
version
: Version number. Reserved field.tool
: the compilation tool that GBB actually calls. Known optional values include:go_build
,go_install
,gb_build
. Note: This value cannot contain SpacesissueSo, for now, underline_
The connection.pakcage
: Package name, which is the definitionDate
,Commit
The package full path of this class of variables, e.ggithub.com/voidint/gbb/build
.variables
: List of variables. Each element in the list containsvariable
andvalue
Two properties.variable
Attributes represent variable names, such asDate
.value
Attributes represent variable-value expressions, such as{{.date}}
. Built-in variable expressionThe list of.