Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear cache between the releases #350

Merged
merged 2 commits into from
Apr 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"; \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
36 changes: 17 additions & 19 deletions pkg/api/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -166,40 +168,26 @@ 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
}
file, err := ioutil.ReadFile(cachePath)
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)
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 7 additions & 11 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down