-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.go
54 lines (52 loc) · 1.04 KB
/
save.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
package io
import (
"os"
"path/filepath"
)
func Save(fullName string, content string) error {
err := os.MkdirAll(filepath.Dir(fullName), os.ModePerm)
if err != nil {
return err
}
return os.WriteFile(fullName, []byte(content), os.ModePerm)
}
func List(path string) ([]string, error) {
var names []string
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
folder, err := os.ReadDir(absPath)
if err != nil {
return names, err
}
for _, tmpl := range folder {
names = append(names, tmpl.Name())
}
return names, nil
}
func Load(directory string) (map[string]string, error) {
tm := make(map[string]string, 0)
names, er1 := List(directory)
if er1 != nil {
return nil, er1
}
for _, name := range names {
content, er2 := os.ReadFile(directory + string(os.PathSeparator) + name)
if er2 != nil {
return nil, er2
}
tm[name] = string(content)
}
return tm, nil
}
func IsValidPath(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}