diff --git a/cmd/config/init/prompt_existing_root.go b/cmd/config/init/prompt_existing_root.go index 0a9eed43..895a4e14 100644 --- a/cmd/config/init/prompt_existing_root.go +++ b/cmd/config/init/prompt_existing_root.go @@ -10,13 +10,20 @@ import ( func dirNotEmpty(path string) (bool, error) { info, err := os.Stat(path) - if err != nil || !info.IsDir() { + if err != nil { + if os.IsNotExist(err) { + return false, nil + } return false, err } + + if !info.IsDir() { + return false, fmt.Errorf("%s is not a directory", path) + } + files, err := ioutil.ReadDir(path) return len(files) > 0, err } - func promptOverwriteConfig(home string) (bool, error) { prompt := promptui.Prompt{ Label: fmt.Sprintf("Directory %s is not empty. Do you want to overwrite", home), diff --git a/test/config/init/init_test.go b/test/config/init/init_test.go index f5e5b916..849040fd 100644 --- a/test/config/init/init_test.go +++ b/test/config/init/init_test.go @@ -2,6 +2,7 @@ package initconfig_test import ( "io/ioutil" + "path/filepath" "testing" "os" @@ -35,6 +36,7 @@ func TestInitCmd(t *testing.T) { t.Run(tc.name, func(t *testing.T) { assert := assert.New(t) tempDir, err := ioutil.TempDir(os.TempDir(), "test") + tempDir = filepath.Join(tempDir, ".roller") assert.NoError(err) defer func() { err := os.RemoveAll(tempDir)