Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/flags/loadflags/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
)

func LoadForCli(progName string) error {
registerVersionFlag(progName)
return loadForCli(progName)
}

func LoadForDaemon(progName string) error {
registerVersionFlag(progName)
return loadFlags(filepath.Join("/etc", progName))
}
14 changes: 14 additions & 0 deletions lib/flags/loadflags/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ import (
"os"
"path/filepath"
"strings"

"github.com/Cloud-Foundations/Dominator/lib/version"
)

const systemDir = "/etc/config"

func registerVersionFlag(name string) {
flag.BoolFunc("version", "Print version information and exit", func(string) error {
info := version.Get()
fmt.Printf("%s %s\n", name, info.Version)
fmt.Printf(" Commit: %s\n", info.GitCommit)
fmt.Printf(" Built: %s\n", info.BuildDate)
fmt.Printf(" Go: %s\n", info.GoVersion)
os.Exit(0)
return nil
})
}

func loadFlags(dirname string) error {
err := loadFlagsFromFile(filepath.Join(dirname, "flags.default"))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions lib/version/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.12.0
77 changes: 77 additions & 0 deletions lib/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package version

import (
_ "embed"
"fmt"
"runtime"
"runtime/debug"
"strings"
)

//go:embed VERSION
var baseVersion string

type Info struct {
Version string `json:"version"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Dirty bool `json:"dirty"`
}

func Get() Info {
vcs := getVCSInfo()
version := strings.TrimSpace(baseVersion)
if vcs.revision != "unknown" {
version += "+" + vcs.revision
}
if vcs.dirty {
version += "-dirty"
}
return Info{
Version: version,
GitCommit: vcs.revision,
BuildDate: vcs.buildTime,
GoVersion: runtime.Version(),
Dirty: vcs.dirty,
}
}

func (i Info) String() string {
return fmt.Sprintf("%s (built: %s)", i.Version, i.BuildDate)
}

type vcsInfo struct {
revision string
buildTime string
dirty bool
}

func getVCSInfo() vcsInfo {
info := vcsInfo{
revision: "unknown",
buildTime: "unknown",
}

buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return info
}

for _, s := range buildInfo.Settings {
switch s.Key {
case "vcs.revision":
if len(s.Value) > 8 {
info.revision = s.Value[:8]
} else {
info.revision = s.Value
}
case "vcs.time":
info.buildTime = s.Value
case "vcs.modified":
info.dirty = s.Value == "true"
}
}

return info
}