Skip to content

Commit

Permalink
Add a default version provider
Browse files Browse the repository at this point in the history
  • Loading branch information
plorenz committed Aug 24, 2023
1 parent ada2305 commit 326d838
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions versions/version_provider.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}

0 comments on commit 326d838

Please sign in to comment.