From 6d584adc235b210fbf6342942b90a486f818a2e5 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Wed, 16 Nov 2022 15:30:57 +0100 Subject: [PATCH] Add version package to later use as configuration package --- globals/globals.go | 13 +++++++++++++ main.go | 7 +++---- version/version.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 globals/globals.go create mode 100644 version/version.go diff --git a/globals/globals.go b/globals/globals.go new file mode 100644 index 0000000..a5d2bce --- /dev/null +++ b/globals/globals.go @@ -0,0 +1,13 @@ +package globals + +import ( + "os" + "path/filepath" + + "github.com/arduino/arduinoOTA/version" +) + +var ( + // VersionInfo contains all info injected during build + VersionInfo = version.NewInfo(filepath.Base(os.Args[0])) +) diff --git a/main.go b/main.go index c716195..6455435 100644 --- a/main.go +++ b/main.go @@ -15,10 +15,9 @@ import ( "strconv" "strings" "time" -) -// AppVersion is the application version. -const AppVersion = "1.3.0" + "github.com/arduino/arduinoOTA/globals" +) var compileInfo string @@ -45,7 +44,7 @@ func main() { flag.Parse() if *version { - fmt.Println(AppVersion + compileInfo) + fmt.Println(globals.VersionInfo.String() + compileInfo) os.Exit(0) } diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..3f7261e --- /dev/null +++ b/version/version.go @@ -0,0 +1,39 @@ +package version + +import "fmt" + +var ( + defaultVersionString = "0.0.0-git" + versionString = "" + commit = "" + date = "" +) + +// Info is a struct that contains information about the application +type Info struct { + Application string `json:"Application"` + VersionString string `json:"VersionString"` + Commit string `json:"Commit"` + Date string `json:"Date"` +} + +// NewInfo returns a pointer to an updated Info struct +func NewInfo(application string) *Info { + return &Info{ + Application: application, + VersionString: versionString, + Commit: commit, + Date: date, + } +} + +func (i *Info) String() string { + return fmt.Sprintf("%[1]s Version: %[2]s Commit: %[3]s Date: %[4]s", i.Application, i.VersionString, i.Commit, i.Date) +} + +//nolint:gochecknoinits +func init() { + if versionString == "" { + versionString = defaultVersionString + } +}