InitCommand() robustness #327

Merged
6543 merged 4 commits from noerw/tea:fix-320 into master 2021-02-28 22:29:27 +00:00
2 changed files with 20 additions and 4 deletions
Showing only changes of commit f069bb5220 - Show all commits

View File

@ -81,7 +81,7 @@ func InitCommand(ctx *cli.Context) *TeaContext {
// check if repoFlag can be interpreted as path to local repo.
if len(repoFlag) != 0 {
repoFlagPathExists, err := utils.PathExists(repoFlag)
repoFlagPathExists, err := utils.DirExists(repoFlag)
if err != nil {
log.Fatal(err.Error())
}

View File

@ -26,15 +26,31 @@ func PathExists(path string) (bool, error) {
// FileExist returns whether the given file exists or not
func FileExist(fileName string) (bool, error) {
f, err := os.Stat(fileName)
return exists(fileName, false)
}
// DirExists returns whether the given file exists or not
func DirExists(path string) (bool, error) {
return exists(path, true)
}
func exists(path string, expectDir bool) (bool, error) {
f, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return false, nil
} else if err.(*os.PathError).Err.Error() == "not a directory" {
// some middle segment of path is a file, cannot traverse
// FIXME: catches error on linux; go does not provide a way to catch this properly..
return false, nil
}
return false, err
}
if f.IsDir() {
isDir := f.IsDir()
if isDir && !expectDir {
return false, errors.New("A directory with the same name exists")
} else if !isDir && expectDir {
return false, errors.New("A file with the same name exists")
}
return true, nil
}