/
spec.go
54 lines (48 loc) · 1.22 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
package allocation
import (
"encoding/json"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/copystructure"
"io"
"strings"
)
const (
specRevisionPrefix = "### SOIL "
SpecRevision = "1.0"
)
type Spec struct {
Revision string
}
func (s *Spec) Unmarshal(src string) (err error) {
for _, line := range strings.Split(src, "\n") {
if strings.HasPrefix(line, specRevisionPrefix) {
err = json.NewDecoder(strings.NewReader(strings.TrimSpace(strings.TrimPrefix(line, specRevisionPrefix)))).Decode(&s)
return
}
}
return
}
func (s *Spec) Marshal(w io.Writer) (err error) {
if _, err = w.Write([]byte(specRevisionPrefix)); err != nil {
return
}
err = json.NewEncoder(w).Encode(s)
return
}
func (s Spec) UnmarshalAssetSlice(paths SystemPaths, v AssetSlice, source string) (err error) {
err = &multierror.Error{}
prefix := v.GetVersionPrefix(s.Revision)
for _, line := range strings.Split(source, "\n") {
if strings.HasPrefix(line, prefix) {
cp, _ := copystructure.Copy(v.GetEmpty(paths))
v1 := cp.(Asset)
if rErr := v1.UnmarshalSpec(line, s, paths); rErr != nil {
err = multierror.Append(err, rErr)
continue
}
v.AppendItem(v1)
}
}
err = err.(*multierror.Error).ErrorOrNil()
return
}