From 715f41bd7a70b5111f898b71ab484da52ee6266d Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Tue, 18 Jul 2017 18:22:37 +0200 Subject: [PATCH] cmd: Ignore hidden files in isEmpty --- cobra/cmd/helpers.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 6114227d..c5e261ce 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -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) - } - return len(dirs) == 0 + + if !fi.IsDir() { + return fi.Size() == 0 } - 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 true } // exists checks if a file or directory exists.