-
Notifications
You must be signed in to change notification settings - Fork 21
/
files.go
197 lines (164 loc) · 4.55 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
package helper
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"io"
"net/http"
"os"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
"github.com/awslabs/tecli/box"
)
// BuildPath changes the given path to a more cross platform friendly format
func BuildPath(path string) string {
sep := string(os.PathSeparator)
return strings.ReplaceAll(path, "/", sep)
}
// WriteFile writes a file and return true if successful
func WriteFile(filename string, data []byte) bool {
err := ioutil.WriteFile(filename, data, os.ModePerm)
if err != nil {
logrus.Fatal(err)
return false
}
return true
}
// WriteFileFromBox get the file from box's resources and write into the given destination, returns false if not able to.
func WriteFileFromBox(source string, dest string) bool {
bytes, found := box.Get(BuildPath(source))
if !found {
logrus.Errorf("file \"%s\" not found under box/resources", BuildPath(source))
return false
}
return WriteFile(BuildPath(dest), bytes)
}
// WriteInterfaceToFile write the given interface into a file
func WriteInterfaceToFile(in interface{}, path string) error {
b, err := yaml.Marshal(&in)
if err != nil {
_, ok := err.(*json.UnsupportedTypeError)
if ok {
return fmt.Errorf("json unsupported type error")
}
}
err = ioutil.WriteFile(path, b, os.ModePerm)
if err != nil {
return fmt.Errorf("unable to update:%s\n%v", path, err)
}
return err
}
// DownloadFileTo downloads a file and saves into the given directory with the given file name
func DownloadFileTo(url string, destination string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(BuildPath(destination))
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
// DownloadFile downloads a file and saves into the given directory with the given file name
func DownloadFile(url string, dirPath string, filename string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(dirPath + "/" + filename)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
// FileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func FileExists(path string) bool {
info, err := os.Stat(BuildPath(path))
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// DirOrFileExists an error is known to report that a file or directory does not exist.
// It is satisfied by ErrNotExist as well as some syscall errors.
func DirOrFileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// CopyFile copies a file from source into a given destination path
// https://github.com/mactsouk/opensource.com/blob/master/cp2.go
func CopyFile(sourceFile string, destinationFile string) {
input, err := ioutil.ReadFile(sourceFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = ioutil.WriteFile(destinationFile, input, 0644)
if err != nil {
fmt.Println("Error creating", destinationFile)
fmt.Println(err)
os.Exit(1)
}
}
// CopyFileTo copy a file from sourceFile location to destinationFile location
func CopyFileTo(sourceFile string, destinationFile string) error {
input, err := ioutil.ReadFile(sourceFile)
if err != nil {
logrus.Errorf("unable to copy file\n%v\n", err)
return err
}
err = ioutil.WriteFile(destinationFile, input, 0644)
if err != nil {
logrus.Errorf("unable to write file\n%v\n", err)
return err
}
return nil
}
// FileSize return the size of the give file path.
// Gives an error if files does not exist
func FileSize(path string) (int64, error) {
var size int64 = -1
if FileExists(path) {
info, err := os.Stat(path)
if err != nil {
if err != nil {
return size, fmt.Errorf("unable to obtain information about file: %s\n%s", path, err)
}
return size, err
}
size = info.Size()
} else {
return size, fmt.Errorf("file does not exist")
}
return size, nil
}
// ListFiles list of all file names in the given directory. Pass "." if you want to list at the current directory.
func ListFiles(dir string) []os.FileInfo {
files, err := ioutil.ReadDir(dir)
if err != nil {
logrus.Fatal(err)
}
return files
}
// DeleteFile removes the named file or (empty) directory.
// If there is an error, it will be of type *PathError.
func DeleteFile(name string) error {
return os.Remove(name)
}