configdir
configdir copied to clipboard
Fix check if config file exists. Directory was detected as file
'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")
}
//
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.)