Skip to content

Instantly share code, notes, and snippets.

@debuggerpk
Created August 11, 2023 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debuggerpk/0a8564c9319580a711cbd1a127237374 to your computer and use it in GitHub Desktop.
Save debuggerpk/0a8564c9319580a711cbd1a127237374 to your computer and use it in GitHub Desktop.
func WithVersionFromBuildInfo() ServiceOption {
return func(s Service) {
if info, ok := debug.ReadBuildInfo(); ok {
var (
revision string
modified string
timestamp time.Time
version string
)
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
revision = setting.Value
}
if setting.Key == "vcs.modified" {
modified = setting.Value
}
if setting.Key == "vcs.time" {
timestamp, _ = time.Parse(time.RFC3339, setting.Value)
}
}
if len(revision) > 0 && len(modified) > 0 && timestamp.Unix() > 0 {
version = timestamp.Format("2006.01.02") + "." + revision[:8]
} else {
version = "debug"
}
if modified == "true" {
version += "-dev"
}
s.(*config).Version = version
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment