-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
81 lines (74 loc) · 2.37 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package eoldate
import (
"fmt"
"os"
"path/filepath"
"time"
)
// readCache reads the cached data for a product
func readCache(product string) ([]byte, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, LogError(err)
}
timestamp := time.Now().Format("01-02-2006")
cacheDir := filepath.Join(homeDir, ".config", "eoldate", "cache")
cacheFile := fmt.Sprintf("%s/%s-%s.json", cacheDir, product, timestamp)
if exists, err := Exists(cacheFile); err == nil && exists {
return os.ReadFile(cacheFile)
} else {
return nil, nil
}
}
// readAllTechnologiesCache ...
func readAllTechnologiesCache() ([]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, LogError(err)
}
timestamp := time.Now().Format("01-02-2006")
cacheDir := filepath.Join(homeDir, ".config", "eoldate", "cache")
cacheAllTechFile := fmt.Sprintf("%s/all-technologies-%s.json", cacheDir, timestamp)
if exists, err := Exists(cacheAllTechFile); err == nil && exists {
return ReadLines(cacheAllTechFile)
} else {
return nil, nil
}
}
// writeCache writes data to the cache for a product
func writeCache(product string, data []byte) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return LogError(err)
}
timestamp := time.Now().Format("01-02-2006")
cacheDir := filepath.Join(homeDir, ".config", "eoldate", "cache")
cacheFile := fmt.Sprintf("%s/%s-%s.json", cacheDir, product, timestamp)
return os.WriteFile(cacheFile, data, 0600)
}
// CacheTechnologies caches all available technologies to choose from to a local file cache
func (c *Client) CacheTechnologies() ([]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, LogError(err)
}
timestamp := time.Now().Format("01-02-2006")
cacheDir := filepath.Join(homeDir, ".config", "eoldate", "cache")
allTechnologiesFileCache := fmt.Sprintf("%s/all-technologies-%s.json", cacheDir, timestamp)
if exists, err := Exists(cacheDir); err == nil && !exists {
if err = os.MkdirAll(cacheDir, 0755); err != nil {
return nil, LogError(err)
}
}
if cacheExists, err := Exists(allTechnologiesFileCache); err == nil && cacheExists {
return ReadLines(allTechnologiesFileCache)
}
allProducts, err := c.GetAllProducts()
if err != nil {
return nil, LogError(err)
}
if err = WriteLines(allProducts, allTechnologiesFileCache); err != nil {
return nil, LogError(err)
}
return allProducts, nil
}