Skip to content

Commit

Permalink
cmd: Ignore hidden files in isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
n10v committed Jul 18, 2017
1 parent d994347 commit 715f41b
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions cobra/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,34 @@ func er(msg interface{}) {
}

// isEmpty checks if a given path is empty.
// Hidden files in path are ignored.
func isEmpty(path string) bool {
fi, err := os.Stat(path)
if err != nil {
er(err)
}
if fi.IsDir() {
f, err := os.Open(path)
if err != nil {
er(err)
}
defer f.Close()
dirs, err := f.Readdirnames(1)
if err != nil && err != io.EOF {
er(err)

if !fi.IsDir() {
return fi.Size() == 0
}

f, err := os.Open(path)
if err != nil {
er(err)
}
defer f.Close()

names, err := f.Readdirnames(-1)
if err != nil && err != io.EOF {
er(err)
}

for _, name := range names {
if len(name) > 0 && name[0] != '.' {
return false
}
return len(dirs) == 0
}
return fi.Size() == 0
return true
}

// exists checks if a file or directory exists.
Expand Down

0 comments on commit 715f41b

Please sign in to comment.