From 589ad638eab4a38060f39061c00ad81be7cc2fbe Mon Sep 17 00:00:00 2001 From: Slex Date: Sun, 11 Aug 2019 00:31:22 +0300 Subject: [PATCH] Implement feature from https://github.com/yggdrasil-network/yggdrasil-go/issues/488 --- build | 2 +- cmd/yggdrasil/main.go | 11 ++++++----- cmd/yggdrasilctl/main.go | 9 +++++++++ src/admin/admin.go | 5 +++-- src/version/version.go | 22 ++++++++++++++++++++++ src/yggdrasil/api.go | 18 ------------------ src/yggdrasil/core.go | 8 +++----- src/yggdrasil/nodeinfo.go | 5 +++-- 8 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 src/version/version.go diff --git a/build b/build index 80a971f..c1e5f86 100755 --- a/build +++ b/build @@ -2,7 +2,7 @@ set -ef -PKGSRC=${PKGSRC:-github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil} +PKGSRC=${PKGSRC:-github.com/yggdrasil-network/yggdrasil-go/src/version} PKGNAME=${PKGNAME:-$(sh contrib/semver/name.sh)} PKGVER=${PKGVER:-$(sh contrib/semver/version.sh --bare)} diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index ee8ebe8..36494c4 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -25,6 +25,7 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/multicast" "github.com/yggdrasil-network/yggdrasil-go/src/tuntap" + "github.com/yggdrasil-network/yggdrasil-go/src/version" "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" ) @@ -119,7 +120,7 @@ func main() { normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised") confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") - version := flag.Bool("version", false, "prints the version of this build") + ver := flag.Bool("version", false, "prints the version of this build") logging := flag.String("logging", "info,warn,error", "comma-separated list of logging levels to enable") logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"") flag.Parse() @@ -127,9 +128,9 @@ func main() { var cfg *config.NodeConfig var err error switch { - case *version: - fmt.Println("Build name:", yggdrasil.BuildName()) - fmt.Println("Build version:", yggdrasil.BuildVersion()) + case *ver: + fmt.Println("Build name:", version.BuildName()) + fmt.Println("Build version:", version.BuildVersion()) os.Exit(0) case *autoconf: // Use an autoconf-generated config, this will give us random keys and @@ -174,7 +175,7 @@ func main() { case "stdout": logger = log.New(os.Stdout, "", log.Flags()) case "syslog": - if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", yggdrasil.BuildName()); err == nil { + if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", version.BuildName()); err == nil { logger = log.New(syslogger, "", log.Flags()) } default: diff --git a/cmd/yggdrasilctl/main.go b/cmd/yggdrasilctl/main.go index 51f4fa5..4cc9745 100644 --- a/cmd/yggdrasilctl/main.go +++ b/cmd/yggdrasilctl/main.go @@ -19,6 +19,7 @@ import ( "github.com/hjson/hjson-go" "github.com/yggdrasil-network/yggdrasil-go/src/defaults" + "github.com/yggdrasil-network/yggdrasil-go/src/version" ) type admin_info map[string]interface{} @@ -53,9 +54,17 @@ func main() { server := flag.String("endpoint", endpoint, "Admin socket endpoint") injson := flag.Bool("json", false, "Output in JSON format (as opposed to pretty-print)") verbose := flag.Bool("v", false, "Verbose output (includes public keys)") + ver := flag.Bool("version", false, "Prints the version of this build") flag.Parse() args := flag.Args() + if *ver { + fmt.Println(os.Args[0], "build name:", version.BuildName()) + fmt.Println(os.Args[0], "version:", version.BuildVersion()) + fmt.Println("\nFor get yggdrasil version use\n - ", os.Args[0], "getSelf") + os.Exit(0) + } + if len(args) == 0 { flag.Usage() return diff --git a/src/admin/admin.go b/src/admin/admin.go index 9789bca..c7fc151 100644 --- a/src/admin/admin.go +++ b/src/admin/admin.go @@ -18,6 +18,7 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/util" + "github.com/yggdrasil-network/yggdrasil-go/src/version" "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" ) @@ -86,8 +87,8 @@ func (a *AdminSocket) Init(c *yggdrasil.Core, state *config.NodeState, log *log. "self": Info{ ip: Info{ "box_pub_key": c.EncryptionPublicKey(), - "build_name": yggdrasil.BuildName(), - "build_version": yggdrasil.BuildVersion(), + "build_name": version.BuildName(), + "build_version": version.BuildVersion(), "coords": fmt.Sprintf("%v", c.Coords()), "subnet": subnet.String(), }, diff --git a/src/version/version.go b/src/version/version.go new file mode 100644 index 0000000..4cc7a8f --- /dev/null +++ b/src/version/version.go @@ -0,0 +1,22 @@ +package version + +var buildName string +var buildVersion string + +// BuildName gets the current build name. This is usually injected if built +// from git, or returns "unknown" otherwise. +func BuildName() string { + if buildName == "" { + return "yggdrasilctl" + } + return buildName +} + +// BuildVersion gets the current build version. This is usually injected if +// built from git, or returns "unknown" otherwise. +func BuildVersion() string { + if buildVersion == "" { + return "unknown" + } + return buildVersion +} diff --git a/src/yggdrasil/api.go b/src/yggdrasil/api.go index 98f9013..57ee5c6 100644 --- a/src/yggdrasil/api.go +++ b/src/yggdrasil/api.go @@ -241,24 +241,6 @@ func (c *Core) GetSessions() []Session { return sessions } -// BuildName gets the current build name. This is usually injected if built -// from git, or returns "unknown" otherwise. -func BuildName() string { - if buildName == "" { - return "yggdrasil" - } - return buildName -} - -// BuildVersion gets the current build version. This is usually injected if -// built from git, or returns "unknown" otherwise. -func BuildVersion() string { - if buildVersion == "" { - return "unknown" - } - return buildVersion -} - // ConnListen returns a listener for Yggdrasil session connections. func (c *Core) ConnListen() (*Listener, error) { c.sessions.listenerMutex.Lock() diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 2aa7834..0921ab9 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -10,11 +10,9 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/crypto" + "github.com/yggdrasil-network/yggdrasil-go/src/version" ) -var buildName string -var buildVersion string - // The Core object represents the Yggdrasil node. You should create a Core // object for each Yggdrasil node you plan to run. type Core struct { @@ -164,10 +162,10 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState, Previous: *nc, } - if name := BuildName(); name != "unknown" { + if name := version.BuildName(); name != "unknown" { c.log.Infoln("Build name:", name) } - if version := BuildVersion(); version != "unknown" { + if version := version.BuildVersion(); version != "unknown" { c.log.Infoln("Build version:", version) } diff --git a/src/yggdrasil/nodeinfo.go b/src/yggdrasil/nodeinfo.go index f1c7ed0..73d4e11 100644 --- a/src/yggdrasil/nodeinfo.go +++ b/src/yggdrasil/nodeinfo.go @@ -9,6 +9,7 @@ import ( "time" "github.com/yggdrasil-network/yggdrasil-go/src/crypto" + "github.com/yggdrasil-network/yggdrasil-go/src/version" ) type nodeinfo struct { @@ -99,8 +100,8 @@ func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) error { m.myNodeInfoMutex.Lock() defer m.myNodeInfoMutex.Unlock() defaults := map[string]interface{}{ - "buildname": BuildName(), - "buildversion": BuildVersion(), + "buildname": version.BuildName(), + "buildversion": version.BuildVersion(), "buildplatform": runtime.GOOS, "buildarch": runtime.GOARCH, }