-
Notifications
You must be signed in to change notification settings - Fork 838
/
utils.go
79 lines (65 loc) · 1.87 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
package upgrade
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"github.com/hashicorp/go-version"
)
var upgradesPath = "../../app/upgrades"
// Custom comparator for sorting semver version strings
type byVersion []string
func (s byVersion) Len() int { return len(s) }
func (s byVersion) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Compare semver versions strings properly
func (s byVersion) Less(i, j int) bool {
v1, err := version.NewVersion(s[i])
if err != nil {
log.Fatal(err)
}
v2, err := version.NewVersion(s[j])
if err != nil {
log.Fatal(err)
}
return v1.LessThan(v2)
}
// RetrieveUpgradeVersion parses app/upgrades folder and returns slice of semver upgrade versions
// ascending order, e.g ["v1.0.0", "v1.0.1", "v1.1.0", ... , "v10.0.0"]
func (m *Manager) RetrieveUpgradesList() ([]string, error) {
dirs, err := os.ReadDir(upgradesPath)
if err != nil {
return nil, err
}
versions := []string{}
// pattern to find quoted string(upgrade version) in a file e.g. "v10.0.0"
pattern := regexp.MustCompile(`"(.*?)"`)
for _, d := range dirs {
// creating path to upgrade dir file with constant upgrade version
constantsPath := fmt.Sprintf("%s/%s/constants.go", upgradesPath, d.Name())
f, err := os.ReadFile(constantsPath)
if err != nil {
return nil, err
}
v := pattern.FindString(string(f))
// v[1:len(v)-1] subslice used to remove quotes from version string
versions = append(versions, v[1:len(v)-1])
}
sort.Sort(byVersion(versions))
return versions, nil
}
// ExportState executes the 'docker cp' command to copy container .evmosd dir
// to the specified target dir (local)
//
// See https://docs.docker.com/engine/reference/commandline/cp/
func (m *Manager) ExportState(targetDir string) error {
/* #nosec G204 */
cmd := exec.Command(
"docker",
"cp",
fmt.Sprintf("%s:/root/.evmosd", m.ContainerID()),
targetDir,
)
return cmd.Run()
}