Skip to content

Commit

Permalink
grab RR version from build info
Browse files Browse the repository at this point in the history
Signed-off-by: Seb <khepin@gmail.com>
  • Loading branch information
khepin committed Jul 15, 2022
1 parent 539b4a1 commit 5d59881
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
36 changes: 35 additions & 1 deletion roadrunner/roadrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions roadrunner/roadrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down

0 comments on commit 5d59881

Please sign in to comment.