-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
controller.go
69 lines (60 loc) · 1.68 KB
/
controller.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
68
69
package updateaqua
import (
"context"
"errors"
"fmt"
"path/filepath"
"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/osfile"
"github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)
type Controller struct {
rootDir string
fs afero.Fs
runtime *runtime.Runtime
github RepositoriesService
installer AquaInstaller
}
func New(param *config.Param, fs afero.Fs, rt *runtime.Runtime, gh RepositoriesService, installer AquaInstaller) *Controller {
return &Controller{
rootDir: param.RootDir,
fs: fs,
runtime: rt,
github: gh,
installer: installer,
}
}
func (c *Controller) UpdateAqua(ctx context.Context, logE *logrus.Entry, param *config.Param) error {
rootBin := filepath.Join(c.rootDir, "bin")
if err := osfile.MkdirAll(c.fs, rootBin); err != nil {
return fmt.Errorf("create the directory: %w", err)
}
version, err := c.getVersion(ctx, param)
if err != nil {
return err
}
logE = logE.WithField("new_version", version)
if err := c.installer.InstallAqua(ctx, logE, version); err != nil {
return fmt.Errorf("download aqua: %w", logerr.WithFields(err, logrus.Fields{
"new_version": version,
}))
}
return nil
}
func (c *Controller) getVersion(ctx context.Context, param *config.Param) (string, error) {
switch len(param.Args) {
case 0:
release, _, err := c.github.GetLatestRelease(ctx, "aquaproj", "aqua")
if err != nil {
return "", fmt.Errorf("get the latest version of aqua: %w", err)
}
return release.GetTagName(), nil
case 1:
return param.Args[0], nil
default:
return "", errors.New("too many arguments")
}
}