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 👀.

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.

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

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

😊