-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
167 lines (148 loc) · 3.54 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
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
package x
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"sync"
"golang.org/x/term"
)
func CopyFile(src, dst string) (int64, error) {
srcF, err := os.Open(src)
if err != nil {
return 0, err
}
defer srcF.Close()
st, err := srcF.Stat()
if err != nil {
return 0, err
}
dstF, err := os.Create(dst)
if err != nil {
return 0, err
}
defer dstF.Close()
written, err := io.Copy(dstF, srcF)
if err != nil {
return written, err
}
if err := dstF.Chmod(st.Mode()); err != nil {
return written, err
}
return written, dstF.Close()
}
// CopyDir copies the contents of src to dst
// This function will be deprecated once os.CopyFS works with symlinks:
//
// os.CopyFS(dst, os.DirFS(src))
func CopyDir(src, dst string) error {
var links [][2]string
if err := filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
newPath := filepath.Join(dst, rel)
if d.IsDir() {
return os.MkdirAll(newPath, 0o755)
} else if d.Type().IsRegular() {
_, err = CopyFile(path, newPath)
return err
} else if d.Type()&os.ModeSymlink != 0 {
target, err := os.Readlink(path)
if err != nil {
return err
}
links = append(links, [2]string{target, newPath})
return nil
}
return fmt.Errorf("unable to copy file: %s", path)
}); err != nil {
return err
}
for _, link := range links {
if err := os.Symlink(link[0], link[1]); err != nil && !os.IsExist(err) {
return err
}
}
return nil
}
func StdinIsPipe() bool {
mode := Must(os.Stdin.Stat()).Mode()
return mode&os.ModeNamedPipe != 0 && mode&os.ModeCharDevice == 0
}
func StdoutIsTerminal() bool {
return term.IsTerminal(int(os.Stdout.Fd()))
}
// Closing
// Deprecated: use [x.Close].
func Closing(v io.Closer) { Close(v) }
var closed sync.Map
// Close is a shortcut, instead of writing:
//
// defer func() { C(f.Close()) }()
//
// One can write:
//
// defer Close(f)
//
// If it is called more than once, it is a no-op
func Close(v io.Closer) {
if _, ok := closed.LoadOrStore(v, struct{}{}); !ok {
Check(v.Close())
}
}
type (
closeAfterReader struct{ r io.ReadCloser }
closeAfterReaderWriterTo closeAfterReader
)
func (c closeAfterReader) Read(b []byte) (int, error) {
n, err := c.r.Read(b)
if err == nil {
return n, nil
}
closeErr := c.r.Close()
if err == io.EOF && closeErr == nil {
return n, io.EOF
}
return n, errors.Join(err, closeErr)
}
func (c closeAfterReaderWriterTo) Read(b []byte) (int, error) {
return closeAfterReader(c).Read(b)
}
func (c closeAfterReaderWriterTo) WriteTo(w io.Writer) (int64, error) {
n, err := c.r.(io.WriterTo).WriteTo(w)
return n, errors.Join(err, c.r.Close())
}
// CloseAfterRead returns a Reader that automatically closes when there is no more data to read or an error has occurred.
// Examples:
//
// // Prints SHA2 of "file_to_hash" in hexadecimal notation
// h := sha256.New()
// C2(io.Copy(h, CloseAfterRead(C2(os.Open("file_to_hash")))))
// fmt.Println(hex.EncodeToString(h.Sum(nil)))
//
// // Downloads a file
// const url = "https://go.dev/dl/go1.20.2.linux-amd64.tar.gz"
// dst := C2(os.Create(path.Base(url)))
// defer Closing(dst)
// C2(io.Copy(dst, CloseAfterRead(C2(http.Get(url)).Body)))
func CloseAfterRead(r io.ReadCloser) io.Reader {
if _, ok := r.(io.WriterTo); ok {
return closeAfterReaderWriterTo{r}
}
return closeAfterReader{r}
}
func DecodeJSON(r io.Reader, v any) error {
b, err := io.ReadAll(r)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}