-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.go
58 lines (46 loc) · 875 Bytes
/
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
package main
import (
"io/fs"
"os"
"path/filepath"
"strings"
)
type FileInfo struct {
IsDir bool
Data []byte
}
func IsDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
return fileInfo.IsDir(), err
}
func GetFileInfo(path string) (*FileInfo, error) {
isDir, err := IsDir(path)
if err != nil {
return nil, err
}
if isDir {
return &FileInfo{
IsDir: true,
Data: nil,
}, nil
}
file, err := os.ReadFile(path)
return &FileInfo{
IsDir: false,
Data: file,
}, err
}
type Tree map[string]string
// this function must return map
func GetTree(rootPath string) (Tree, error) {
var tree = make(Tree)
err := filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
if path == rootPath {
return nil
}
path = strings.TrimPrefix(path, rootPath)
tree[path] = ""
return nil
})
return tree, err
}