-
Notifications
You must be signed in to change notification settings - Fork 1
/
yarn_impl.go
165 lines (136 loc) · 3.2 KB
/
yarn_impl.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package yarn
import (
"fmt"
"path/filepath"
"strings"
)
// MissingYarn error format.
const MissingYarn = "Missing %s"
// Yarn is the implemention of yarn.Yarn
type yarn struct {
prefix string
yarns map[string]string
}
// NewFromMap creats a new yarn from provided map of kv.
// it is intend for use by other libraries, for an example
// see yarn/catalog.
func NewFromMap(files map[string]string) Yarn {
return &yarn{
prefix: "",
yarns: files,
}
}
// New returns a new Yarn
func newYarn() *yarn {
return &yarn{
prefix: "",
yarns: make(map[string]string),
}
}
// Add adds a string to the yarn.
// Add is not thread-safe, so it must be only
// used before handing over impl.Yarn as yarn.Yarn
// which has no Add.
func (y *yarn) add(key, value string) {
y.yarns[key] = value
}
// Has checks if a file for the provided list of keys exists, if not, returns an error.
func (y *yarn) Has(strings ...string) error {
var (
s string
ok bool
missing []string
)
for _, s = range strings {
if y.prefix != "" {
s = filepath.Join(y.prefix, s)
}
if _, ok = y.yarns[s]; !ok {
missing = append(missing, s)
}
}
if len(missing) > 0 {
return fmt.Errorf(MissingYarn, missing)
}
return nil
}
// MustHave is like Has but panics on missing keys.
func (y *yarn) MustHave(strings ...string) {
err := y.Has(strings...)
if err != nil {
panic(err.Error())
}
}
// Get returns a loaded file's contents as string and if it exists by filename.
func (y *yarn) Get(key string) (string, bool) {
if y.prefix != "" {
key = filepath.Join(y.prefix, key)
}
content, ok := y.yarns[key]
return content, ok
}
// Must returns a loaded file's contents as string, it panics if file doesn't exist.
func (y *yarn) Must(key string) string {
content, ok := y.yarns[key]
if !ok {
panic(fmt.Sprintf(MissingYarn, key))
}
return content
}
// List returns all the files.
func (y *yarn) List() []string {
files := []string{}
for path := range y.yarns {
// if no prefix or matching path prefix.
if y.prefix == "" || strings.HasPrefix(path, y.prefix) {
files = append(files, path)
}
}
return files
}
// All returns every file and content in the yarn.
func (y *yarn) All() map[string]string {
files := make(map[string]string, len(y.yarns))
for path, content := range y.yarns {
// if no prefix or matching path prefix.
if y.prefix == "" || strings.HasPrefix(path, y.prefix) {
files[path] = content
}
}
return files
}
// Sub creats a subview of the yarn only covering the provided
// directory.
func (y *yarn) Sub(path string) Yarn {
if y.prefix != "" {
path = filepath.Join(y.prefix, path)
}
return &yarn{
prefix: path,
yarns: y.yarns,
}
}
// Walk calls the provided function for each file.
// Exactly `**` matches any file.
func (y *yarn) Walk(pattern string, visitor func(path string, content string) error) error {
// we use all to take care of subviews.
if pattern == "**" {
for path, content := range y.All() {
err := visitor(path, content)
if err != nil {
return err
}
}
return nil
}
for path, content := range y.All() {
match, _ := filepath.Match(pattern, path)
if match {
err := visitor(path, content)
if err != nil {
return err
}
}
}
return nil
}