configdir icon indicating copy to clipboard operation
configdir copied to clipboard

Fix check if config file exists. Directory was detected as file

Open dol opened this issue 7 years ago • 1 comments

'os.Stat' checks if a file or directory exists. By adding an additional check (mode) the distinction could be made.

By checking !os.IsNotExist(err) could also go wrong, due to other types of error that can occur. It's best IMHO to check if there is no error.

E.g on Linux:

mkdir -p ~/.config/vendor/app/foobar
...
configDirs := configdir.New("vendor", "app")

config := configDirs.QueryFolders(configdir.Global)
if config.Exists("foobar") {
  // Path ~/.config/vendor/app/foobar is a directory
  fmt.Println("file exists")
}
// 

dol avatar Apr 05 '17 22:04 dol

I don't think Exists is meant to only check for file existence. In pretty much every language, similarly-named functions return true for both files and directories. (Also, IsRegular is too strict; what if it's a symlink? I regularly symlink config files into my configuration Git repo.)

andreyvit avatar Sep 06 '17 08:09 andreyvit