Skip to content

Commit

Permalink
Cleanup getDataDir. Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
aQaTL committed Aug 2, 2022
1 parent 4b04f64 commit 3680453
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -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

Expand Down
37 changes: 20 additions & 17 deletions utils.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 3680453

Please sign in to comment.