diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 4236026..006bfea 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -6,6 +6,10 @@ builds: - linux - windows - darwin + ldflags: + - -X main.BuildDate={{ .CommitDate }} + - -X main.Version={{ .Version }} + - -X main.Commit={{ .Commit }} binary: tagger dockers: # build latest and specific tag version images diff --git a/tagger.go b/tagger.go index c981c1e..54627ef 100644 --- a/tagger.go +++ b/tagger.go @@ -24,6 +24,12 @@ import ( "golang.org/x/oauth2" ) +var ( + Version string // will be populated by linker during `go build` + BuildDate string // will be populated by linker during `go build` + Commit string // will be populated by linker during `go build` +) + type objectTagMap map[string]map[int][]string type ReportMap map[string]map[string]ReportData @@ -564,6 +570,14 @@ func init() { }) } +func version() { + fmt.Printf("Tagger Build Information\nVersion: %s\nBuild Date: %s\nCommit: %s\n", + Version, + BuildDate, + Commit, + ) +} + func main() { // prep and parse flags flag.String("config", "", "Path to configuration file to use") @@ -571,12 +585,18 @@ func main() { flag.Bool("dry-run", false, "Don't apply the tag changes") flag.Bool("report", false, "Report output to summarize tag changes") flag.Bool("json", false, "Provide changes in JSON") + flag.BoolP("version", "v", false, "Print version information about this build of tagger") flag.Parse() if err := viper.BindPFlags(flag.CommandLine); err != nil { log.Fatal("Unable to bind flags") } + if viper.GetBool("version") { + version() + os.Exit(0) + } + // get config configFile := viper.GetString("config") viper.SetConfigType("yaml")