...
1
2
3
4 package filesys
5
6 import (
7 "fmt"
8 "path/filepath"
9
10 "sigs.k8s.io/kustomize/kyaml/errors"
11 )
12
13 const (
14 Separator = string(filepath.Separator)
15 SelfDir = "."
16 ParentDir = ".."
17 )
18
19
20
21 type FileSystem interface {
22
23
24 Create(path string) (File, error)
25
26
27 Mkdir(path string) error
28
29
30 MkdirAll(path string) error
31
32
33 RemoveAll(path string) error
34
35
36 Open(path string) (File, error)
37
38
39 IsDir(path string) bool
40
41
42 ReadDir(path string) ([]string, error)
43
44
45
46
47
48
49 CleanedAbs(path string) (ConfirmedDir, string, error)
50
51
52 Exists(path string) bool
53
54
55
56 Glob(pattern string) ([]string, error)
57
58
59 ReadFile(path string) ([]byte, error)
60
61
62
63 WriteFile(path string, data []byte) error
64
65
66 Walk(path string, walkFn filepath.WalkFunc) error
67 }
68
69
70
71 func ConfirmDir(fSys FileSystem, path string) (ConfirmedDir, error) {
72 if path == "" {
73 return "", errors.Errorf("directory path cannot be empty")
74 }
75
76 d, f, err := fSys.CleanedAbs(path)
77 if err != nil {
78 return "", errors.WrapPrefixf(err, "not a valid directory")
79 }
80 if f != "" {
81 return "", errors.WrapPrefixf(errors.Errorf("file is not directory"), fmt.Sprintf("'%s'", path))
82 }
83 return d, nil
84 }
85
86
87
88
89 type FileSystemOrOnDisk struct {
90 FileSystem FileSystem
91 }
92
93
94 func (fs *FileSystemOrOnDisk) Set(f FileSystem) { fs.FileSystem = f }
95
96 func (fs FileSystemOrOnDisk) fs() FileSystem {
97 if fs.FileSystem != nil {
98 return fs.FileSystem
99 }
100 return MakeFsOnDisk()
101 }
102
103 func (fs FileSystemOrOnDisk) Create(path string) (File, error) {
104 return fs.fs().Create(path)
105 }
106
107 func (fs FileSystemOrOnDisk) Mkdir(path string) error {
108 return fs.fs().Mkdir(path)
109 }
110
111 func (fs FileSystemOrOnDisk) MkdirAll(path string) error {
112 return fs.fs().MkdirAll(path)
113 }
114
115 func (fs FileSystemOrOnDisk) RemoveAll(path string) error {
116 return fs.fs().RemoveAll(path)
117 }
118
119 func (fs FileSystemOrOnDisk) Open(path string) (File, error) {
120 return fs.fs().Open(path)
121 }
122
123 func (fs FileSystemOrOnDisk) IsDir(path string) bool {
124 return fs.fs().IsDir(path)
125 }
126
127 func (fs FileSystemOrOnDisk) ReadDir(path string) ([]string, error) {
128 return fs.fs().ReadDir(path)
129 }
130
131 func (fs FileSystemOrOnDisk) CleanedAbs(path string) (ConfirmedDir, string, error) {
132 return fs.fs().CleanedAbs(path)
133 }
134
135 func (fs FileSystemOrOnDisk) Exists(path string) bool {
136 return fs.fs().Exists(path)
137 }
138
139 func (fs FileSystemOrOnDisk) Glob(pattern string) ([]string, error) {
140 return fs.fs().Glob(pattern)
141 }
142
143 func (fs FileSystemOrOnDisk) ReadFile(path string) ([]byte, error) {
144 return fs.fs().ReadFile(path)
145 }
146
147 func (fs FileSystemOrOnDisk) WriteFile(path string, data []byte) error {
148 return fs.fs().WriteFile(path, data)
149 }
150
151 func (fs FileSystemOrOnDisk) Walk(path string, walkFn filepath.WalkFunc) error {
152 return fs.fs().Walk(path, walkFn)
153 }
154
View as plain text