-
Notifications
You must be signed in to change notification settings - Fork 42
/
io.go
81 lines (70 loc) · 1.36 KB
/
io.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
package upx
import (
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/vbauerster/mpb/v8"
)
var (
IsVerbose = true
mu = &sync.Mutex{}
)
func NewFileWrappedWriter(localPath string, bar *mpb.Bar, resume bool) (io.WriteCloser, error) {
var fd *os.File
var err error
if resume {
fd, err = os.OpenFile(localPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0755)
} else {
fd, err = os.Create(localPath)
}
if err != nil {
return nil, err
}
fileinfo, err := fd.Stat()
if err != nil {
return nil, err
}
if bar == nil {
return fd, nil
}
bar.SetCurrent(fileinfo.Size())
return bar.ProxyWriter(fd), nil
}
func NewFileWrappedReader(bar *mpb.Bar, fd io.ReadCloser) io.ReadCloser {
if bar == nil {
return fd
}
return bar.ProxyReader(fd)
}
func Print(arg0 string, args ...interface{}) {
s := arg0 //arg0 may include '%'
if len(args) > 0 {
s = fmt.Sprintf(arg0, args...)
}
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
mu.Lock()
os.Stdout.WriteString(s)
mu.Unlock()
}
func PrintOnlyVerbose(arg0 string, args ...interface{}) {
if IsVerbose {
Print(arg0, args...)
}
}
func PrintError(arg0 string, args ...interface{}) {
s := fmt.Sprintf(arg0, args...)
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
mu.Lock()
os.Stderr.WriteString(s)
mu.Unlock()
}
func PrintErrorAndExit(arg0 string, args ...interface{}) {
PrintError(arg0, args...)
os.Exit(-1)
}