Debian Codegraph @

kyaml/filesys/filesystem.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package filesys
5
6 import (
7 "path/filepath"
8
9 "sigs.k8s.io/kustomize/kyaml/errors"
10 )
11
12 const (
13 Separator = string(filepath.Separator)
14 SelfDir = "."
15 ParentDir = ".."
16 )
17
18 // FileSystem groups basic os filesystem methods.
19 // It's supposed be functional subset of https://golang.org/pkg/os
20 type FileSystem interface {
21
22 // Create a file.
23 Create(path string) (File, error)
24
25 // MkDir makes a directory.
26 Mkdir(path string) error
27
28 // MkDirAll makes a directory path, creating intervening directories.
29 MkdirAll(path string) error
30
31 // RemoveAll removes path and any children it contains.
32 RemoveAll(path string) error
33
34 // Open opens the named file for reading.
35 Open(path string) (File, error)
36
37 // IsDir returns true if the path is a directory.
38 IsDir(path string) bool
39
40 // ReadDir returns a list of files and directories within a directory.
41 ReadDir(path string) ([]string, error)
42
43 // CleanedAbs converts the given path into a
44 // directory and a file name, where the directory
45 // is represented as a ConfirmedDir and all that implies.
46 // If the entire path is a directory, the file component
47 // is an empty string.
48 CleanedAbs(path string) (ConfirmedDir, string, error)
49
50 // Exists is true if the path exists in the file system.
51 Exists(path string) bool
52
53 // Glob returns the list of matching files,
54 // emulating https://golang.org/pkg/path/filepath/#Glob
55 Glob(pattern string) ([]string, error)
56
57 // ReadFile returns the contents of the file at the given path.
58 ReadFile(path string) ([]byte, error)
59
60 // WriteFile writes the data to a file at the given path,
61 // overwriting anything that's already there.
62 WriteFile(path string, data []byte) error
63
64 // Walk walks the file system with the given WalkFunc.
65 Walk(path string, walkFn filepath.WalkFunc) error
66 }
67
68 // ConfirmDir returns an error if the user-specified path is not an existing directory on fSys.
69 // Otherwise, ConfirmDir returns path, which can be relative, as a ConfirmedDir and all that implies.
70 func ConfirmDir(fSys FileSystem, path string) (ConfirmedDir, error) {
71 if path == "" {
72 return "", errors.Errorf("directory path cannot be empty")
73 }
74
75 d, f, err := fSys.CleanedAbs(path)
76 if err != nil {
77 return "", errors.WrapPrefixf(err, "not a valid directory")
78 }
79 if f != "" {
80 return "", errors.WrapPrefixf(errors.Errorf("file is not directory"), "'%s'", path)
81 }
82 return d, nil
83 }
84
85 // FileSystemOrOnDisk satisfies the FileSystem interface by forwarding
86 // all of its method calls to the given FileSystem whenever it's not nil.
87 // If it's nil, the call is forwarded to the OS's underlying file system.
88 type FileSystemOrOnDisk struct {
89 FileSystem FileSystem
90 }
91
92 // Set sets the given FileSystem as the target for all the FileSystem method calls.
93 func (fs *FileSystemOrOnDisk) Set(f FileSystem) { fs.FileSystem = f }
94
95 func (fs FileSystemOrOnDisk) fs() FileSystem {
96 if fs.FileSystem != nil {
97 return fs.FileSystem
98 }
99 return MakeFsOnDisk()
100 }
101
102 func (fs FileSystemOrOnDisk) Create(path string) (File, error) {
103 return fs.fs().Create(path)
104 }
105
106 func (fs FileSystemOrOnDisk) Mkdir(path string) error {
107 return fs.fs().Mkdir(path)
108 }
109
110 func (fs FileSystemOrOnDisk) MkdirAll(path string) error {
111 return fs.fs().MkdirAll(path)
112 }
113
114 func (fs FileSystemOrOnDisk) RemoveAll(path string) error {
115 return fs.fs().RemoveAll(path)
116 }
117
118 func (fs FileSystemOrOnDisk) Open(path string) (File, error) {
119 return fs.fs().Open(path)
120 }
121
122 func (fs FileSystemOrOnDisk) IsDir(path string) bool {
123 return fs.fs().IsDir(path)
124 }
125
126 func (fs FileSystemOrOnDisk) ReadDir(path string) ([]string, error) {
127 return fs.fs().ReadDir(path)
128 }
129
130 func (fs FileSystemOrOnDisk) CleanedAbs(path string) (ConfirmedDir, string, error) {
131 return fs.fs().CleanedAbs(path)
132 }
133
134 func (fs FileSystemOrOnDisk) Exists(path string) bool {
135 return fs.fs().Exists(path)
136 }
137
138 func (fs FileSystemOrOnDisk) Glob(pattern string) ([]string, error) {
139 return fs.fs().Glob(pattern)
140 }
141
142 func (fs FileSystemOrOnDisk) ReadFile(path string) ([]byte, error) {
143 return fs.fs().ReadFile(path)
144 }
145
146 func (fs FileSystemOrOnDisk) WriteFile(path string, data []byte) error {
147 return fs.fs().WriteFile(path, data)
148 }
149
150 func (fs FileSystemOrOnDisk) Walk(path string, walkFn filepath.WalkFunc) error {
151 return fs.fs().Walk(path, walkFn)
152 }