-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.go
68 lines (60 loc) · 1.05 KB
/
functions.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
package oreka
import (
"runtime"
"fmt"
"github.com/fatih/color"
"bufio"
"os"
"io"
"crypto/md5"
"encoding/hex"
)
func MD5(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
func Check(e error) {
if e != nil {
panic(e)
}
}
func Die(msg string, e error) {
if runtime.GOOS == "windows" {
fmt.Println("ERROR:", msg)
} else {
fmt.Println(color.RedString(msg))
}
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
Check(e)
os.Exit(1)
}
type deleteCloser struct {
io.ReadCloser
path string
f *os.File
}
func (d *deleteCloser) Close() error {
err := d.ReadCloser.Close()
if err != nil {
return err
}
err = os.Remove(d.path)
if err != nil {
fmt.Println("ERROR DeleteOnCloseReader", d.path)
return err
}
return nil
}
func (d *deleteCloser) Size() int64 {
fi, _ := d.f.Stat()
return fi.Size()
}
func DeleteOnCloseReader(path string) (*deleteCloser, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
return &deleteCloser{f, path, f}, nil
}