From 3680453fcf121c397e0b10fe8fd49adc5b47e90b Mon Sep 17 00:00:00 2001 From: aQaTL Date: Tue, 2 Aug 2022 23:22:10 +0200 Subject: [PATCH] Cleanup getDataDir. Update README --- README.md | 6 +++++- utils.go | 37 ++++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f670873..20b3da9 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,11 @@ In order to have `mal copy` command working, you need to have either `xsel` or ` If you have a working Go environment, you can download the app via `go get -u github.com/aqatl/mal`. Otherwise, download binaries from the [release](https://github.com/aQaTL/MAL/releases) page. -Remember that everything is stored in `$XDG_CACHE_HOME/mal` or `%LocalAppData%\mal` (Windows). +Config files location: + +1. Linux: `$XDG_CONFIG_DIR/mal` (`$HOME/.config/mal` if `$XDG_CONFIG_DIR` env var is not set) . +2. Windows: `%AppData%\mal` +3. MacOS: `$HOME/Library/Application Support/mal` ### AniList mode diff --git a/utils.go b/utils.go index ce6203c..977721b 100644 --- a/utils.go +++ b/utils.go @@ -4,14 +4,15 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/aqatl/mal/anilist" - "github.com/aqatl/mal/mal" - "github.com/fatih/color" "log" "os" "os/user" - "time" "path/filepath" + "time" + + "github.com/aqatl/mal/anilist" + "github.com/aqatl/mal/mal" + "github.com/fatih/color" ) func basicAuth(username, password string) string { @@ -27,25 +28,27 @@ func reverseAnimeSlice(s []*mal.Anime) { func getDataDir() string { // Check for old cache dir at $HOME/.mal - usr, err := user.Current() - if err != nil { - log.Printf("Error getting current user: %v. ignoring", err) - } else { - oldDir := filepath.Join(usr.HomeDir, ".mal") - _, err := os.Stat(oldDir) - if err == nil { return oldDir } - if os.IsExist(err) { - log.Printf("Error checking for old cache dir: %v, ignoring", err) + if usr, err := user.Current(); err == nil { + dir := filepath.Join(usr.HomeDir, ".mal") + if _, err := os.Stat(dir); err == nil { + return dir + } else { + if !os.IsNotExist(err) { + log.Printf("Error probing for %s: %v", dir, err) + } } + } else { + log.Printf("Error getting current user: %v. ignoring", err) } - // Old dir isn't there, use new $XDG_CACHE_HOME/mal - dir, err := os.UserConfigDir() + // Old cache dir not present, use user config dir + dataDir, err := os.UserConfigDir() if err != nil { - log.Printf("Error getting cache dir: %v", err) + log.Printf("Error getting user config dir: %v", err) return "" } - return filepath.Join(dir, "mal") + + return filepath.Join(dataDir, "mal") } func chooseStrFromSlice(alts []string) string {