-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from aalda/build-info
Add new version command to print build info Former-commit-id: 48f3634 [formerly 2e5c31f] Former-commit-id: 0f4dc3a
- Loading branch information
Showing
8 changed files
with
174 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters