forked from rillig/gobco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
108 lines (93 loc) · 1.64 KB
/
util.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
package main
import (
"crypto/rand"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func copyDir(src string, dst string) error {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
err := os.MkdirAll(dst, 0o777)
if err != nil {
return err
}
action := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() {
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
dstPath := filepath.Join(dst, rel)
err = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
if err == nil {
err = copyFile(path, dstPath)
}
}
return err
}
return filepath.Walk(src, action)
}
func copyFile(src string, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer func() {
closeErr := in.Close()
if err == nil {
err = closeErr
}
}()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
closeErr := out.Close()
if err == nil {
err = closeErr
}
}()
_, err = io.Copy(out, in)
return
}
func randomHex(n int) string {
rnd := make([]byte, n)
_, err := io.ReadFull(rand.Reader, rnd[:])
if err != nil {
panic(err)
}
return fmt.Sprintf("%x", rnd)
}
type sliceFlag struct {
values *[]string
}
func newSliceFlag(values *[]string) *sliceFlag {
return &sliceFlag{values}
}
func (s *sliceFlag) String() string {
if s.values == nil {
return ""
}
return strings.Join(*s.values, ", ")
}
func (s *sliceFlag) Set(str string) error {
*s.values = append(*s.values, str)
return nil
}
func ok(err error) {
if err != nil {
panic(err)
}
}
func assert(cond bool, msg string) {
if !cond {
panic(msg)
}
}