From cac3c2b2e5688b064921da6c88e974942ad95eaa Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 12 Jan 2020 20:52:15 +0100 Subject: [PATCH] Split info package for easier usage without modules --- info/flags.go | 62 -------------------------------------------- info/module/flags.go | 40 ++++++++++++++++++++++++++++ info/version.go | 22 ++++++++++++++++ 3 files changed, 62 insertions(+), 62 deletions(-) delete mode 100644 info/flags.go create mode 100644 info/module/flags.go diff --git a/info/flags.go b/info/flags.go deleted file mode 100644 index 6c677b4..0000000 --- a/info/flags.go +++ /dev/null @@ -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 -} diff --git a/info/module/flags.go b/info/module/flags.go new file mode 100644 index 0000000..a3caaa1 --- /dev/null +++ b/info/module/flags.go @@ -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 +} diff --git a/info/version.go b/info/version.go index 2d85237..c7ed4b8 100644 --- a/info/version.go +++ b/info/version.go @@ -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 +}