-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
227 lines (188 loc) · 4.53 KB
/
files.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package files
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/fatih/color"
"gopkg.in/yaml.v2"
)
type FileDir interface {
Describe() string
DescribeSelf() string
PlainName() string
TabcompleteName() string
TryCD() (error, *FilesystemDir)
TryCat() (error, string)
Path() string
}
type FilesystemFile struct {
Name string `yaml:"name"`
Content string `yaml:"content"`
Parent *FilesystemDir `yaml:"-"`
}
func (f FilesystemFile) PlainName() string {
return f.Name
}
func (f FilesystemFile) TabcompleteName() string {
return f.Name + " "
}
func (f FilesystemFile) Describe() string {
return f.DescribeSelf()
}
func (f FilesystemFile) DescribeSelf() string {
return f.Name
}
func (f FilesystemFile) Path() string {
return f.Parent.Path() + "/" + f.Name
}
func (f FilesystemFile) TryCD() (error, *FilesystemDir) {
return errors.New(fmt.Sprintf("bash: cd: %s: Not a directory", f.Name)), nil
}
func (f FilesystemFile) TryCat() (error, string) {
return nil, f.Content
}
type FilesystemDir struct {
Name string `yaml:"name"`
Permissions int `yaml:"permissions"`
Subdirs []*FilesystemDir `yaml:"subdirs"`
Files []*FilesystemFile `yaml:"files"`
Parent *FilesystemDir `yaml:"-"`
}
func (d FilesystemDir) Path() string {
return d.PathHelp(false)
}
func (d *FilesystemDir) PathHelp(belowRoot bool) string {
parent := d.Parent
if parent == d {
if belowRoot {
return ""
} else {
return "/"
}
}
return parent.PathHelp(true) + "/" + d.Name
}
func (d *FilesystemDir) GetSubdir(name string) (error, *FilesystemDir) {
if name == "" {
return nil, d
}
if name == "." {
return nil, d
}
if name == ".." {
return nil, d.Parent
}
for _, subdir := range d.Subdirs {
if subdir.Name == name {
return nil, subdir
}
}
return errors.New("No subdir with name " + name), nil
}
func (d *FilesystemDir) GetFile(name string) (error, *FilesystemFile) {
for _, file := range d.Files {
if file.Name == name {
return nil, file
}
}
return errors.New("No file with name " + name), nil
}
func (d FilesystemDir) PlainName() string {
return d.Name
}
func (d FilesystemDir) TabcompleteName() string {
return d.Name + "/"
}
func (d FilesystemDir) DescribeSelf() string {
boldBlue := color.New(color.FgBlue, color.Bold)
return boldBlue.Sprint(d.Name)
}
func (d FilesystemDir) Describe() string {
var thingsToDescribe []FileDir
for _, file := range d.Files {
thingsToDescribe = append(thingsToDescribe, file)
}
for _, dir := range d.Subdirs {
thingsToDescribe = append(thingsToDescribe, dir)
}
sort.Slice(thingsToDescribe, func(i, j int) bool {
return thingsToDescribe[i].PlainName() < thingsToDescribe[j].PlainName()
})
ret := ""
for _, d := range thingsToDescribe {
ret += d.DescribeSelf() + " "
}
return ret
}
func (d *FilesystemDir) TryCD() (error, *FilesystemDir) {
return nil, d
}
func (d FilesystemDir) TryCat() (error, string) {
return errors.New(fmt.Sprintf("cat: %s: Is a directory", d.Name)), ""
}
type FilesystemConfig struct {
Root *FilesystemDir `json:"root"`
}
func fillInParents(root *FilesystemDir) {
for _, subdir := range root.Subdirs {
subdir.Parent = root
fillInParents(subdir)
}
for _, file := range root.Files {
file.Parent = root
}
}
func (cfg *FilesystemConfig) ToString() (string, error) {
data, err := yaml.Marshal(cfg)
return string(data), err
}
func StrToFilesystem(cfg []byte) FilesystemConfig {
ret := &FilesystemConfig{}
yaml.Unmarshal(cfg, ret)
ret.Root.Parent = ret.Root
fillInParents(ret.Root)
return *ret
}
func (cwd *FilesystemDir) GetFileOrDir(root *FilesystemDir, path string) (error, FileDir) {
if path == "" {
return nil, cwd
}
pathParts := strings.Split(path, "/")
lenParts := len(pathParts)
leadingSlash := pathParts[0] == ""
trailingSlash := pathParts[lenParts-1] == ""
if lenParts == 0 {
return nil, cwd
}
var currentDir *FilesystemDir = cwd
if leadingSlash {
currentDir = root
}
for i, part := range pathParts {
if part == "" {
continue
}
lastSegment := i == lenParts-1
dirErr, dirRes := currentDir.GetSubdir(part)
if dirErr == nil {
currentDir = dirRes
continue
}
if (lastSegment && trailingSlash) || !lastSegment {
// must be a dir only
if dirErr != nil {
return dirErr, nil
}
currentDir = dirRes
continue
}
// could be a file...
fileErr, fileRes := currentDir.GetFile(part)
if fileErr != nil {
return errors.New(fmt.Sprintf("cannot access '%s': No such file or directory", path)), nil
}
return nil, fileRes
}
return nil, currentDir
}