forked from gen0cide/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
201 lines (186 loc) · 4.98 KB
/
file.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
package file
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
// WriteFileFromBytes writes data from a byte array to a dest filepath with the dest parent dirs permissions.
func WriteFileFromBytes(destPath string, fileData []byte) error {
absDir, err := filepath.Abs(filepath.Dir(destPath))
if err != nil {
return err
}
dirInfo, err := os.Stat(absDir)
if err != nil {
return err
}
err = ioutil.WriteFile(destPath, fileData, os.FileMode(dirInfo.Mode()))
if err != nil {
return err
}
return nil
}
// WriteFileFromString writes data from a string to a dest filepath with the dest parent dirs permissions.
func WriteFileFromString(destPath string, fileData string) error {
absDir, err := filepath.Abs(filepath.Dir(destPath))
if err != nil {
return err
}
dirInfo, err := os.Stat(absDir)
if err != nil {
return err
}
err = ioutil.WriteFile(destPath, []byte(fileData), os.FileMode(dirInfo.Mode()))
if err != nil {
return err
}
return nil
}
//ReadFileAsString takes a file path and reads that files contents and returns a string representation of the contents
func ReadFileAsString(readPath string) (string, error) {
absPath, err := filepath.Abs(readPath)
if err != nil {
return "", err
}
contents, err := ioutil.ReadFile(absPath)
if err != nil {
return "", err
}
return string(contents), nil
}
//ReadFileAsBytes takes a file path and reads that files contents and returns a byte array of the contents
func ReadFileAsBytes(readPath string) ([]byte, error) {
absPath, err := filepath.Abs(readPath)
if err != nil {
return nil, err
}
contents, err := ioutil.ReadFile(absPath)
if err != nil {
return nil, err
}
return contents, nil
}
// CopyFile copies a file from the src to the dest with the original files permissions
func CopyFile(srcPath, destPath string) (int, error) {
srcInfo, err := os.Stat(srcPath)
if err != nil {
return 0, err
}
from, err := os.Open(srcPath)
if err != nil {
return 0, err
}
defer from.Close()
to, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE, os.FileMode((srcInfo.Mode())))
if err != nil {
return 0, err
}
defer to.Close()
dataCopied, err := io.Copy(to, from)
if err != nil {
return 0, err
}
return int(dataCopied), nil
}
// AppendFileBytes takes a file and adds a byte array to the end of it
func AppendFileBytes(targetPath string, addData []byte) error {
fileInfo, err := os.Stat(targetPath)
if err != nil {
return err
}
absPath, err := filepath.Abs(targetPath)
if err != nil {
return err
}
targetFile, err := os.OpenFile(absPath, os.O_APPEND|os.O_WRONLY, fileInfo.Mode())
if err != nil {
return err
}
if _, err = targetFile.Write(addData); err != nil {
return err
}
targetFile.Close()
return nil
}
// AppendFileString takes a file and adds a string to the end of it
func AppendFileString(targetPath, addString string) error {
fileInfo, err := os.Stat(targetPath)
if err != nil {
return err
}
absPath, err := filepath.Abs(targetPath)
if err != nil {
return err
}
targetFile, err := os.OpenFile(absPath, os.O_APPEND|os.O_WRONLY, fileInfo.Mode())
if err != nil {
return err
}
if _, err = targetFile.WriteString(addString); err != nil {
return err
}
targetFile.Close()
return nil
}
//ReplaceInFileWithString searches a file for a string and replaces each instance found of that string. Returns the amount of strings replaced
func ReplaceInFileWithString(file, match, replacement string) (int, error) {
fileInfo, err := os.Stat(file)
if os.IsNotExist(err) {
return 0, err
}
contents, err := ioutil.ReadFile(file)
if err != nil {
return 0, err
}
var count int
lines := strings.Split(string(contents), "\n")
for index, line := range lines {
if strings.Contains(line, match) {
lines[index] = strings.Replace(line, match, replacement, 10)
count++
}
}
ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), fileInfo.Mode())
return count, nil
}
//ReplaceInFileWithRegex searches a file for a string and replaces each instance found of that string. Returns the amount of strings replaced
func ReplaceInFileWithRegex(file string, regexString string, replaceWith string) (int, error) {
re := regexp.MustCompile(regexString)
fileInfo, err := os.Stat(file)
if os.IsNotExist(err) {
return 0, err
}
contents, err := ioutil.ReadFile(file)
if err != nil {
return 0, err
}
var count int
lines := strings.Split(string(contents), "\n")
for index, line := range lines {
if re.MatchString(line) {
lines[index] = re.ReplaceAllString(line, replaceWith)
count++
}
}
ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), fileInfo.Mode())
return count, nil
}
//SetPerms changes the file permissions of a givin file
func SetPerms(targetPath string, perms int64) error {
err := os.Chmod(targetPath, os.FileMode(uint32(perms)))
if err != nil {
return err
}
return nil
}
//CheckExists takes a file or directory and checks to see if it exists on the file system
func CheckExists(targetPath string) bool {
_, err := os.Stat(targetPath)
if err == nil {
return true
}
return false
}