diff --git a/README.md b/README.md index 62e0a2b..eb0753b 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,11 @@ folder. On Windows, this folder is `%LOCALAPPDATA%\sni`. -On MacOS, this folder is `~/.sni/`. +On MacOS and Linux, this folder is `$XDG_CONFIG_HOME/sni/` or `$HOME/.config/sni/` with `$HOME/.sni/` as a fallback. +SNI uses the following order when choosing a config folder: +1. `$HOME/.sni/` if it already exists +2. `$XDG_CONFIG_HOME/sni/` if `$XDG_CONFIG_HOME` is set (not empty) +3. `$HOME/.config/sni/` During start-up, in the console window, SNI will output where the current log file is located at: diff --git a/cmd/sni/config/config.go b/cmd/sni/config/config.go index 867f31a..f5a75ec 100644 --- a/cmd/sni/config/config.go +++ b/cmd/sni/config/config.go @@ -86,6 +86,16 @@ func InitDir() { return } Dir = filepath.Join(Dir, ".sni") + + // Follow XDG Base Directory Specification + if _, err := os.Stat(Dir); err != nil { + var xdgConfig = os.Getenv("XDG_CONFIG_HOME") + if xdgConfig == "" { + homeDir, _ := os.UserHomeDir() + xdgConfig = filepath.Join(homeDir, ".config") + } + Dir = filepath.Join(xdgConfig, "sni") + } } // make the directory if it doesn't exist: _ = os.MkdirAll(Dir, 0755|os.ModeDir)