Skip to content

Commit 6fe24f5

Browse files
author
Aaron Schlesinger
committed
fix(pkg/cleaner): refactor localDirs to accept a filter function
and use it to return only directories with the git suffix in them
1 parent 8b0e31b commit 6fe24f5

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

pkg/cleaner/cleaner.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const (
2020
dotGitSuffix = ".git"
2121
)
2222

23-
func localDirs(gitHome string) ([]string, error) {
23+
// localDirs returns all of the local directories immediately under gitHome that filter returns true for. filter will receive only the names of each of the top level directories (not their fully qualified paths), and should return true if it should be included in the output
24+
func localDirs(gitHome string, filter func(string) bool) ([]string, error) {
2425
fileInfos, err := ioutil.ReadDir(gitHome)
2526
if err != nil {
2627
return nil, err
@@ -31,7 +32,9 @@ func localDirs(gitHome string) ([]string, error) {
3132
if len(nm) <= 0 || nm == "." || !fileInfo.IsDir() {
3233
continue
3334
}
34-
ret = append(ret, filepath.Join(gitHome, nm))
35+
if filter(nm) {
36+
ret = append(ret, filepath.Join(gitHome, nm))
37+
}
3538
}
3639
return ret, nil
3740
}
@@ -71,6 +74,10 @@ func stripSuffixes(strs []string, suffix string) []string {
7174
return ret
7275
}
7376

77+
func dirHasGitSuffix(dir string) bool {
78+
return strings.HasSuffix(dir, dotGitSuffix)
79+
}
80+
7481
// Run starts the deleted app cleaner. Every pollSleepDuration, it compares the result of nsLister.List with the directories in the top level of gitHome on the local file system. On any error, it uses log messages to output a human readable description of what happened.
7582
func Run(gitHome string, nsLister k8s.NamespaceLister, repoLock sshd.RepositoryLock, pollSleepDuration time.Duration) error {
7683
for {
@@ -86,7 +93,7 @@ func Run(gitHome string, nsLister k8s.NamespaceLister, repoLock sshd.RepositoryL
8693
log.Printf("Cleaner found namespaces %s", lst)
8794
}
8895

89-
gitDirs, err := localDirs(gitHome)
96+
gitDirs, err := localDirs(gitHome, dirHasGitSuffix)
9097
if err != nil {
9198
log.Printf("Cleaner error listing local git directories (%s)", err)
9299
continue

pkg/cleaner/cleaner_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@ func TestGetDiff(t *testing.T) {
2020
assert.Equal(t, len(diff), 1, "number of items in the disjunction")
2121
}
2222

23+
func TestDirHasGitSuffix(t *testing.T) {
24+
assert.True(t, dirHasGitSuffix("a.git"), "'a.git' reported no git suffix")
25+
assert.False(t, dirHasGitSuffix("abc"), "'a' reported git suffix")
26+
}
27+
2328
func TestLocalDirs(t *testing.T) {
2429
wd, err := os.Getwd()
2530
assert.NoErr(t, err)
2631
pkgDir, err := filepath.Abs(wd + "/..")
2732
assert.NoErr(t, err)
28-
lDirs, err := localDirs(pkgDir)
33+
lDirs, err := localDirs(pkgDir, func(dir string) bool {
34+
// no directories with any dots in them
35+
return len(strings.Split(dir, ".")) == 1
36+
})
2937
assert.NoErr(t, err)
3038

3139
expectedPackages := map[string]int{

0 commit comments

Comments
 (0)