forked from adyatlov/bun-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.go
180 lines (167 loc) · 3.99 KB
/
directory.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
package bun
import (
"bufio"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
// DirType represent different types of the hosts.
type DirType string
const (
// DTRoot is a bundle root directory
DTRoot DirType = "root"
// DTMaster directory
DTMaster = "master"
// DTAgent direrctory
DTAgent = "agent"
// DTPublicAgent directory
DTPublicAgent = "public agent"
)
type directory struct {
Path string
Type DirType
}
type bulkCloser []io.Closer
func (bc bulkCloser) Close() error {
e := []string{}
for _, c := range bc {
if err := c.Close(); err != nil {
e = append(e, err.Error())
}
}
if len(e) > 0 {
return errors.New(strings.Join(e, "\n"))
}
return nil
}
type namer string
func (name namer) Name() string {
return string(name)
}
// File is a safe way to access bundle files.
type File interface {
io.ReadCloser
Name() string
}
// OpenFile opens the files of the typeName file type.
// If the file is not found, it tries to open it from a correspondent .gzip archive.
// If the .gzip archive is not found as well then returns an error.
// Caller is responsible for closing the file.
func (d directory) OpenFile(typeName string) (File, error) {
fileType := GetFileType(typeName)
ok := false
for _, dirType := range fileType.DirTypes {
if dirType == d.Type {
ok = true
break
}
}
if !ok {
panic(fmt.Sprintf("%v files do not belong to %v hosts", fileType.Name,
d.Type))
}
notFound := []string{}
for _, localPath := range fileType.Paths {
filePath := path.Join(d.Path, localPath)
file, err := os.Open(filePath)
if err == nil {
return file, nil // found
}
if !os.IsNotExist(err) {
return nil, err // error
}
// not found
notFound = append(notFound, filePath)
// try to open correspondent .gz file
filePath += ".gz"
file, err = os.Open(filePath)
if err != nil {
if !os.IsNotExist(err) {
return nil, err // error
}
notFound = append(notFound, filePath)
continue // not found
}
// found
r, err := gzip.NewReader(file)
if err != nil {
return nil, err // error
}
return struct {
io.Reader
io.Closer
namer
}{io.Reader(r), bulkCloser{r, file}, namer(filePath)}, nil
}
return nil, fmt.Errorf("none of the following files are found:\n%v",
strings.Join(notFound, "\n"))
}
// ReadJSON reads JSON-encoded data from the bundle file and stores the result in
// the value pointed to by v.
func (d directory) ReadJSON(typeName string, v interface{}) error {
fileType := GetFileType(typeName)
if fileType.ContentType != CTJson {
panic(fmt.Sprintf("Content of the %v file is not JSON", typeName))
}
file, err := d.OpenFile(typeName)
if err != nil {
return err
}
defer func() {
if err := file.Close(); err != nil {
log.Printf("bun.directory.ReadJSON: Cannot close file: %v", err)
}
}()
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
// FindLine returns a number and a content of the first line in a file of
// a type t which contains a substring s.
// If the line is not found, n == 0.
func (d directory) FindLine(t string, s string) (n int, l string, err error) {
var file File
file, err = d.OpenFile(t)
if err != nil {
return
}
defer func() {
if err := file.Close(); err != nil {
log.Printf("bun.directory.FindLine: Cannot close file %v with error: %v",
file.Name(), err)
if strings.HasSuffix(file.Name(), ".gz") {
log.Printf("The .gz file might be corrupted. Try to fix it with"+
" the gzrecover command and run the check again:\n"+
"1) brew install gzrt\n"+
"2) gzrecover -o %v %v", strings.TrimSuffix(file.Name(), ".gz"),
file.Name())
}
}
}()
return findLine(file, s)
}
func findLine(r io.Reader, substr string) (n int, l string, err error) {
scanner := bufio.NewScanner(r)
for i := 1; scanner.Scan(); i++ {
line := scanner.Text()
if strings.Contains(line, substr) {
l = line
n = i
return
}
}
if err = scanner.Err(); err != nil {
return
}
// Not found
return
}