Created
August 11, 2023 16:50
-
-
Save debuggerpk/0a8564c9319580a711cbd1a127237374 to your computer and use it in GitHub Desktop.
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
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