Skip to content

Commit

Permalink
Merge pull request #215 from replicatedhq/laverya/import-version
Browse files Browse the repository at this point in the history
add a version.Version function that returns the import version
  • Loading branch information
laverya authored Oct 20, 2023
2 parents 225cf0a + f019ef4 commit ef399ee
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

import (
"fmt"
"runtime/debug"
)

// NOTE: these variables are injected at build time
Expand All @@ -10,6 +11,32 @@ var (
version, gitSHA, buildTime string
)

const pvmModuleName = "github.com/replicatedhq/pvmigrate"

func init() {
if version == "" {
// attempt to get the version from runtime build info
// go through all the dependencies to find the pvmigrate module version
// failure to read buildinfo is ok, we just won't have a version set
bi, ok := debug.ReadBuildInfo()
if ok {
for _, dep := range bi.Deps {
if dep.Path == pvmModuleName {
version = dep.Version
if dep.Replace != nil {
version = dep.Replace.Version
}
break
}
}
}
}
}

func Version() string {
return version
}

// Print prints the version, git sha and build time.
func Print() {
fmt.Printf("version=%s\nsha=%s\ntime=%s\n", version, gitSHA, buildTime)
Expand Down

0 comments on commit ef399ee

Please sign in to comment.