-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
153 lines (135 loc) · 3.39 KB
/
helpers.go
File metadata and controls
153 lines (135 loc) · 3.39 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
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
package main
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
// ListDirs recursively walks through 'dir' and returns a slice of all directories
func ListDirs(dir string) ([]string, error) {
var dirs []string
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("[DataHub.main.ListDirs] os.ReadDir error: %w", err)
}
for _, entry := range entries {
if entry.IsDir() {
dirs = append(dirs, entry.Name())
}
}
return dirs, nil
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return fmt.Errorf("[DataHub.main.copyFile] os.Open error: %w", err)
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return fmt.Errorf("[DataHub.main.copyFile] os.Create error: %w", err)
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return fmt.Errorf("[DataHub.main.copyFile] io.Copy error: %w", err)
}
return nil
}
func extractZIP(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return fmt.Errorf("[DataHub.main.extractZIP] zip.OpenReader error: %w", err)
}
defer r.Close()
for _, f := range r.File {
fpath := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, 0755)
continue
}
if err := os.MkdirAll(filepath.Dir(fpath), 0755); err != nil {
return fmt.Errorf("[DataHub.main.extractZIP] os.MkdirAll error: %w", err)
}
outFile, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("[DataHub.main.extractZIP] os.Create error: %w", err)
}
rc, err := f.Open()
if err != nil {
outFile.Close()
return fmt.Errorf("[DataHub.main.extractZIP] f.Open error: %w", err)
}
if _, err = io.Copy(outFile, rc); err != nil {
outFile.Close()
rc.Close()
return fmt.Errorf("[DataHub.main.extractZIP] io.Copy error: %w", err)
}
outFile.Close()
rc.Close()
}
return nil
}
func extractTAR(src, dest string) error {
f, err := os.Open(src)
if err != nil {
return fmt.Errorf("[DataHub.main.extractTAR] os.Open error: %w", err)
}
defer f.Close()
return untar(f, dest)
}
func extractTARGZ(src, dest string) error {
f, err := os.Open(src)
if err != nil {
return fmt.Errorf("[DataHub.main.extractTARGZ] os.Open error: %w", err)
}
defer f.Close()
gzr, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("[DataHub.main.extractTARGZ] gzip.NewReader error: %w", err)
}
defer gzr.Close()
return untar(gzr, dest)
}
func untar(r io.Reader, dest string) error {
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("[DataHub.main.untar] tr.Next error: %w", err)
}
fpath := filepath.Join(dest, hdr.Name)
switch hdr.Typeflag {
case tar.TypeDir:
os.MkdirAll(fpath, 0755)
case tar.TypeReg:
os.MkdirAll(filepath.Dir(fpath), 0755)
outFile, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("[DataHub.main.untar] os.Create error: %w", err)
}
if _, err = io.Copy(outFile, tr); err != nil {
outFile.Close()
return fmt.Errorf("[DataHub.main.untar] io.Copy error: %w", err)
}
outFile.Close()
}
}
return nil
}
func errMissingParam(param string) error {
return ¶mError{msg: "missing parameter: " + param}
}
func errNotFoundDir(dir string) error {
return ¶mError{msg: "directory not found: " + dir}
}
type paramError struct {
msg string
}
func (e *paramError) Error() string { return e.msg }