This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
spec.go
135 lines (114 loc) · 2.55 KB
/
spec.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/Masterminds/semver"
)
// Filename to read/write the spec data.
const specfile = "tools.json"
type spec struct {
Tools []*tool
RetoolVersion *semver.Version
}
// jsonSpec is a helper type to describe the JSON encoding of a spec
type jsonSpec struct {
Tools []*tool
RetoolVersion string
}
func (s *spec) UnmarshalJSON(data []byte) error {
js := new(jsonSpec)
if err := json.Unmarshal(data, js); err != nil {
return err
}
if js.RetoolVersion != "" {
v, err := semver.NewVersion(js.RetoolVersion)
if err != nil {
return err
}
s.RetoolVersion = v
}
s.Tools = js.Tools
return nil
}
func (s spec) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonSpec{
Tools: s.Tools,
RetoolVersion: s.RetoolVersion.String(),
})
}
func (s spec) write() error {
specfilePath := filepath.Join(baseDirPath, specfile)
f, err := os.Create(specfilePath)
if err != nil {
return fmt.Errorf("unable to open %s: %s", specfile, err)
}
defer func() {
_ = f.Close()
}()
// s.write() is called when we have successfully added, removed, or upgraded a
// tool. The success of that operation indicates that we should be comfortable
// bumping up this version.
s.RetoolVersion = version
bytes, err := json.MarshalIndent(s, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal json spec: %s", err)
}
_, err = f.Write(bytes)
if err != nil {
return fmt.Errorf("unable to write %s: %s", specfile, err)
}
return nil
}
func (s spec) find(t *tool) int {
for i, tt := range s.Tools {
if t.Repository == tt.Repository {
return i
}
}
return -1
}
func (s spec) cleanup() {
var pkgs []string
for _, t := range s.Tools {
pkgs = append(pkgs, t.Repository)
}
clean(pkgs)
}
func readPath(path string) (spec, error) {
file, err := os.Open(path)
if err != nil {
return spec{}, fmt.Errorf("unable to open spec file at %s: %s", path, err)
}
defer func() {
_ = file.Close()
}()
s := new(spec)
err = json.NewDecoder(file).Decode(s)
if err != nil {
return spec{}, err
}
return *s, nil
}
func read() (spec, error) {
specfilePath := filepath.Join(baseDirPath, specfile)
return readPath(specfilePath)
}
func specExists() bool {
specfilePath := filepath.Join(baseDirPath, specfile)
_, err := os.Stat(specfilePath)
if os.IsNotExist(err) {
return false
}
if err != nil {
fatal("unable to stat tools.json: %s", err)
}
return true
}
func writeBlankSpec() error {
return spec{
Tools: []*tool{},
RetoolVersion: version,
}.write()
}