forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_cache.go
54 lines (48 loc) · 1.2 KB
/
plugin_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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
var pluginCachePath = filepath.Join(AppDir(), "plugin-cache.json")
// AddPluginsToCache adds/updates a set of plugins to ~/.heroku/plugin-cache.json
func AddPluginsToCache(plugins ...*Plugin) {
cache := FetchPluginCache()
for _, plugin := range plugins {
if plugin != nil {
cache[plugin.Name] = plugin
}
}
savePluginCache(cache)
}
// RemovePluginFromCache will take a plugin and remove it from the list
func RemovePluginFromCache(name string) {
cache := FetchPluginCache()
delete(cache, name)
savePluginCache(cache)
}
func savePluginCache(cache map[string]*Plugin) {
data, err := json.MarshalIndent(cache, "", " ")
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(pluginCachePath, data, 0644); err != nil {
panic(err)
}
}
// FetchPluginCache returns the plugins from the cache
func FetchPluginCache() map[string]*Plugin {
plugins := make(map[string]*Plugin, 100)
if exists, _ := fileExists(pluginCachePath); !exists {
return plugins
}
f, err := os.Open(pluginCachePath)
if err != nil {
return plugins
}
if err := json.NewDecoder(f).Decode(&plugins); err != nil {
WarnIfError(err)
}
return plugins
}