Split info package for easier usage without modules

This commit is contained in:
Daniel 2020-01-12 20:52:15 +01:00
parent 9f81efa210
commit cac3c2b2e5
3 changed files with 62 additions and 62 deletions

View file

@ -1,62 +0,0 @@
package info
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/safing/portbase/modules"
)
var (
showVersion bool
)
func init() {
modules.Register("info", prep, nil, nil)
flag.BoolVar(&showVersion, "version", false, "show version and exit")
}
func prep() error {
err := CheckVersion()
if err != nil {
return err
}
if PrintVersion() {
return modules.ErrCleanExit
}
return nil
}
// CheckVersion checks if the metadata is ok.
func CheckVersion() error {
if !strings.HasSuffix(os.Args[0], ".test") {
if name == "[NAME]" {
return errors.New("must call SetInfo() before calling CheckVersion()")
}
if version == "[version unknown]" ||
commit == "[commit unknown]" ||
license == "[license 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|...}")
}
}
return nil
}
// PrintVersion prints the version, if requested, and returns if it did so.
func PrintVersion() (printed bool) {
if showVersion {
fmt.Println(FullVersion())
return true
}
return false
}

40
info/module/flags.go Normal file
View file

@ -0,0 +1,40 @@
package module
import (
"flag"
"fmt"
"github.com/safing/portbase/info"
"github.com/safing/portbase/modules"
)
var (
showVersion bool
)
func init() {
modules.Register("info", prep, nil, nil)
flag.BoolVar(&showVersion, "version", false, "show version and exit")
}
func prep() error {
err := info.CheckVersion()
if err != nil {
return err
}
if printVersion() {
return modules.ErrCleanExit
}
return nil
}
// printVersion prints the version, if requested, and returns if it did so.
func printVersion() (printed bool) {
if showVersion {
fmt.Println(info.FullVersion())
return true
}
return false
}

View file

@ -1,7 +1,9 @@
package info
import (
"errors"
"fmt"
"os"
"runtime"
"strings"
)
@ -80,3 +82,23 @@ func FullVersion() string {
s += fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, buildSource)
return s
}
// CheckVersion checks if the metadata is ok.
func CheckVersion() error {
if !strings.HasSuffix(os.Args[0], ".test") {
if name == "[NAME]" {
return errors.New("must call SetInfo() before calling CheckVersion()")
}
if version == "[version unknown]" ||
commit == "[commit unknown]" ||
license == "[license 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|...}")
}
}
return nil
}