generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
78 lines (64 loc) · 1.82 KB
/
file.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
package utils
import (
"fmt"
"io"
"net/http"
"os"
)
// ReadFile reads the given file and returns its content.
func ReadFile(path string) (string, error) {
res, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("could not read file: %w", err)
}
return string(res), nil
}
// WriteFile writes the given content to the given file.
// Accepts a string or a byte slice.
func WriteFile[T string | []byte](path string, content T) error {
err := os.WriteFile(path, []byte(content), 0o600)
if err != nil {
return fmt.Errorf("could not write file: %w", err)
}
return nil
}
// AppendToFile appends the given content to the given file.
// Accepts a string or a byte slice.
func AppendToFile[T string | []byte](path string, content T) error {
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf("could not open file: %w", err)
}
defer file.Close()
if _, err = file.Write([]byte(content)); err != nil {
return fmt.Errorf("could not write to file: %w", err)
}
return nil
}
// FileExists returns true if the given file exists.
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// DownloadFile downloads the given URL to the given path.
// If the file already exists, it will be overwritten.
func DownloadFile(url, path string) error {
out, err := os.Create(path)
if err != nil {
return fmt.Errorf("could not create file: %w", err)
}
defer out.Close()
resp, err := http.Get(url) //nolint:gosec // wrapper function
if err != nil {
return fmt.Errorf("could not download file: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status) //nolint: err113
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("could not write file: %w", err)
}
return nil
}