-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utils.go
More file actions
98 lines (73 loc) · 2.17 KB
/
Copy pathfile_utils.go
File metadata and controls
98 lines (73 loc) · 2.17 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
package main
import (
"bufio"
"os"
"regexp"
)
func openFile(filePath string, errorCode int, optional bool) *os.File {
file, err := os.Open(filePath)
if err != nil {
if optional && os.IsNotExist(err) {
return nil
}
Error(errorCode, filePath, err)
}
return file
}
func createFile(filePath string, errorCode int) *os.File {
file, err := os.Create(filePath)
if err != nil {
Error(errorCode, filePath, err)
}
return file
}
// OpenTaskfile opens a taskfile for reading and handles errors.
func OpenTaskfile(filePath string, optional bool) (taskfile *os.File) {
return openFile(filePath, ErrTaskfileOpen, optional)
}
// CreateTaskfile opens a taskfile for writing and handles errors.
func CreateTaskfile(filePath string) (taskfile *os.File) {
return createFile(filePath, ErrTaskfileOpen)
}
// OpenSyncfile opens a syncfile for reading and handles errors.
func OpenSyncfile(optional bool) (syncfile *os.File) {
return openFile(SyncfilePath, ErrSyncfileOpen, optional)
}
// CreateSyncfile opens a syncfile for writing and handles errors.
func CreateSyncfile() (syncfile *os.File) {
return createFile(SyncfilePath, ErrSyncfileOpen)
}
// ReplaceOrAppendSyncfileLine essentialy makes sure that an "entry" appears in a
// syncfile exactly once.
func ReplaceOrAppendSyncfileLine(pattern *regexp.Regexp, repl string) {
// Read Syncfile contents into an array, skipping lines that match the
// pattern.
syncfile, err := os.OpenFile(SyncfilePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
Error(ErrSyncfileOpen, SyncfilePath, err)
}
defer syncfile.Close()
var contents []byte
scanner := bufio.NewScanner(syncfile)
for scanner.Scan() {
line := scanner.Text()
if !pattern.MatchString(line) {
contents = append(contents, line+"\n"...)
}
}
if err := scanner.Err(); err != nil {
Error(ErrSyncfileRead, SyncfilePath, err)
}
// Add the replacement to the end of the list and write the array back into
// the file.
contents = append(contents, repl...)
err = syncfile.Truncate(0)
if err != nil {
Error(ErrSyncfileWrite, SyncfilePath, err)
}
syncfile.Seek(0, 0)
_, err = syncfile.Write(contents)
if err != nil {
Error(ErrSyncfileWrite, SyncfilePath, err)
}
}