Skip to content

Commit 710c010

Browse files
author
PanDoes
committed
add replace data dir with XDG_CACHE
Uses old $HOME/.mal data dir when available but automatically switches to $XDG_CACHE_HOME/mal whenever possible. Updates readme to reflect these changes
1 parent 0a66d2b commit 710c010

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ In order to have `mal copy` command working, you need to have either `xsel` or `
1515

1616
## Quick start
1717

18-
If you have a working Go environment, you can download the app via `go get -u github.com/aqatl/mal`.
18+
If you have a working Go environment, you can download the app via `go get -u github.com/aqatl/mal`.
1919
Otherwise, download binaries from the [release](https://github.com/aQaTL/MAL/releases) page.
2020

21-
Remember that everything is stored in `$HOME/.mal` or `%userprofile%\.mal` (Windows).
21+
Remember that everything is stored in `$XDG_CACHE_HOME/mal` or `%LocalAppData%\mal` (Windows).
2222

2323
### AniList mode
2424

25-
AniList mode is used by default. All you need to do to configure the app is to simply execute the program.
26-
It'll open AniList login page in your browser. Log in and authorize the app. And that's it - mal will cache
27-
the received token on your disk and use it to authenticate your requests.
25+
AniList mode is used by default. All you need to do to configure the app is to simply execute the program.
26+
It'll open AniList login page in your browser. Log in and authorize the app. And that's it - mal will cache
27+
the received token on your disk and use it to authenticate your requests.
2828

2929
Run mal with `-r` flag to refresh cached lists.
3030

@@ -34,14 +34,14 @@ Run mal with `-r` flag to refresh cached lists.
3434

3535
To switch between AniList and MyAnimeList mode use the `s` command (e.g. `mal s`).
3636

37-
First, you need to give the app your credentials - username and password. To do that, execute
38-
`mal --prompt-credentials --verify --save-password`. If everything went good, you should see
37+
First, you need to give the app your credentials - username and password. To do that, execute
38+
`mal --prompt-credentials --verify --save-password`. If everything went good, you should see
3939
a list of 10 entries.
4040

4141
### Default behavior
4242

43-
The base command for everything is `mal`, which by default displays 10 last updated entries
44-
from your MAL. You can change the displayed list through some flags:
43+
The base command for everything is `mal`, which by default displays 10 last updated entries
44+
from your MAL. You can change the displayed list through some flags:
4545

4646
```
4747
--max value visible entries threshold (default: 0)
@@ -53,7 +53,7 @@ from your MAL. You can change the displayed list through some flags:
5353

5454
It's also good to run the app with `-r` (or `--refresh`) to update the cached list. Mind that there is not refresh interval so you have to refresh manually.
5555

56-
List of all commands and possible flags is available via `mal --help`.
56+
List of all commands and possible flags is available via `mal --help`.
5757

5858
### Commands
5959

@@ -66,14 +66,14 @@ Commands listed in `help` are divides into categories:
6666
* **Action** command performs action that uses the entry data like printing it to the console
6767
* **Config** command manipulates on the app configuration file (look at `mal cfg --help` for details)
6868

69-
You can always see the details of the specific command via `help` like this:
69+
You can always see the details of the specific command via `help` like this:
7070
`mal <command> --help`
7171

7272
#### Select entry to work with
7373

74-
Commands that use entry data need to know which entry you want to use. And there's a thing
75-
called "selected entry". To select an entry, use the `mal sel` command. And here's a usage
76-
of that command (` mal sel --help`):
74+
Commands that use entry data need to know which entry you want to use. And there's a thing
75+
called "selected entry". To select an entry, use the `mal sel` command. And here's a usage
76+
of that command (` mal sel --help`):
7777

7878
```
7979
NAME:
@@ -91,7 +91,7 @@ If `sel` is given no arguments, it will open a fuzzy search cui (console gui).
9191

9292
#### Update entry
9393

94-
For now, you can update your entry with the following commands:
94+
For now, you can update your entry with the following commands:
9595

9696
```
9797
eps, episodes Set the watched episodes value. If n not specified, the number will be increased by one
@@ -114,8 +114,8 @@ CATEGORY:
114114
Update
115115
```
116116

117-
There's an option to have mal automatically turn the entry status to completed after updating
118-
the watched episodes value. To do that, use the `status-auto-update` config command.
117+
There's an option to have mal automatically turn the entry status to completed after updating
118+
the watched episodes value. To do that, use the `status-auto-update` config command.
119119

120120
```
121121
NAME:
@@ -127,11 +127,11 @@ USAGE:
127127

128128
As you can see, there are 2 modes of auto-update: normal and after-threshold.
129129

130-
The first behaves as you would expect -> the status is changes when entry has 12 episodes
130+
The first behaves as you would expect -> the status is changes when entry has 12 episodes
131131
and you hit the 12 watched episodes.
132132

133-
As for the `after-threshold`, the status will change after you exceed the number of
134-
episodes. For example: when entry has 12 episodes and you hit 13 -> status is changed to
133+
As for the `after-threshold`, the status will change after you exceed the number of
134+
episodes. For example: when entry has 12 episodes and you hit 13 -> status is changed to
135135
completed and your watched entries value is changed back to 12.
136136

137137
##### `mal score` command
@@ -173,16 +173,16 @@ Remember that everything is in `--help` :)
173173

174174
### Everyday usage
175175

176-
Okay, so when I add a new anime to my list, I run `mal -r` to update the cache. Then, if I
177-
want to watch it, I select it with `mal sel [name]`. Then I go to the web browser to find a
178-
website where I can watch it. If the name is long, I copy the title with `mal copy title`.
179-
To not forget the website and make it a little bit more convenient for me in the future, I
180-
copy the website's link and bind it to the selected anime with `mal web [website url]`.
176+
Okay, so when I add a new anime to my list, I run `mal -r` to update the cache. Then, if I
177+
want to watch it, I select it with `mal sel [name]`. Then I go to the web browser to find a
178+
website where I can watch it. If the name is long, I copy the title with `mal copy title`.
179+
To not forget the website and make it a little bit more convenient for me in the future, I
180+
copy the website's link and bind it to the selected anime with `mal web [website url]`.
181181

182-
Now, when I want to watch it, I can just type `mal web` and it will open saved url in the
183-
web browser (you can configure which browser to use). When I finish an episode I type
184-
`mal eps` to update watched episodes and that's it. There's an option to automatically set
185-
the status to "completed", so I don't have to do anything more.
182+
Now, when I want to watch it, I can just type `mal web` and it will open saved url in the
183+
web browser (you can configure which browser to use). When I finish an episode I type
184+
`mal eps` to update watched episodes and that's it. There's an option to automatically set
185+
the status to "completed", so I don't have to do anything more.
186186

187187
Oh, and usually I also rate the show by `mal score [number from 0 to 10]`.
188188

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"path/filepath"
1010
)
1111

12-
var dataDir = filepath.Join(homeDir(), ".mal")
12+
var dataDir = getDataDir()
1313
var (
1414
AppConfigFile = filepath.Join(dataDir, "appConfig.json")
1515

utils.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"os/user"
1313
"time"
14+
"path/filepath"
1415
)
1516

1617
func basicAuth(username, password string) string {
@@ -24,14 +25,27 @@ func reverseAnimeSlice(s []*mal.Anime) {
2425
}
2526
}
2627

27-
func homeDir() string {
28+
func getDataDir() string {
29+
// Check for old cache dir at $HOME/.mal
2830
usr, err := user.Current()
2931
if err != nil {
30-
log.Printf("Error getting current user: %v", err)
31-
return ""
32+
log.Printf("Error getting current user: %v. ignoring", err)
33+
} else {
34+
oldDir := filepath.Join(usr.HomeDir, ".mal")
35+
_, err := os.Stat(oldDir)
36+
if err == nil { return oldDir }
37+
if os.IsExist(err) {
38+
log.Printf("Error checking for old cache dir: %v, ignoring", err)
39+
}
3240
}
3341

34-
return usr.HomeDir
42+
// Old dir isn't there, use new $XDG_CACHE_HOME/mal
43+
dir, err := os.UserCacheDir()
44+
if err != nil {
45+
log.Printf("Error getting cache dir: %v", err)
46+
return ""
47+
}
48+
return filepath.Join(dir, "mal")
3549
}
3650

3751
func chooseStrFromSlice(alts []string) string {

0 commit comments

Comments
 (0)