-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcache.go
44 lines (37 loc) · 803 Bytes
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"os/user"
"path/filepath"
"strconv"
"fmt"
"os"
)
func getCachePath(path... string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
cachePath := filepath.Join(usr.HomeDir, ".pokevision-cli")
if len(path) > 0 {
cachePath = filepath.Join(cachePath, filepath.Join(path...))
}
return cachePath, nil;
}
func existsInCache(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}
func DownloadIcon(index int) (string, error) {
path, _ := getCachePath("icons", strconv.Itoa(index) + ".png")
if !existsInCache(path) {
url := fmt.Sprintf(POKEDEX_POKEMON_ICON_URL, index)
err := RequestToPath(url, path)
if err != nil {
return "", err
}
return path, nil
}
return path, nil
}