diff --git a/Makefile b/Makefile index bff8a0c31f..f96c8e7450 100644 --- a/Makefile +++ b/Makefile @@ -159,7 +159,7 @@ convey: .PHONY: travis_login travis_login: @if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \ - echo '{"api_endpoint":"https://api.scaleway.com/","account_endpoint":"https://account.scaleway.com/","organization":"$(TRAVIS_SCALEWAY_ORGANIZATION)","token":"$(TRAVIS_SCALEWAY_TOKEN)"}' > ~/.scwrc && \ + echo '{"organization":"$(TRAVIS_SCALEWAY_ORGANIZATION)","token":"$(TRAVIS_SCALEWAY_TOKEN)"}' > ~/.scwrc && \ chmod 600 ~/.scwrc; \ else \ echo "Cannot login, credentials are missing"; \ diff --git a/README.md b/README.md index c047f4d5f3..ae7a1cb060 100644 --- a/README.md +++ b/README.md @@ -1184,6 +1184,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) +* Clear cache between the releases ([#329](https://github.com/scaleway/scaleway-cli/issues/329) * Fix `scw _patch bootscript` nil dereference * Fix `scw images` bad error message ([#336](https://github.com/scaleway/scaleway-cli/issues/337)) * Fix sshExecCommand with Windows ([#338](https://github.com/scaleway/scaleway-cli/issues/338)) diff --git a/pkg/api/api.go b/pkg/api/api.go index e96bc94bf5..d015ef52b5 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -860,6 +860,11 @@ func NewScalewayAPI(organization, token, userAgent string) (*ScalewayAPI, error) return s, nil } +// ClearCache clears the cache +func (s *ScalewayAPI) ClearCache() { + s.Cache.Clear() +} + // Sync flushes out the cache to the disk func (s *ScalewayAPI) Sync() { s.Cache.Save() diff --git a/pkg/api/cache.go b/pkg/api/cache.go index de4b97ed25..44e566cd24 100644 --- a/pkg/api/cache.go +++ b/pkg/api/cache.go @@ -158,6 +158,8 @@ REDO: // NewScalewayCache loads a per-user cache func NewScalewayCache() (*ScalewayCache, error) { + var cache ScalewayCache + homeDir := os.Getenv("HOME") // *nix if homeDir == "" { // Windows homeDir = os.Getenv("USERPROFILE") @@ -166,16 +168,11 @@ func NewScalewayCache() (*ScalewayCache, error) { homeDir = "/tmp" } cachePath := filepath.Join(homeDir, ".scw-cache.db") + cache.Path = cachePath _, err := os.Stat(cachePath) if os.IsNotExist(err) { - return &ScalewayCache{ - Images: make(map[string][CacheMaxfield]string), - Snapshots: make(map[string][CacheMaxfield]string), - Volumes: make(map[string][CacheMaxfield]string), - Bootscripts: make(map[string][CacheMaxfield]string), - Servers: make(map[string][CacheMaxfield]string), - Path: cachePath, - }, nil + cache.Clear() + return &cache, nil } else if err != nil { return nil, err } @@ -183,23 +180,14 @@ func NewScalewayCache() (*ScalewayCache, error) { if err != nil { return nil, err } - var cache ScalewayCache - - cache.Path = cachePath err = json.Unmarshal(file, &cache) if err != nil { // fix compatibility with older version if err = os.Remove(cachePath); err != nil { return nil, err } - return &ScalewayCache{ - Images: make(map[string][CacheMaxfield]string), - Snapshots: make(map[string][CacheMaxfield]string), - Volumes: make(map[string][CacheMaxfield]string), - Bootscripts: make(map[string][CacheMaxfield]string), - Servers: make(map[string][CacheMaxfield]string), - Path: cachePath, - }, nil + cache.Clear() + return &cache, nil } if cache.Images == nil { cache.Images = make(map[string][CacheMaxfield]string) @@ -219,6 +207,16 @@ func NewScalewayCache() (*ScalewayCache, error) { return &cache, nil } +// Clear removes all information from the cache +func (s *ScalewayCache) Clear() { + s.Images = make(map[string][CacheMaxfield]string) + s.Snapshots = make(map[string][CacheMaxfield]string) + s.Volumes = make(map[string][CacheMaxfield]string) + s.Bootscripts = make(map[string][CacheMaxfield]string) + s.Servers = make(map[string][CacheMaxfield]string) + s.Modified = true +} + // Flush flushes the cache database func (c *ScalewayCache) Flush() error { return os.Remove(c.Path) diff --git a/pkg/cli/main.go b/pkg/cli/main.go index a0ec380223..78e0c40044 100644 --- a/pkg/cli/main.go +++ b/pkg/cli/main.go @@ -109,6 +109,11 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) { } cmd.API = api } + // clean cache between versions + if cmd.API != nil && config.Version != scwversion.VERSION { + cmd.API.ClearCache() + config.Save() + } err = cmd.Exec(cmd, cmd.Flag.Args()) switch err { case nil: diff --git a/pkg/config/config.go b/pkg/config/config.go index 9a6897831e..dfadbbc1ca 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,21 +13,20 @@ import ( "os" "path/filepath" "runtime" + + "github.com/scaleway/scaleway-cli/pkg/scwversion" ) // Config is a Scaleway CLI configuration file type Config struct { - // ComputeAPI is the endpoint to the Scaleway API - ComputeAPI string `json:"api_endpoint"` - - // AccountAPI is the endpoint to the Scaleway Account API - AccountAPI string `json:"account_endpoint"` - // Organization is the identifier of the Scaleway orgnization Organization string `json:"organization"` // Token is the authentication token for the Scaleway organization Token string `json:"token"` + + // Version is the actual version of scw + Version string `json:"version"` } // Save write the config file @@ -42,6 +41,7 @@ func (c *Config) Save() error { } defer scwrc.Close() encoder := json.NewEncoder(scwrc) + c.Version = scwversion.VERSION err = encoder.Encode(c) if err != nil { return fmt.Errorf("Unable to encode scw config file: %s", err) @@ -74,15 +74,11 @@ func GetConfig() (*Config, error) { return nil, err } var config Config + err = json.Unmarshal(file, &config) if err != nil { return nil, err } - // check if he has an old scwrc version - if config.AccountAPI == "" { - config.AccountAPI = "https://account.scaleway.com" - config.Save() - } return &config, nil }