From cd875097a7df2928beb79d3adfb9eac29ba5be92 Mon Sep 17 00:00:00 2001 From: Alvaro Alda Date: Sat, 3 Aug 2019 13:02:21 +0200 Subject: [PATCH] Add new version command to print build info --- .goreleaser.yml | 4 +++ build/cgo_compiler.go | 32 +++++++++++++++++ build/info.go | 83 +++++++++++++++++++++++++++++++++++++++++++ cmd/bug.go | 6 ++-- cmd/root.go | 14 -------- cmd/version.go | 51 ++++++++++++++++++++++++++ main.go | 7 ---- 7 files changed, 173 insertions(+), 24 deletions(-) create mode 100644 build/cgo_compiler.go create mode 100644 build/info.go create mode 100644 cmd/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 788e7ead5..f13c46c5d 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 diff --git a/build/cgo_compiler.go b/build/cgo_compiler.go new file mode 100644 index 000000000..ca2e1fb94 --- /dev/null +++ b/build/cgo_compiler.go @@ -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()) +} diff --git a/build/info.go b/build/info.go new file mode 100644 index 000000000..b60394dc3 --- /dev/null +++ b/build/info.go @@ -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, + } +} diff --git a/cmd/bug.go b/cmd/bug.go index 5393d8001..346bcc924 100644 --- a/cmd/bug.go +++ b/cmd/bug.go @@ -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" @@ -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 }) diff --git a/cmd/root.go b/cmd/root.go index e882c7bd1..efefb4b2a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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, - }) -} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 000000000..41eb62665 --- /dev/null +++ b/cmd/version.go @@ -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) +} diff --git a/main.go b/main.go index dc7469381..445b616ad 100644 --- a/main.go +++ b/main.go @@ -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) }