mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +00:00
Split info package for easier usage without modules
This commit is contained in:
parent
9f81efa210
commit
cac3c2b2e5
3 changed files with 62 additions and 62 deletions
|
@ -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
40
info/module/flags.go
Normal 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
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package info
|
package info
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"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)
|
s += fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, buildSource)
|
||||||
return s
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue