This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git


The following commit(s) were added to refs/heads/master by this push:
     new ace8e64  config: Implement per profile based api cache (#39)
ace8e64 is described below

commit ace8e64528bce5426164240ccf02c2d18dc5acb7
Author: Rohit Yadav <[email protected]>
AuthorDate: Tue Mar 5 14:18:18 2019 +0530

    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 <[email protected]>
---
 cmd/set.go       |  2 ++
 config/cache.go  | 10 ++++++----
 config/config.go | 35 ++++++++++++++++++++++++-----------
 3 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/cmd/set.go b/cmd/set.go
index 2580a62..4e2c490 100644
--- a/cmd/set.go
+++ b/cmd/set.go
@@ -57,6 +57,8 @@ func init() {
                                fmt.Println("Username:   ", 
r.Config.ActiveProfile.Username)
                                fmt.Println("Domain:     ", 
r.Config.ActiveProfile.Domain)
                                fmt.Println("API Key:    ", 
r.Config.ActiveProfile.APIKey)
+                               fmt.Println("Total APIs: ", 
len(r.Config.GetCache()))
+
                                fmt.Println()
                        }
                        return nil
diff --git a/config/cache.go b/config/cache.go
index 4320c61..34e021c 100644
--- a/config/cache.go
+++ b/config/cache.go
@@ -79,8 +79,10 @@ func (c *Config) GetCache() map[string]*API {
 }
 
 // LoadCache loads cache using the default cache file
-func LoadCache(c *Config) {
-       cache, err := ioutil.ReadFile(c.CacheFile)
+func LoadCache(c *Config) interface{} {
+       cacheFile := c.CacheFile()
+       Debug("Trying to read API cache from:", cacheFile)
+       cache, err := ioutil.ReadFile(cacheFile)
        if err != nil {
                if c.HasShell {
                        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) {
        }
        var data map[string]interface{}
        _ = json.Unmarshal(cache, &data)
-       c.UpdateCache(data)
+       return c.UpdateCache(data)
 }
 
 // SaveCache saves received auto-discovery data to cache file
 func (c *Config) SaveCache(response map[string]interface{}) {
        output, _ := json.Marshal(response)
-       ioutil.WriteFile(c.CacheFile, output, 0600)
+       ioutil.WriteFile(c.CacheFile(), output, 0600)
 }
 
 // UpdateCache uses auto-discovery data to update internal API cache
diff --git a/config/config.go b/config/config.go
index 56f7c37..4c4f0d5 100644
--- a/config/config.go
+++ b/config/config.go
@@ -68,28 +68,41 @@ type Config struct {
        Dir           string
        ConfigFile    string
        HistoryFile   string
-       CacheFile     string
        LogFile       string
        HasShell      bool
        Core          *Core
        ActiveProfile *ServerProfile
 }
 
-func getDefaultConfigDir() string {
-       home, err := homedir.Dir()
-       if err != nil {
-               fmt.Println(err)
-               os.Exit(1)
+// CacheFile returns the path to the cache file for a server profile
+func (c Config) CacheFile() string {
+       cacheDir := path.Join(c.Dir, "profiles")
+       cacheFileName := "cache"
+       if c.Core != nil && len(c.Core.ProfileName) > 0 {
+               cacheFileName = c.Core.ProfileName + ".cache"
        }
-       cmkHome := path.Join(home, ".cmk")
-       if _, err := os.Stat(cmkHome); os.IsNotExist(err) {
-               err := os.Mkdir(cmkHome, 0700)
+       checkAndCreateDir(cacheDir)
+       return path.Join(cacheDir, cacheFileName)
+}
+
+func checkAndCreateDir(path string) string {
+       if fileInfo, err := os.Stat(path); os.IsNotExist(err) || 
!fileInfo.IsDir() {
+               err := os.Mkdir(path, 0700)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(1)
                }
        }
-       return cmkHome
+       return path
+}
+
+func getDefaultConfigDir() string {
+       home, err := homedir.Dir()
+       if err != nil {
+               fmt.Println(err)
+               os.Exit(1)
+       }
+       return checkAndCreateDir(path.Join(home, ".cmk"))
 }
 
 func defaultCoreConfig() Core {
@@ -122,7 +135,6 @@ func defaultConfig() *Config {
        return &Config{
                Dir:           configDir,
                ConfigFile:    path.Join(configDir, "config"),
-               CacheFile:     path.Join(configDir, "cache"),
                HistoryFile:   path.Join(configDir, "history"),
                LogFile:       path.Join(configDir, "log"),
                HasShell:      false,
@@ -164,6 +176,7 @@ func reloadConfig(cfg *Config) *Config {
        }
        cfg = saveConfig(cfg)
        fileLock.Unlock()
+       LoadCache(cfg)
        return cfg
 }
 

Reply via email to