diff --git a/versions/version_provider.go b/versions/version_provider.go new file mode 100644 index 0000000..c6621c5 --- /dev/null +++ b/versions/version_provider.go @@ -0,0 +1,57 @@ +package versions + +import ( + "runtime" + "runtime/debug" + "time" +) + +type DefaultVersionProvider struct { + SourceBranch string + VersionInfo +} + +func (v DefaultVersionProvider) Branch() string { + return v.SourceBranch +} + +func (v DefaultVersionProvider) EncoderDecoder() VersionEncDec { + return &StdVersionEncDec +} + +func (v DefaultVersionProvider) Version() string { + return v.VersionInfo.Version +} + +func (v DefaultVersionProvider) BuildDate() string { + return v.VersionInfo.BuildDate +} + +func (v DefaultVersionProvider) Revision() string { + return v.VersionInfo.Revision +} + +func (v DefaultVersionProvider) AsVersionInfo() *VersionInfo { + return &v.VersionInfo +} + +func NewDefaultVersionProvider() VersionProvider { + buildInfo, ok := debug.ReadBuildInfo() + rev := "" + if ok { + for _, kv := range buildInfo.Settings { + if kv.Key == "vcs.revision" { + rev = kv.Value + } + } + } + return &DefaultVersionProvider{ + VersionInfo: VersionInfo{ + Version: "v0.0.0", + Revision: rev, + BuildDate: time.Now().String(), + OS: runtime.GOOS, + Arch: runtime.GOARCH, + }, + } +}