-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
100 lines (77 loc) · 1.58 KB
/
versions.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
type Version struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
}
type Versions []Version
func (v Version) IsNotEmpty() bool {
if v.Version == "" {
return false
}
return true
}
func (list Versions) Get(repoName string) (*Version, error) {
for _, version := range list {
if strings.Contains(version.Name, repoName) {
return &version, nil
}
}
return &Version{}, fmt.Errorf("failed finding %s", repoName)
}
func (list *Versions) AddReplace(ver Version) {
var idx = -1
for i, version := range *list {
if strings.Contains(version.Name, ver.Name) {
idx = i
break
}
}
if idx == -1 {
*list = append(*list, ver)
return
}
(*list)[idx] = ver
}
func versionsFile() io.ReadWriteCloser {
var (
srcPath string
err error
)
if config.VersionsPath == "" {
srcPath = filepath.Join(".", "data", "versions.json")
} else {
srcPath, err = filepath.Abs(config.VersionsPath)
}
file, err := os.OpenFile(srcPath, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
log.Fatal(err)
}
return file
}
func readVersions(list *Versions) error {
file := versionsFile()
defer file.Close()
err := json.NewDecoder(file).Decode(&list)
if err != nil {
err = errors.Wrap(err, "failed reading in versions list")
}
return err
}
func saveVersions(list *Versions) error {
file := versionsFile()
err := json.NewEncoder(file).Encode(list)
if err != nil {
return errors.Wrap(err, "failed writing to versions file")
}
return nil
}