-
Notifications
You must be signed in to change notification settings - Fork 2
/
upgradeabilityChecker.go
37 lines (28 loc) · 1.09 KB
/
upgradeabilityChecker.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
package controllers
import (
"fmt"
"github.com/cloudogu/cesapp-lib/core"
)
type upgradeChecker struct{}
// IsUpgradeable determines if the given toDogu is an upgrade of the given fromDogu. The option forceUpgrade overrides
// all checks always returns true.
func (u *upgradeChecker) IsUpgradeable(fromDogu *core.Dogu, toDogu *core.Dogu, forceUpgrade bool) (bool, error) {
if forceUpgrade {
return true, nil
}
return u.checkVersionBeforeUpgrade(fromDogu, toDogu)
}
func (u *upgradeChecker) checkVersionBeforeUpgrade(fromDogu *core.Dogu, toDogu *core.Dogu) (bool, error) {
localVersion, err := core.ParseVersion(fromDogu.Version)
if err != nil {
return false, fmt.Errorf("could not check upgradeability of local dogu: %w", err)
}
remoteVersion, err := core.ParseVersion(toDogu.Version)
if err != nil {
return false, fmt.Errorf("could not check upgradeability of remote dogu: %w", err)
}
if remoteVersion.IsOlderThan(localVersion) {
return false, fmt.Errorf("downgrade from %s to %s is not allowed", fromDogu.Version, toDogu.Version)
}
return remoteVersion.IsNewerThan(localVersion), nil
}