Skip to content

Commit

Permalink
feat: add version flag and embed build info in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhop committed Aug 3, 2022
1 parent 29e6f77 commit 04d22a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -564,19 +570,33 @@ 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")
flag.String("logging.level", "", "Logging level may be one of: trace, debug, info, warning, error, fatal and panic")
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")
Expand Down

0 comments on commit 04d22a4

Please sign in to comment.