52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
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
|
|
}
|