Skip to content

Commit fc8a5b4

Browse files
authored
Merge pull request #132 from yangcao77/update-filesystem
update the filesystem
2 parents 0749c7f + 25c06ea commit fc8a5b4

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
require (
66
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c
77
github.com/fatih/color v1.7.0
8+
github.com/fsnotify/fsnotify v1.4.9
89
github.com/gobwas/glob v0.2.3
910
github.com/golang/mock v1.5.0
1011
github.com/google/go-cmp v0.5.5

pkg/testingutil/filesystem/default_fs.go

+18
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (DefaultFs) Remove(name string) error {
9292
return os.Remove(name)
9393
}
9494

95+
// Getwd via os.Getwd
96+
func (DefaultFs) Getwd() (dir string, err error) {
97+
return os.Getwd()
98+
}
99+
95100
// ReadFile via ioutil.ReadFile
96101
func (DefaultFs) ReadFile(filename string) ([]byte, error) {
97102
return ioutil.ReadFile(filename)
@@ -126,6 +131,11 @@ func (DefaultFs) Walk(root string, walkFn filepath.WalkFunc) error {
126131
return filepath.Walk(root, walkFn)
127132
}
128133

134+
// Chmod via os.Chmod
135+
func (f DefaultFs) Chmod(name string, mode os.FileMode) error {
136+
return os.Chmod(name, mode)
137+
}
138+
129139
// defaultFile implements File using same-named functions from "os"
130140
type defaultFile struct {
131141
file *os.File
@@ -159,3 +169,11 @@ func (file *defaultFile) Close() error {
159169
func (file *defaultFile) Readdir(n int) ([]os.FileInfo, error) {
160170
return file.file.Readdir(n)
161171
}
172+
173+
func (file *defaultFile) Read(b []byte) (n int, err error) {
174+
return file.file.Read(b)
175+
}
176+
177+
func (file *defaultFile) Chmod(name string, mode os.FileMode) error {
178+
return file.file.Chmod(mode)
179+
}

pkg/testingutil/filesystem/fake_fs.go

+13
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,20 @@ func (fs *fakeFs) RemoveAll(path string) error {
125125
return fs.a.RemoveAll(path)
126126
}
127127

128+
func (fs *fakeFs) Getwd() (dir string, err error) {
129+
return ".", nil
130+
}
131+
128132
// Remove via afero.RemoveAll
129133
func (fs *fakeFs) Remove(name string) error {
130134
return fs.a.Remove(name)
131135
}
132136

137+
// Chmod via afero.Chmod
138+
func (fs *fakeFs) Chmod(name string, mode os.FileMode) error {
139+
return fs.a.Chmod(name, mode)
140+
}
141+
133142
// fakeFile implements File; for use with fakeFs
134143
type fakeFile struct {
135144
file afero.File
@@ -163,3 +172,7 @@ func (file *fakeFile) Close() error {
163172
func (file *fakeFile) Readdir(n int) ([]os.FileInfo, error) {
164173
return file.file.Readdir(n)
165174
}
175+
176+
func (file *fakeFile) Read(b []byte) (n int, err error) {
177+
return file.file.Read(b)
178+
}

pkg/testingutil/filesystem/filesystem.go

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Filesystem interface {
3939
Chtimes(name string, atime time.Time, mtime time.Time) error
4040
RemoveAll(path string) error
4141
Remove(name string) error
42+
Chmod(name string, mode os.FileMode) error
43+
Getwd() (dir string, err error)
4244

4345
// from "io/ioutil"
4446
ReadFile(filename string) ([]byte, error)
@@ -58,5 +60,6 @@ type File interface {
5860
WriteString(s string) (n int, err error)
5961
Sync() error
6062
Close() error
63+
Read(b []byte) (n int, err error)
6164
Readdir(n int) ([]os.FileInfo, error)
6265
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package filesystem
2+
3+
var singleFs Filesystem
4+
5+
func Get() Filesystem {
6+
if singleFs == nil {
7+
singleFs = &DefaultFs{}
8+
}
9+
return singleFs
10+
}

pkg/testingutil/filesystem/watcher.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
This package is a FORK of https://github.com/kubernetes/kubernetes/blob/master/pkg/util/filesystem/watcher.go
19+
See above license
20+
*/
21+
22+
package filesystem
23+
24+
import (
25+
"github.com/fsnotify/fsnotify"
26+
)
27+
28+
// FSWatcher is a callback-based filesystem watcher abstraction for fsnotify.
29+
type FSWatcher interface {
30+
// Initializes the watcher with the given watch handlers.
31+
// Called before all other methods.
32+
Init(FSEventHandler, FSErrorHandler) error
33+
34+
// Starts listening for events and errors.
35+
// When an event or error occurs, the corresponding handler is called.
36+
Run()
37+
38+
// Add a filesystem path to watch
39+
AddWatch(path string) error
40+
}
41+
42+
// FSEventHandler is called when a fsnotify event occurs.
43+
type FSEventHandler func(event fsnotify.Event)
44+
45+
// FSErrorHandler is called when a fsnotify error occurs.
46+
type FSErrorHandler func(err error)
47+
48+
type fsnotifyWatcher struct {
49+
watcher *fsnotify.Watcher
50+
eventHandler FSEventHandler
51+
errorHandler FSErrorHandler
52+
}
53+
54+
var _ FSWatcher = &fsnotifyWatcher{}
55+
56+
func (w *fsnotifyWatcher) AddWatch(path string) error {
57+
return w.watcher.Add(path)
58+
}
59+
60+
func (w *fsnotifyWatcher) Init(eventHandler FSEventHandler, errorHandler FSErrorHandler) error {
61+
var err error
62+
w.watcher, err = fsnotify.NewWatcher()
63+
if err != nil {
64+
return err
65+
}
66+
67+
w.eventHandler = eventHandler
68+
w.errorHandler = errorHandler
69+
return nil
70+
}
71+
72+
func (w *fsnotifyWatcher) Run() {
73+
go func() {
74+
defer w.watcher.Close()
75+
for {
76+
select {
77+
case event := <-w.watcher.Events:
78+
if w.eventHandler != nil {
79+
w.eventHandler(event)
80+
}
81+
case err := <-w.watcher.Errors:
82+
if w.errorHandler != nil {
83+
w.errorHandler(err)
84+
}
85+
}
86+
}
87+
}()
88+
}

0 commit comments

Comments
 (0)