Upgrade to ReachView v2.24.0

This commit is contained in:
2020-11-23 11:15:30 +01:00
parent b8a07953dd
commit e06aa5129a
16 changed files with 260 additions and 103 deletions

17
emlid/common/client.go Normal file
View File

@ -0,0 +1,17 @@
package common
import (
"forge.cadoles.com/Pyxis/orion/emlid"
)
// Client is a ReachRS Updater client
type Client struct {
*emlid.Client
}
// NewClient returns a new ReachRS ReachView client
func NewClient(opts ...emlid.OptionFunc) *Client {
return &Client{
Client: emlid.NewClient(opts...),
}
}

View File

@ -0,0 +1,51 @@
package common
import (
"context"
"github.com/blang/semver/v4"
"github.com/pkg/errors"
)
type VersionHandlerFunc func(ctx context.Context, version string, stable bool) error
type VersionHandler struct {
Range string
Handler VersionHandlerFunc
}
func NewVersionHandler(versionRange string, h VersionHandlerFunc) *VersionHandler {
return &VersionHandler{
Range: versionRange,
Handler: h,
}
}
func (c *Client) HandleVersion(ctx context.Context, handlers ...*VersionHandler) error {
rawVersion, stable, err := c.ReachViewVersion(ctx)
if err != nil {
return errors.Wrap(err, "could not retrieve reachview version")
}
version, err := semver.ParseTolerant(rawVersion)
if err != nil {
return errors.Wrap(err, "could not parse reachview version")
}
for _, h := range handlers {
versionRange, err := semver.ParseRange(h.Range)
if err != nil {
return errors.Wrap(err, "could not parse handler version range")
}
if !versionRange(version) {
continue
}
if err := h.Handler(ctx, rawVersion, stable); err != nil {
return errors.WithStack(err)
}
}
return nil
}

View File

@ -0,0 +1,26 @@
package common
import (
"context"
"strings"
)
const (
eventGetReachViewVersion = "get reachview version"
eventReachViewVersionResults = "current reachview version"
)
type reachViewVersion struct {
Version string `json:"version"`
Stable bool `json:"bool"`
}
// ReachViewVersion returns the ReachRS module ReachView version
func (c *Client) ReachViewVersion(ctx context.Context) (string, bool, error) {
res := &reachViewVersion{}
if err := c.ReqResp(ctx, eventGetReachViewVersion, nil, eventReachViewVersionResults, res); err != nil {
return "", false, err
}
return strings.TrimSpace(res.Version), res.Stable, nil
}

View File

@ -0,0 +1,39 @@
package common
import (
"context"
"testing"
"time"
"forge.cadoles.com/Pyxis/orion/emlid"
)
func TestClientReachViewVersion(t *testing.T) {
if !*runCommonIntegrationTests {
t.Skip("To run this test, use: go test -updater-integration")
}
client := NewClient(
emlid.WithStandardLogger(),
emlid.WithEndpoint(*reachHost, 80),
)
if err := client.Connect(); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
version, _, err := client.ReachViewVersion(ctx)
if err != nil {
t.Error(err)
}
if version == "" {
t.Error("version should not be empty")
}
defer client.Close()
}

13
emlid/common/util_test.go Normal file
View File

@ -0,0 +1,13 @@
package common
import "flag"
var runCommonIntegrationTests = flag.Bool(
"common-integration", false,
"Run the 'Common' integration tests (in addition to the unit tests)",
)
var reachHost = flag.String(
"reach-host", "192.168.42.1",
"The Reach module host to use in integration tests",
)