mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Add new info package to handle version and build information
This commit is contained in:
parent
cfca5386cf
commit
cb94a48a29
2 changed files with 121 additions and 0 deletions
50
info/flags.go
Normal file
50
info/flags.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package info
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Safing/portbase/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
showVersion bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
modules.Register("info", prep, start, stop)
|
||||
|
||||
flag.BoolVar(&showVersion, "version", false, "show version and exit")
|
||||
}
|
||||
|
||||
func prep() error {
|
||||
if !strings.HasSuffix(os.Args[0], ".test") {
|
||||
if name == "[NAME]" ||
|
||||
version == "[version unknown]" ||
|
||||
commit == "[commit unknown]" ||
|
||||
buildOptions == "[options unknown]" ||
|
||||
buildUser == "[user unknown]" ||
|
||||
buildHost == "[host unknown]" ||
|
||||
buildDate == "[date unknown]" ||
|
||||
buildSource == "[source unknown]" {
|
||||
return errors.New("please build using the supplied build script.\n$ ./build {main.go|...}")
|
||||
}
|
||||
}
|
||||
|
||||
if showVersion {
|
||||
fmt.Println(FullVersion())
|
||||
return modules.ErrCleanExit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func stop() error {
|
||||
return nil
|
||||
}
|
71
info/version.go
Normal file
71
info/version.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package info
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
name = "[NAME]"
|
||||
version = "[version unknown]"
|
||||
commit = "[commit unknown]"
|
||||
buildOptions = "[options unknown]"
|
||||
buildUser = "[user unknown]"
|
||||
buildHost = "[host unknown]"
|
||||
buildDate = "[date unknown]"
|
||||
buildSource = "[source unknown]"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
Name string
|
||||
Version string
|
||||
Commit string
|
||||
BuildOptions string
|
||||
BuildUser string
|
||||
BuildHost string
|
||||
BuildDate string
|
||||
BuildSource string
|
||||
}
|
||||
|
||||
func Set(setName string, setVersion string) {
|
||||
name = setName
|
||||
version = setVersion
|
||||
}
|
||||
|
||||
func GetInfo() *Info {
|
||||
return &Info{
|
||||
Name: name,
|
||||
Version: version,
|
||||
Commit: commit,
|
||||
BuildOptions: buildOptions,
|
||||
BuildUser: buildUser,
|
||||
BuildHost: buildHost,
|
||||
BuildDate: buildDate,
|
||||
BuildSource: buildSource,
|
||||
}
|
||||
}
|
||||
|
||||
func Version() string {
|
||||
if strings.HasPrefix(commit, fmt.Sprintf("v%s-0-", version)) {
|
||||
return version
|
||||
} else {
|
||||
return version + "*"
|
||||
}
|
||||
}
|
||||
|
||||
func FullVersion() string {
|
||||
s := ""
|
||||
if strings.HasPrefix(commit, fmt.Sprintf("v%s-0-", version)) {
|
||||
s += fmt.Sprintf("%s\nversion %s\n", name, version)
|
||||
} else {
|
||||
s += fmt.Sprintf("%s\ndevelopment build, built on top version %s\n", name, version)
|
||||
}
|
||||
s += fmt.Sprintf("\ncommit %s\n", commit)
|
||||
s += fmt.Sprintf("built with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)
|
||||
s += fmt.Sprintf(" using options %s\n", strings.Replace(buildOptions, "§", " ", -1))
|
||||
s += fmt.Sprintf(" by %s@%s\n", buildUser, buildHost)
|
||||
s += fmt.Sprintf(" on %s\n", buildDate)
|
||||
s += fmt.Sprintf("\nLicensed under the AGPL license.\nThe source code is available here: %s", buildSource)
|
||||
return s
|
||||
}
|
Loading…
Add table
Reference in a new issue