-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
187 lines (156 loc) · 3.45 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package gofs
import (
"fmt"
"github.com/spf13/afero"
"os"
"path"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/pkg/browser"
)
// File is a file in the filesystem.
type File struct {
// path is the absolute filepath
path string
createPermissions os.FileMode
fs afero.Fs
}
func FileAt(filePath string) File {
return FileWithFs(filePath, afero.NewOsFs())
}
func fileWithSameFs(filePath string, f File) File {
return FileWithFs(filePath, f.fs)
}
func FileWithFs(filePath string, fs afero.Fs) File {
// replace home dir path
filePath, err := homedir.Expand(filePath)
if err != nil {
// this should really not happen
panic(err)
}
// make path absolute
if !filepath.IsAbs(filePath) {
pwd, err := os.Getwd()
if err != nil {
// this should not happen
panic(err)
}
filePath = filepath.Join(pwd, filePath)
}
return File{
path: filePath,
createPermissions: 0640,
fs: fs,
}
}
// SetCreatePermissions allows you to define the FileMode used when creating this file (if it did not exist).
func (x File) SetCreatePermissions(perm os.FileMode) File {
x.createPermissions = perm
return x
}
func (x File) Exists() bool {
fi, err := x.fs.Stat(x.Path())
return !os.IsNotExist(err) && !fi.IsDir()
}
func (x File) AssertExists() File {
if !x.Exists() {
panic(fmt.Errorf("file %s should have existed", x))
}
return x
}
func (x File) NotExists() bool {
return !x.Exists()
}
func (x File) AssertNotExists() File {
if !x.NotExists() {
panic(fmt.Errorf("file %s should not have existed", x))
}
return x
}
func (x File) Path() string {
return x.path
}
func (x File) RelativeTo(to Dir) string {
result, err := filepath.Rel(to.Path(), x.Path())
if err != nil {
return x.Path()
}
return result
}
func (x File) IsHidden() bool {
return strings.HasPrefix(x.Filename(), ".")
}
func (x File) Equals(otherFile File) bool {
return x.Path() == otherFile.Path()
}
func (x File) String() string {
return x.path
}
func (x File) Filename() string {
return path.Base(x.path)
}
func (x File) FilenameWithoutExtension() string {
return strings.TrimRight(x.Filename(), path.Ext(x.path))
}
func (x File) WithoutExtension() File {
return FileWithFs(strings.TrimRight(x.path, path.Ext(x.path)), x.fs)
}
func (x File) OpenStandard() error {
return browser.OpenFile(x.Path())
}
func (x File) Filesize() int64 {
if !x.Exists() {
return 0
}
if !x.IsReadable() {
panic(fmt.Errorf("file %s was not readable to check filesize", x))
}
fi, err := x.fs.Stat(x.Path())
if err != nil {
// this should not happen
panic(err)
}
return fi.Size()
}
func (x File) FilesizeHuman() string {
const unit = 1024
b := x.Filesize()
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
func (x File) IsEmpty() bool {
return x.Filesize() == 0
}
func (x File) AssertEmpty() File {
if !x.IsEmpty() {
panic(fmt.Errorf("file %s should have been empty", x))
}
return x
}
func (x File) AssertNotEmpty() File {
if x.IsEmpty() {
panic(fmt.Errorf("file %s should not have been empty", x))
}
return x
}
func (x File) WithFileReadOnly(logic func(f afero.File) error) error {
f, err := x.fs.Open(x.Path())
if err != nil {
return err
}
err = logic(f)
closeErr := f.Close()
if err != nil {
return err
}
return closeErr
}