-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
151 lines (124 loc) · 2.85 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/template"
"time"
)
const (
filePerm os.FileMode = 0600
dirPerm os.FileMode = 0750
)
// File represents the file options
type File struct {
Filename string
Path string
FullName string
Extension string
}
// NewFile returns a new File
func NewFile(path ...string) *File {
p := filepath.Join(path...)
return &File{
Filename: filepath.Base(p),
Path: filepath.Dir(p),
FullName: p,
Extension: filepath.Ext(p),
}
}
// Exists returns if file exists
func (f *File) Exists() bool {
_, err := os.Stat(f.FullName)
return !os.IsNotExist(err)
}
// Create a directory, along with any necessary parents
func (f *File) Create() error {
if err := os.MkdirAll(f.Path, dirPerm); err != nil {
return err
}
return nil
}
// Backup makes a copy of the file
func (f *File) Backup() (string, error) {
if !f.Exists() {
return "", nil
}
bkpFilename := fmt.Sprintf("%s.backup_%d", f.FullName, time.Now().Unix())
b, err := os.ReadFile(f.FullName)
if err != nil {
return "", err
}
if err := os.WriteFile(bkpFilename, b, filePerm); err != nil {
return "", err
}
return bkpFilename, nil
}
// Read reads a file and returns the content as []byte
func (f *File) Read() ([]byte, error) {
b, err := os.ReadFile(f.FullName)
if err != nil {
return nil, err
}
return b, nil
}
// ReadString reads a file and returns the content as string
func (f *File) ReadString() (string, error) {
b, err := os.ReadFile(f.FullName)
if err != nil {
return "", err
}
return string(b), nil
}
// ReadJSON reads a file and fills in the provided struct
func (f *File) ReadJSON(s interface{}) error {
b, err := f.Read()
if err != nil {
return err
}
err = json.Unmarshal(b, s)
if err != nil {
return err
}
return nil
}
// Write writes the string content in the file
func (f *File) Write(content string) error {
return os.WriteFile(f.FullName, []byte(content), filePerm)
}
// WriteJSON writes the json in the file
func (f *File) WriteJSON(data interface{}) error {
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
return os.WriteFile(f.FullName, b, filePerm)
}
// WriteTemplate writes the struct using a template
func (f *File) WriteTemplate(tmpl string, data interface{}) error {
t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return err
}
var b bytes.Buffer
if err = t.Execute(&b, data); err != nil {
return err
}
return os.WriteFile(f.FullName, b.Bytes(), filePerm)
}
// WriteTemplateSlice writes a slice using a template
func (f *File) WriteTemplateSlice(tmpl string, data []interface{}) error {
t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return err
}
var b bytes.Buffer
for _, d := range data {
if err = t.Execute(&b, d); err != nil {
return err
}
}
return os.WriteFile(f.FullName, b.Bytes(), filePerm)
}