-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.go
67 lines (58 loc) · 1.86 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Package upgrade - Content managed by Project Forge, see [projectforge.md] for details.
package upgrade
import (
"context"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/google/go-github/v39/github"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/util"
)
type Service struct {
logger util.Logger
client *github.Client
}
func NewService(ctx context.Context, logger util.Logger) *Service {
return &Service{logger: logger, client: createGitHubClient(ctx)}
}
func (s *Service) UpgradeIfNeeded(ctx context.Context, o string, n string, force bool) error {
currVersion, err := semver.NewVersion(o)
if err != nil {
return errors.Wrapf(err, "unable to parse current version from [%s]", o)
}
tgtRelease, err := s.getRelease(ctx, n)
if err != nil {
return err
}
tgtVersion, err := semver.NewVersion(strings.TrimPrefix(*tgtRelease.TagName, "v"))
if err != nil {
return errors.Wrapf(err, "unable to parse version of latest release from [%s]", *tgtRelease.TagName)
}
if !force {
if tgtVersion.Equal(*currVersion) {
const msg = "no action needed, already using [%s], the latest version"
s.logger.Infof(msg, currVersion.String())
return nil
}
if tgtVersion.LessThan(*currVersion) {
const msg = "no action needed, you're using [%s], which is somehow higher than [%s], the latest version"
s.logger.Infof(msg, currVersion.String(), tgtVersion.String())
return nil
}
}
s.logger.Infof("upgrading from [%s] to [%s]...", currVersion.String(), tgtVersion.String())
zipped, err := s.downloadAsset(ctx, *tgtVersion, tgtRelease)
if err != nil {
return err
}
fc, err := unzip(zipped)
if err != nil {
return err
}
err = overwrite(fc)
if err != nil {
return errors.Wrap(err, "unable to overwrite binary installation")
}
s.logger.Infof("successfully upgraded [%s] to version [%s]", util.AppKey, tgtVersion.String())
return nil
}