-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
51 lines (47 loc) · 851 Bytes
/
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
package readme_merge
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
)
func fileExists(file string) (exists bool, err error) {
info, err := os.Stat(file)
if errors.Is(err, fs.ErrNotExist) {
err = nil
exists = false
goto end
}
if err != nil {
err = fmt.Errorf("stating file '%s'; %w", file, err)
goto end
}
exists = !info.IsDir()
end:
return exists, err
}
func FileMustExist(file string) (exists bool) {
exists, err := fileExists(file)
if err != nil {
slog.Error("Stating file", "file", file, "error", err)
goto end
}
end:
return exists
}
func DirMustExist(path string) (exists bool) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
err = nil
exists = false
goto end
}
if err != nil {
slog.Error("Stating dir", "path", path, "error", err)
return false
}
exists = info.IsDir()
end:
return exists
}