This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_walker.go
165 lines (143 loc) · 3.76 KB
/
tree_walker.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 loader
import (
"fmt"
"github.com/golang/glog"
"k8s.io/kops/util/pkg/vfs"
"os"
"path"
"strings"
)
type TreeWalker struct {
Contexts map[string]Handler
Extensions map[string]Handler
DefaultHandler Handler
Tags map[string]struct{}
}
type TreeWalkItem struct {
Context string
Name string
Path vfs.Path
RelativePath string
Meta string
Tags []string
}
func (i *TreeWalkItem) ReadString() (string, error) {
b, err := i.ReadBytes()
if err != nil {
return "", err
}
return string(b), nil
}
func (i *TreeWalkItem) ReadBytes() ([]byte, error) {
b, err := i.Path.ReadFile()
if err != nil {
return nil, fmt.Errorf("error reading file %q: %v", i.Path, err)
}
return b, nil
}
type Handler func(item *TreeWalkItem) error
func IsTag(name string) bool {
return len(name) != 0 && name[0] == '_'
}
func (t *TreeWalker) Walk(basedir vfs.Path) error {
i := &TreeWalkItem{
Context: "",
Path: basedir,
RelativePath: "",
Tags: nil,
}
return t.walkDirectory(i)
}
func (t *TreeWalker) walkDirectory(parent *TreeWalkItem) error {
files, err := parent.Path.ReadDir()
if err != nil {
return fmt.Errorf("error reading directory %q: %v", parent.Path, err)
}
for _, f := range files {
var err error
fileName := f.Base()
i := &TreeWalkItem{
Context: parent.Context,
Path: f,
RelativePath: path.Join(parent.RelativePath, fileName),
Name: fileName,
Tags: parent.Tags,
}
glog.V(4).Infof("visit %q", f)
hasMeta := false
{
metaPath := parent.Path.Join(fileName + ".meta")
metaBytes, err := metaPath.ReadFile()
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("error reading file %q: %v", metaPath, err)
}
metaBytes = nil
}
if metaBytes != nil {
hasMeta = true
i.Meta = string(metaBytes)
}
}
if _, err := f.ReadDir(); err == nil {
if IsTag(fileName) {
// Only descend into the tag directory if we have the tag
_, found := t.Tags[fileName]
if !found {
glog.V(2).Infof("Skipping directory as tag not present: %q", f)
continue
} else {
i.Tags = append(i.Tags, fileName)
glog.V(2).Infof("Descending into directory, as tag is present: %q", f)
err = t.walkDirectory(i)
}
} else if _, found := t.Contexts[fileName]; found {
// Entering a new context (mode of operation)
if parent.Context != "" {
return fmt.Errorf("found context %q inside context %q at %q", fileName, parent.Context, f)
}
i.Context = fileName
i.RelativePath = ""
err = t.walkDirectory(i)
} else {
// Simple directory for organization / structure
err = t.walkDirectory(i)
}
if err != nil {
return err
}
// So that we can manage directories, we do not ignore directories which have a .meta file
if hasMeta {
glog.V(4).Infof("Found .meta file for directory %q; will process", f)
} else {
continue
}
}
if strings.HasSuffix(fileName, ".meta") {
// We'll read it when we see the actual file
// But check the actual file is there
primaryPath := strings.TrimSuffix(f.Base(), ".meta")
if _, err := parent.Path.Join(primaryPath).ReadFile(); os.IsNotExist(err) {
return fmt.Errorf("found .meta file without corresponding file: %q", f)
}
continue
}
var handler Handler
if i.Context != "" {
handler = t.Contexts[i.Context]
} else {
// TODO: Just remove extensions.... we barely use them!
// (or remove default handler and replace with lots of small files?)
extension := path.Ext(fileName)
handler = t.Extensions[extension]
if handler == nil {
handler = t.DefaultHandler
}
}
err = handler(i)
if err != nil {
return fmt.Errorf("error handling file %q: %v", f, err)
}
}
return nil
}