/
file.go
216 lines (168 loc) · 4.46 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
package file
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/nanobox-io/nanobox/config"
)
// Tar takes a source and variable writers and walks 'source' writing each file
// found to the tar writer; the purpose for accepting multiple writers is to allow
// for multiple outputs (for example a file, or md5 hash)
func Tar(src string, writers ...io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(src); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}
mw := io.MultiWriter(writers...)
gzw := gzip.NewWriter(mw)
defer gzw.Close()
tw := tar.NewWriter(gzw)
defer tw.Close()
// walk path
return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
// return on any error
if err != nil {
return err
}
// create a new dir/file header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}
// update the name to correctly reflect the desired destination when untaring
header.Name = strings.TrimPrefix(strings.Replace(file, src, "", -1), string(filepath.Separator))
// write the header
if err := tw.WriteHeader(header); err != nil {
return err
}
// return on directories since there will be no content to tar
if fi.Mode().IsDir() {
return nil
}
// open files for taring
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
return err
}
return nil
})
}
// Untar takes a destination path and a reader; a tar reader loops over the tarfile
// creating the file structure at 'dst' along the way, and writing any files
func Untar(dst string, r io.Reader) error {
gzr, err := gzip.NewReader(r)
defer gzr.Close()
if err != nil {
return err
}
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)
// the following switch could also be done using fi.Mode(), not sure if there
// a benefit of using one vs. the other.
// fi := header.FileInfo()
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
// if it's a file create it
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
defer f.Close()
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
return err
}
}
}
}
// Download downloads a file
func Download(path string, w io.Writer) error {
res, err := http.Get(path)
if err != nil {
return err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
config.Fatal("[util/file/file] ioutil.ReadAll() failed", err.Error())
}
w.Write(b)
return nil
}
// Progress downloads a file with a fancy progress bar
func Progress(path string, w io.Writer) error {
//
download, err := http.Get(path)
if err != nil {
return err
}
defer download.Body.Close()
var percent float64
var down int
// format the response content length to be more 'friendly'
total := float64(download.ContentLength) / math.Pow(1024, 2)
// create a 'buffer' to read into
b := make([]byte, 2048)
//
for {
// read the response body (streaming)
n, err := download.Body.Read(b)
// write the contents of our buffer to the writer
w.Write(b[:n])
// update the total bytes read
down += n
switch {
default:
// update the percent downloaded
percent = (float64(down) / float64(download.ContentLength)) * 100
// show download progress: 0.0/0.0MB [*** progress *** 0.0%]
fmt.Printf("\r %.2f/%.2fMB [%-41s %.2f%%]", float64(down)/math.Pow(1024, 2), total, strings.Repeat("*", int(percent/2.5)), percent)
// if no more files are found return
case download.ContentLength < 1:
fmt.Printf("\r %.2fMB", float64(down)/math.Pow(1024, 2))
}
// detect EOF and break the 'stream'
if err != nil {
if err == io.EOF {
fmt.Println("")
break
} else {
return err
}
}
}
return nil
}