-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileMap.go
More file actions
109 lines (92 loc) · 2.79 KB
/
fileMap.go
File metadata and controls
109 lines (92 loc) · 2.79 KB
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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
)
// FileMap is structure that hold (relative) names of files with their content.
type FileMap struct {
// files is a map where keys are relative pathes (wit skipped `skipPath`
// part). Values are raw file content.
files map[string]string
// skipPath is used to omit part of loaded file path
skipPath string
// excludedExt stores extentions witch mustn't be loaded. Changing this in
// runtime has no effect on already loaded files.
excludedExt []string
}
// init creates FileMap. Allocates map.
func (p *FileMap) init(skipPath string, excludedExt []string) {
p.files = make(map[string]string)
p.skipPath = skipPath
p.excludedExt = excludedExt
}
// load loads file from given `path` and `extention`.
// Returns content of file as string. Error is returned if load weren't
// successfull OR given extention should be skipped.
func (p *FileMap) load(path string, ext string) (string, error) {
for _, val := range p.excludedExt {
if ext == val {
err := StringError{"INFO: Skipped loading: \"" + path + ext + "\"."}
return "", err
}
}
dat, err := ioutil.ReadFile(path + ext)
if err != nil {
fmt.Printf(err.Error())
err = StringError{"ERROR: Can't load file \"" + path + ext + "\"!"}
return "", err
}
content := string(dat)
// XXX clear that
p.files[path[len(p.skipPath):]] = content
return content, nil
}
// loadFromFullPath loads file from full path (relative to executable).
// For details see `load`.
func (p *FileMap) loadFromFullPath(path string) (string, error) {
base, ext := getBaseAndExt(path)
return p.load(base, ext)
}
// get returns content of file with given path. Note that `skipPath` shouldn't
// be passed. Returns content of file. Error is returned if no file were loaded
// (or were skipped).
func (p FileMap) get(path string) (string, error) {
val, ok := p.files[path]
if ok {
return val, nil
} else {
err := StringError{s: "WARNING: No file named \"" +
path + "\" was loaded."}
return "", err
}
}
// loadFilesRecursively loads every file from `cwd`, and subdirectories.
// Returns error if `cwd` cant' be opened. Sub-errors about files and
// subdirectories are ignored.
func (p *FileMap) loadFilesRecursively(cwd string) error {
fileList, err := ioutil.ReadDir(cwd)
if err != nil {
err = StringError{s: "ERROR: Can't open \"" + cwd + "\" directory!"}
return err
}
for _, f := range fileList {
fileName := f.Name()
if f.IsDir() {
err := p.loadFilesRecursively(cwd + fileName + "/")
if err != nil {
fmt.Println(err.Error())
continue
}
} else {
baseName, ext := getBaseAndExt(fileName)
_, err := p.load(cwd+baseName, ext)
if err != nil {
fmt.Println(err.Error())
continue
}
fmt.Println("INFO: Loaded file: " + cwd + filepath.Base(fileName))
}
}
return nil
}