mirror of
https://github.com/spf13/cobra
synced 2024-11-16 10:47:09 +00:00
cmd: Ignore hidden files in isEmpty
This commit is contained in:
parent
d994347eda
commit
715f41bd7a
1 changed files with 22 additions and 12 deletions
|
@ -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() {
|
||||
|
||||
if !fi.IsDir() {
|
||||
return fi.Size() == 0
|
||||
}
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
er(err)
|
||||
}
|
||||
defer f.Close()
|
||||
dirs, err := f.Readdirnames(1)
|
||||
|
||||
names, err := f.Readdirnames(-1)
|
||||
if err != nil && err != io.EOF {
|
||||
er(err)
|
||||
}
|
||||
return len(dirs) == 0
|
||||
|
||||
for _, name := range names {
|
||||
if len(name) > 0 && name[0] != '.' {
|
||||
return false
|
||||
}
|
||||
return fi.Size() == 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// exists checks if a file or directory exists.
|
||||
|
|
Loading…
Reference in a new issue