Skip to content

Commit

Permalink
Merge pull request #150 from aalda/build-info
Browse files Browse the repository at this point in the history
Add new version command to print build info

Former-commit-id: 48f3634 [formerly 2e5c31f]
Former-commit-id: 0f4dc3a
  • Loading branch information
aalda authored Aug 5, 2019
2 parents a954217 + 47209cc commit 5e2f570
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ builds:
- CGO_LDFLAGS_ALLOW=".*"
- GOOS=linux
binary: qed
# Custom ldflags templates.
# Default is `-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`.
ldflags:
- -s -w -X github.com/bbva/qed/build.tag={{.Tag}} -X github.com/bbva/qed/build.rev={{.FullCommit}} -X github.com/bbva/qed/build.utcTime={{.Date}}
goos:
- linux
- darwin
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ steps:
gocov-xml < coverage.json > coverage.xml
mkdir coverage
gocov-html < coverage.json > coverage/index.html
'$(GOROOT)/bin/go' clean -modcache
workingDirectory: '$(modulePath)'
displayName: 'Get dependencies, then build'

Expand Down
32 changes: 32 additions & 0 deletions build/cgo_compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package build

// const char* compilerVersion() {
// #if defined(__clang__)
// return __VERSION__;
// #elif defined(__GNUC__) || defined(__GNUG__)
// return "gcc " __VERSION__;
// #else
// return "non-gcc, non-clang (or an unrecognized version)";
// #endif
// }
import "C"

func cgoVersion() string {
return C.GoString(C.compilerVersion())
}
83 changes: 83 additions & 0 deletions build/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package build

import (
"fmt"
"runtime"
"time"
)

// TimeFormat is the reference format for build.Time. Make sure it stays in sync
// with the string passed to the linker in the goreleaser config file
const TimeFormat = "2006/01/02 15:04:05"

var (
// These variables are initialized via the linker -X flag in the
// goleaser config file when compiling release binaries.
tag = "unknown" // Tag of this build (git describe --tags)
utcTime string // Build time in UTC (year/month/day hour:min:sec)
rev string // SHA-1 of this build (git rev-parse)
cgoCompiler = cgoVersion()
platform = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)
)

// Info stores the build information
type Info struct {
GoVersion string
Tag string
Time string
Revision string
CgoCompiler string
Platform string
}

// Short returns a pretty printed build and version summary.
func (i Info) Short() string {
return fmt.Sprintf("QED %s (%s, built %s, %s)",
i.Tag, i.Platform, i.Time, i.GoVersion)
}

// GoTime parses the utcTime string and returns a time.Time.
func (i Info) GoTime() time.Time {
val, err := time.Parse(TimeFormat, i.Time)
if err != nil {
return time.Time{}
}
return val
}

// Timestamp parses the utcTime string and returns the number of seconds since epoch.
func (i Info) Timestamp() (int64, error) {
val, err := time.Parse(TimeFormat, i.Time)
if err != nil {
return 0, err
}
return val.Unix(), nil
}

// GetInfo returns an Info struct populated with the build information.
func GetInfo() Info {
return Info{
GoVersion: runtime.Version(),
Tag: tag,
Time: utcTime,
Revision: rev,
CgoCompiler: cgoCompiler,
Platform: platform,
}
}
6 changes: 3 additions & 3 deletions cmd/bug.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

"github.com/bbva/qed/build"
"github.com/bbva/qed/log"
"github.com/octago/sflags/gen/gpflag"
"github.com/spf13/cobra"
Expand All @@ -45,11 +46,10 @@ func formatInfo(w io.Writer, title string, body func() error) error {

func debugInfo(w io.Writer) error {
var err error
release := Ctx.Value(k("root.release")).(release)
qedVersion := release.version
buildInfo := build.GetInfo()
// Get build version
formatInfo(w, "Build Info", func() error {
fmt.Fprintf(w, "QED version %s, built in $GOPATH mode\n", qedVersion)
fmt.Fprintf(w, "QED version %s, built in $GOPATH mode\n", buildInfo.Tag)
return nil
})

Expand Down
14 changes: 0 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,3 @@ var Root *cobra.Command = &cobra.Command{
}

var Ctx context.Context = context.Background()

type release struct {
version string
commit string
date string
}

func SetReleaseInfo(version, commit, date string) {
Ctx = context.WithValue(Ctx, k("root.release"), release{
version: version,
commit: commit,
date: date,
})
}
51 changes: 51 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"os"
"text/tabwriter"

"github.com/bbva/qed/build"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Outputs version information",
Long: `
Output build version information.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
info := build.GetInfo()
tw := tabwriter.NewWriter(os.Stdout, 2, 1, 2, ' ', 0)
fmt.Fprintf(tw, "Build Tag: %s\n", info.Tag)
fmt.Fprintf(tw, "Build Time: %s\n", info.Time)
fmt.Fprintf(tw, "Platform: %s", info.Platform)
fmt.Fprintln(tw)
fmt.Fprintf(tw, "Go Version: %s\n", info.GoVersion)
fmt.Fprintf(tw, "C Compiler: %s\n", info.CgoCompiler)
fmt.Fprintf(tw, "Build SHA-1: %s\n", info.Revision)
return tw.Flush()
},
}

func init() {
Root.AddCommand(versionCmd)
}
7 changes: 0 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ import (
"github.com/bbva/qed/cmd"
)

var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
cmd.SetReleaseInfo(version, commit, date)
if err := cmd.Root.Execute(); err != nil {
os.Exit(-1)
}
Expand Down

0 comments on commit 5e2f570

Please sign in to comment.