Skip to content

Commit ace8e64

Browse files
committed
config: Implement per profile based api cache (#39)
This fixes #39 and implements logic to have separate api cache on per profile basis. On changing server profile, user won't need to run sync, any previously sync-ed cache will be re-used. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 603425f commit ace8e64

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

cmd/set.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func init() {
5757
fmt.Println("Username: ", r.Config.ActiveProfile.Username)
5858
fmt.Println("Domain: ", r.Config.ActiveProfile.Domain)
5959
fmt.Println("API Key: ", r.Config.ActiveProfile.APIKey)
60+
fmt.Println("Total APIs: ", len(r.Config.GetCache()))
61+
6062
fmt.Println()
6163
}
6264
return nil

config/cache.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ func (c *Config) GetCache() map[string]*API {
7979
}
8080

8181
// LoadCache loads cache using the default cache file
82-
func LoadCache(c *Config) {
83-
cache, err := ioutil.ReadFile(c.CacheFile)
82+
func LoadCache(c *Config) interface{} {
83+
cacheFile := c.CacheFile()
84+
Debug("Trying to read API cache from:", cacheFile)
85+
cache, err := ioutil.ReadFile(cacheFile)
8486
if err != nil {
8587
if c.HasShell {
8688
fmt.Fprintf(os.Stderr, "Loaded in-built API cache. Failed to read API cache, please run 'sync'.\n")
@@ -89,13 +91,13 @@ func LoadCache(c *Config) {
8991
}
9092
var data map[string]interface{}
9193
_ = json.Unmarshal(cache, &data)
92-
c.UpdateCache(data)
94+
return c.UpdateCache(data)
9395
}
9496

9597
// SaveCache saves received auto-discovery data to cache file
9698
func (c *Config) SaveCache(response map[string]interface{}) {
9799
output, _ := json.Marshal(response)
98-
ioutil.WriteFile(c.CacheFile, output, 0600)
100+
ioutil.WriteFile(c.CacheFile(), output, 0600)
99101
}
100102

101103
// UpdateCache uses auto-discovery data to update internal API cache

config/config.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,41 @@ type Config struct {
6868
Dir string
6969
ConfigFile string
7070
HistoryFile string
71-
CacheFile string
7271
LogFile string
7372
HasShell bool
7473
Core *Core
7574
ActiveProfile *ServerProfile
7675
}
7776

78-
func getDefaultConfigDir() string {
79-
home, err := homedir.Dir()
80-
if err != nil {
81-
fmt.Println(err)
82-
os.Exit(1)
77+
// CacheFile returns the path to the cache file for a server profile
78+
func (c Config) CacheFile() string {
79+
cacheDir := path.Join(c.Dir, "profiles")
80+
cacheFileName := "cache"
81+
if c.Core != nil && len(c.Core.ProfileName) > 0 {
82+
cacheFileName = c.Core.ProfileName + ".cache"
8383
}
84-
cmkHome := path.Join(home, ".cmk")
85-
if _, err := os.Stat(cmkHome); os.IsNotExist(err) {
86-
err := os.Mkdir(cmkHome, 0700)
84+
checkAndCreateDir(cacheDir)
85+
return path.Join(cacheDir, cacheFileName)
86+
}
87+
88+
func checkAndCreateDir(path string) string {
89+
if fileInfo, err := os.Stat(path); os.IsNotExist(err) || !fileInfo.IsDir() {
90+
err := os.Mkdir(path, 0700)
8791
if err != nil {
8892
fmt.Println(err)
8993
os.Exit(1)
9094
}
9195
}
92-
return cmkHome
96+
return path
97+
}
98+
99+
func getDefaultConfigDir() string {
100+
home, err := homedir.Dir()
101+
if err != nil {
102+
fmt.Println(err)
103+
os.Exit(1)
104+
}
105+
return checkAndCreateDir(path.Join(home, ".cmk"))
93106
}
94107

95108
func defaultCoreConfig() Core {
@@ -122,7 +135,6 @@ func defaultConfig() *Config {
122135
return &Config{
123136
Dir: configDir,
124137
ConfigFile: path.Join(configDir, "config"),
125-
CacheFile: path.Join(configDir, "cache"),
126138
HistoryFile: path.Join(configDir, "history"),
127139
LogFile: path.Join(configDir, "log"),
128140
HasShell: false,
@@ -164,6 +176,7 @@ func reloadConfig(cfg *Config) *Config {
164176
}
165177
cfg = saveConfig(cfg)
166178
fileLock.Unlock()
179+
LoadCache(cfg)
167180
return cfg
168181
}
169182

0 commit comments

Comments
 (0)