/
util.go
91 lines (81 loc) · 2.02 KB
/
util.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
package filestore
import (
"bytes"
"io/ioutil"
"os"
"path"
"github.com/golang/protobuf/jsonpb"
"github.com/octavore/nagax/util/errors"
git "gopkg.in/src-d/go-git.v4"
"github.com/ketchuphq/ketchup/proto/ketchup/models"
)
type themeFile interface {
SetData(*string)
GetData() string
}
// readConfig unmarshals a theme.json file to a model.Theme.
// if there is no file, return an error.
func readConfig(themeConfigPath string) (*models.Theme, error) {
_, err := os.Stat(themeConfigPath)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.Wrap(err)
}
b, err := ioutil.ReadFile(themeConfigPath)
if err != nil {
return nil, errors.Wrap(err)
}
t := &models.Theme{
Templates: map[string]*models.ThemeTemplate{},
Assets: map[string]*models.ThemeAsset{},
}
err = jsonpb.Unmarshal(bytes.NewBuffer(b), t)
if err != nil {
return nil, errors.New("filestore: error parsing theme.json (malformed?)")
}
return t, nil
}
// themeIterator iterates over theme files stored in a models.Theme struct
func themeIterator(theme *models.Theme, iterFn func(fn string, el themeFile) error) error {
for fn, tmpl := range theme.Templates {
err := iterFn(path.Join(fileStoreTemplateDir, fn), tmpl)
if err != nil {
return errors.Wrap(err)
}
}
for fn, asset := range theme.Assets {
err := iterFn(path.Join(fileStoreAssetsDir, fn), asset)
if err != nil {
return errors.Wrap(err)
}
}
return nil
}
func isVCS(repoPath string) (bool, error) {
_, err := git.PlainOpen(repoPath)
if err == git.ErrRepositoryNotExists {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// getCurrentRef returns the current ref for repo at repoPath
func getCurrentRef(repoPath string) (string, error) {
repo, err := git.PlainOpen(repoPath)
if err != nil {
return "", errors.Wrap(err)
}
head, err := repo.Head()
if err != nil {
return "", errors.Wrap(err)
}
return head.Hash().String(), nil
}
func themeNameFromPath(p string) string {
dir, _ := path.Split(p)
return path.Base(dir)
}