-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
42 lines (36 loc) · 906 Bytes
/
state.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
package asset
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// State is the state of an Asset.
type State struct {
Contents []Content
}
// Content is a generated portion of an Asset.
type Content struct {
Name string // the path on disk for this content.
Data []byte
}
// PersistToFile persists the data in the State to files. Each Content entry that
// has a non-empty Name will be persisted to a file with that name.
func (s *State) PersistToFile(directory string) error {
if s == nil {
return nil
}
for _, c := range s.Contents {
if c.Name == "" {
continue
}
path := filepath.Join(directory, c.Name)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return errors.Wrap(err, "failed to create dir")
}
if err := ioutil.WriteFile(path, c.Data, 0644); err != nil {
return errors.Wrap(err, "failed to write file")
}
}
return nil
}