Skip to content

Commit

Permalink
Merge pull request #1724 from tejal29/init_perf_impr
Browse files Browse the repository at this point in the history
Improve `skaffold init` performance by not walking hidden dirs.
  • Loading branch information
tejal29 authored Mar 5, 2019
2 parents ab2791f + 104a490 commit 45bb7f4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ func doInit(out io.Writer) error {

var potentialConfigs, k8sConfigs, dockerfiles, images []string
err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error {
if f.IsDir() {
return nil
if f.IsDir() && util.IsHiddenDir(f.Name()) {
logrus.Debugf("skip walking hidden dir %s", f.Name())
return filepath.SkipDir
}
if strings.HasPrefix(path, ".") {
if f.IsDir() || util.IsHiddenFile(f.Name()) {
return nil
}
if util.IsSupportedKubernetesFormat(path) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import (
"github.com/sirupsen/logrus"
)

const (
hiddenPrefix string = "."
)

func RandomID() string {
b := make([]byte, 16)
_, err := rand.Read(b)
Expand Down Expand Up @@ -293,3 +297,22 @@ func AbsolutePaths(workspace string, paths []string) []string {
}
return p
}

// IsHiddenDir returns if a directory is hidden.
func IsHiddenDir(filename string) bool {
// Return false for current dir
if filename == hiddenPrefix {
return false
}
return hasHiddenPrefix(filename)
}

// IsHiddenFile returns if a file is hidden.
// File is hidden if it starts with prefix "."
func IsHiddenFile(filename string) bool {
return hasHiddenPrefix(filename)
}

func hasHiddenPrefix(s string) bool {
return strings.HasPrefix(s, hiddenPrefix)
}
57 changes: 57 additions & 0 deletions pkg/skaffold/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,60 @@ func TestCloneThroughJSON(t *testing.T) {
})
}
}

func TestIsHiddenDir(t *testing.T) {
tests := []struct {
name string
filename string
expected bool
}{
{
name: "hidden dir",
filename: ".hidden",
expected: true,
},
{
name: "not hidden dir",
filename: "not_hidden",
expected: false,
},
{
name: "current dir",
filename: ".",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if IsHiddenDir(test.filename) != test.expected {
t.Errorf("error want %t, got %t", test.expected, !test.expected)
}
})
}
}

func TestIsHiddenFile(t *testing.T) {
tests := []struct {
name string
filename string
expected bool
}{
{
name: "hidden file name",
filename: ".hidden",
expected: true,
},
{
name: "not hidden file",
filename: "not_hidden",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if IsHiddenDir(test.filename) != test.expected {
t.Errorf("error want %t, got %t", test.expected, !test.expected)
}
})
}
}

0 comments on commit 45bb7f4

Please sign in to comment.