From 5d598819f74a860b4f265fb6fde033da008d706e Mon Sep 17 00:00:00 2001 From: Seb Date: Fri, 15 Jul 2022 13:15:55 +0200 Subject: grab RR version from build info Signed-off-by: Seb --- roadrunner/roadrunner.go | 36 +++++++++++++++++++++++++++++++++++- roadrunner/roadrunner_test.go | 1 + 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/roadrunner/roadrunner.go b/roadrunner/roadrunner.go index b3335db0..320e48c4 100644 --- a/roadrunner/roadrunner.go +++ b/roadrunner/roadrunner.go @@ -2,6 +2,7 @@ package roadrunner import ( "fmt" + "runtime/debug" configImpl "github.com/roadrunner-server/config/v2" endure "github.com/roadrunner-server/endure/pkg/container" @@ -17,6 +18,7 @@ const ( type RR struct { container *endure.Endure stop chan struct{} + Version string } // NewRR creates a new RR instance that can then be started or stopped by the caller @@ -32,7 +34,7 @@ func NewRR(cfgFile string, override *[]string, pluginList []interface{}) (*RR, e Prefix: rrPrefix, Timeout: containerCfg.GracePeriod, Flags: *override, - Version: meta.Version(), + Version: getRRVersion(), } // create endure container @@ -62,6 +64,7 @@ func NewRR(cfgFile string, override *[]string, pluginList []interface{}) (*RR, e rr := &RR{ container: endureContainer, stop: make(chan struct{}), + Version: cfg.Version, } return rr, nil @@ -99,3 +102,34 @@ func (rr *RR) Stop() error { func DefaultPluginsList() []interface{} { return container.Plugins() } + +// Tries to find the version info for a given module's path +// empty string if not found +func getModuleVersion(modulePath string) string { + bi, ok := debug.ReadBuildInfo() + if !ok { + return "" + } + + for _, d := range bi.Deps { + if d.Path == modulePath { + return d.Version + } + } + + return "" +} + +// Grabs RR's module version if available, meta.Version() otherwise +func getRRVersion() string { + v := getModuleVersion("github.com/roadrunner-server/roadrunner/v2") + if v == "" { + return meta.Version() + } + + if len(v) > 1 && ((v[0] == 'v' || v[0] == 'V') && (v[1] >= '0' && v[1] <= '9')) { + return v[1:] + } + + return v +} diff --git a/roadrunner/roadrunner_test.go b/roadrunner/roadrunner_test.go index 0f90f075..af591643 100644 --- a/roadrunner/roadrunner_test.go +++ b/roadrunner/roadrunner_test.go @@ -39,6 +39,7 @@ func TestNewWithConfig(t *testing.T) { rr, err := roadrunner.NewRR(cfgFile, &[]string{}, roadrunner.DefaultPluginsList()) assert.Nil(t, err) + assert.Equal(t, "2", string(rr.Version[0])) assert.Equal(t, fsm.Initialized, rr.CurrentState()) } -- cgit v1.2.3