Skip to content

Commit 1cda3dd

Browse files
BenPopejunr03
authored andcommitted
Allow dotfiles to be ignored. (#17)
1 parent abba47f commit 1cda3dd

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ import (
106106

107107
// for full docs on gostats visit https://github.com/lyft/gostats
108108
store := stats.NewDefaultStore()
109-
runtime := loader.New("runtime_path", "runtime_subdirectory", store.Scope("runtime"), &DirectoryRefresher{})
109+
runtime := loader.New("runtime_path", "runtime_subdirectory", store.Scope("runtime"), &DirectoryRefresher{}, AllowDotFiles)
110110
```
111111

112112
The Loader will use filesystem events to update the filesystem snapshot it has.
113+
Dotfiles can be allowed by passing `AllowDotFiles` or ignored by passing `IgnoreDotFiles`.
113114

114115
#### Snapshot
115116

@@ -160,7 +161,7 @@ And the runtime loader is setup like so:
160161

161162
```Go
162163
store := stats.NewDefaultStore()
163-
runtime := loader.New("/runtime", "config", stats.Scope("runtime"))
164+
runtime := loader.New("/runtime", "config", stats.Scope("runtime"), AllowDotFiles)
164165
```
165166

166167
The values in all three files can be obtained the following way:

loader/loader.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Loader struct {
4040
updateLock sync.RWMutex
4141
callbacks []chan<- int
4242
stats loaderStats
43+
ignoreDotfiles bool
4344
}
4445

4546
func (l *Loader) Snapshot() snapshot.IFace {
@@ -90,7 +91,15 @@ func (l *Loader) walkDirectoryCallback(path string, info os.FileInfo, err error)
9091
}
9192

9293
logger.Debugf("runtime: processing %s", path)
94+
if l.ignoreDotfiles && info.IsDir() && strings.HasPrefix(info.Name(), ".") {
95+
return filepath.SkipDir
96+
}
97+
9398
if !info.IsDir() {
99+
if l.ignoreDotfiles && strings.HasPrefix(info.Name(), ".") {
100+
return nil
101+
}
102+
94103
contents, err := ioutil.ReadFile(path)
95104

96105
if err != nil {
@@ -148,7 +157,12 @@ func getFileSystemOp(ev fsnotify.Event) FileSystemOp {
148157
return -1
149158
}
150159

151-
func New(runtimePath string, runtimeSubdirectory string, scope stats.Scope, refresher Refresher) IFace {
160+
const (
161+
AllowDotFiles = false
162+
IgnoreDotFiles = true
163+
)
164+
165+
func New(runtimePath string, runtimeSubdirectory string, scope stats.Scope, refresher Refresher, ignoreDotfiles bool) IFace {
152166
if runtimePath == "" || runtimeSubdirectory == "" {
153167
logger.Warnf("no runtime configuration. using nil loader.")
154168
return NewNil()
@@ -168,7 +182,7 @@ func New(runtimePath string, runtimeSubdirectory string, scope stats.Scope, refr
168182

169183
newLoader := Loader{
170184
watcher, runtimePath, runtimeSubdirectory, nil, nil, sync.RWMutex{}, nil,
171-
newLoaderStats(scope)}
185+
newLoaderStats(scope), ignoreDotfiles}
172186
newLoader.onRuntimeChanged()
173187

174188
go func() {

loader/loader_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func makeFileInDir(assert *require.Assertions, path string, text string) {
3333
func TestNilRuntime(t *testing.T) {
3434
assert := require.New(t)
3535

36-
loader := New("", "", nullScope, &SymlinkRefresher{RuntimePath: ""})
36+
loader := New("", "", nullScope, &SymlinkRefresher{RuntimePath: ""}, AllowDotFiles)
3737
snapshot := loader.Snapshot()
3838
assert.Equal("", snapshot.Get("foo"))
3939
assert.Equal(uint64(100), snapshot.GetInteger("bar", 100))
@@ -57,7 +57,7 @@ func TestSymlinkRefresher(t *testing.T) {
5757
err = os.Symlink(tempDir+"/testdir1", tempDir+"/current")
5858
assert.NoError(err)
5959

60-
loader := New(tempDir+"/current", "app", nullScope, &SymlinkRefresher{RuntimePath: tempDir + "/current"})
60+
loader := New(tempDir+"/current", "app", nullScope, &SymlinkRefresher{RuntimePath: tempDir + "/current"}, AllowDotFiles)
6161
runtime_update := make(chan int)
6262
loader.AddUpdateCallback(runtime_update)
6363
snapshot := loader.Snapshot()
@@ -101,6 +101,30 @@ func TestSymlinkRefresher(t *testing.T) {
101101
assert.EqualValues([]string{"dir.file2", "dir2.file3", "file1"}, keys)
102102
}
103103

104+
func TestIgnoreDotfiles(t *testing.T) {
105+
assert := require.New(t)
106+
107+
// Setup base test directory.
108+
tempDir, err := ioutil.TempDir("", "runtime_test")
109+
assert.NoError(err)
110+
defer os.RemoveAll(tempDir)
111+
112+
// Make test files for runtime snapshot.
113+
makeFileInDir(assert, tempDir+"/testdir1/app/dir3/.file4", ".file4")
114+
makeFileInDir(assert, tempDir+"/testdir1/app/.dir/file5", ".dir")
115+
assert.NoError(err)
116+
117+
loaderIgnoreDotfiles := New(tempDir+"/testdir1", "app", nullScope, &SymlinkRefresher{RuntimePath: tempDir + "/testdir1"}, IgnoreDotFiles)
118+
snapshot := loaderIgnoreDotfiles.Snapshot()
119+
assert.Equal("", snapshot.Get("dir3..file4"))
120+
assert.Equal("", snapshot.Get(".dir.file5"))
121+
122+
loaderIncludeDotfiles := New(tempDir+"/testdir1", "app", nullScope, &SymlinkRefresher{RuntimePath: tempDir + "/testdir1"}, AllowDotFiles)
123+
snapshot = loaderIncludeDotfiles.Snapshot()
124+
assert.Equal(".file4", snapshot.Get("dir3..file4"))
125+
assert.Equal(".dir", snapshot.Get(".dir.file5"))
126+
}
127+
104128
func TestDirectoryRefresher(t *testing.T) {
105129
assert := require.New(t)
106130

@@ -113,7 +137,7 @@ func TestDirectoryRefresher(t *testing.T) {
113137
err = os.MkdirAll(appDir, os.ModeDir|os.ModePerm)
114138
assert.NoError(err)
115139

116-
loader := New(tempDir, "app", nullScope, &DirectoryRefresher{})
140+
loader := New(tempDir, "app", nullScope, &DirectoryRefresher{}, AllowDotFiles)
117141
runtime_update := make(chan int)
118142
loader.AddUpdateCallback(runtime_update)
119143
snapshot := loader.Snapshot()

0 commit comments

Comments
 (0)