forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
176 lines (153 loc) · 4.54 KB
/
helper.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
package astilectron
import (
"bytes"
"context"
"net/http"
"os"
"path/filepath"
"github.com/asticode/go-astilog"
"github.com/asticode/go-astitools/archive"
"github.com/asticode/go-astitools/context"
"github.com/asticode/go-astitools/http"
"github.com/asticode/go-astitools/io"
"github.com/pkg/errors"
)
// Download is a cancellable function that downloads a src into a dst using a specific *http.Client and cleans up on
// failed downloads
func Download(ctx context.Context, c *http.Client, src, dst string) (err error) {
// Log
astilog.Debugf("Downloading %s into %s", src, dst)
// Destination already exists
if _, err = os.Stat(dst); err == nil {
astilog.Debugf("%s already exists, skipping download...", dst)
return
} else if !os.IsNotExist(err) {
return errors.Wrapf(err, "stating %s failed", dst)
}
err = nil
// Clean up on error
defer func(err *error) {
if *err != nil || ctx.Err() != nil {
astilog.Debugf("Removing %s...", dst)
os.Remove(dst)
}
}(&err)
// Make sure the dst directory exists
if err = os.MkdirAll(filepath.Dir(dst), 0775); err != nil {
return errors.Wrapf(err, "mkdirall %s failed", filepath.Dir(dst))
}
// Download
if err = astihttp.Download(ctx, c, src, dst); err != nil {
return errors.Wrap(err, "astihttp.Download failed")
}
return
}
// Disembed is a cancellable disembed of an src to a dst using a custom Disembedder
func Disembed(ctx context.Context, d Disembedder, src, dst string) (err error) {
// Log
astilog.Debugf("Disembedding %s into %s...", src, dst)
// No need to disembed
if _, err = os.Stat(dst); err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "stating %s failed", dst)
} else if err == nil {
astilog.Debugf("%s already exists, skipping disembed...", dst)
return
}
err = nil
// Clean up on error
defer func(err *error) {
if *err != nil || ctx.Err() != nil {
astilog.Debugf("Removing %s...", dst)
os.Remove(dst)
}
}(&err)
// Make sure directory exists
var dirPath = filepath.Dir(dst)
astilog.Debugf("Creating %s", dirPath)
if err = os.MkdirAll(dirPath, 0755); err != nil {
return errors.Wrapf(err, "mkdirall %s failed", dirPath)
}
// Create dst
var f *os.File
astilog.Debugf("Creating %s", dst)
if f, err = os.Create(dst); err != nil {
return errors.Wrapf(err, "creating %s failed", dst)
}
defer f.Close()
// Disembed
var b []byte
astilog.Debugf("Disembedding %s", src)
if b, err = d(src); err != nil {
return errors.Wrapf(err, "disembedding %s failed", src)
}
// Copy
astilog.Debugf("Copying disembedded data to %s", dst)
if _, err = astiio.Copy(ctx, bytes.NewReader(b), f); err != nil {
return errors.Wrapf(err, "copying disembedded data into %s failed", dst)
}
return
}
// Unzip unzips a src into a dst.
// Possible src formats are /path/to/zip.zip or /path/to/zip.zip/internal/path.
func Unzip(ctx context.Context, src, dst string) (err error) {
// Clean up on error
defer func(err *error) {
if *err != nil || ctx.Err() != nil {
astilog.Debugf("Removing %s...", dst)
os.RemoveAll(dst)
}
}(&err)
// Unzipping
astilog.Debugf("Unzipping %s into %s", src, dst)
if err = astiarchive.Unzip(ctx, src, dst); err != nil {
err = errors.Wrapf(err, "unzipping %s into %s failed", src, dst)
return
}
return
}
// PtrBool transforms a bool into a *bool
func PtrBool(i bool) *bool {
return &i
}
// PtrFloat transforms a float64 into a *float64
func PtrFloat(i float64) *float64 {
return &i
}
// PtrInt transforms an int into an *int
func PtrInt(i int) *int {
return &i
}
// PtrInt64 transforms an int64 into an *int64
func PtrInt64(i int64) *int64 {
return &i
}
// PtrStr transforms a string into a *string
func PtrStr(i string) *string {
return &i
}
// synchronousFunc executes a function, blocks until it has received a specific event or the canceller has been
// cancelled and returns the corresponding event
func synchronousFunc(c *asticontext.Canceller, l listenable, fn func(), eventNameDone string) (e Event) {
var ctx, cancel = c.NewContext()
defer cancel()
l.On(eventNameDone, func(i Event) (deleteListener bool) {
e = i
cancel()
return true
})
fn()
<-ctx.Done()
return
}
// synchronousEvent sends an event, blocks until it has received a specific event or the canceller has been cancelled
// and returns the corresponding event
func synchronousEvent(c *asticontext.Canceller, l listenable, w *writer, i Event, eventNameDone string) (o Event, err error) {
o = synchronousFunc(c, l, func() {
if err = w.write(i); err != nil {
err = errors.Wrapf(err, "writing %+v event failed", i)
return
}
return
}, eventNameDone)
return
}