diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-04-06 18:53:33 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-04-06 18:53:33 +0800 |
commit | c76ad944920300be58446ddd1a50c8d693957774 (patch) | |
tree | ec9aed051e82deb3c479ab1b7ab50aa2c07d9efb /vendor/github.com/Azure/go-autorest/autorest/azure/persist.go | |
parent | 3d8de95f999de6f52f0c1605eb2913278f1d87d2 (diff) | |
download | go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.tar go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.tar.gz go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.tar.bz2 go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.tar.lz go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.tar.xz go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.tar.zst go-tangerine-c76ad944920300be58446ddd1a50c8d693957774.zip |
.travis, build: autodelete old unstable archives (#13867)
This commit adds a build step to travis to auto-delete unstable archives older than
14 days (our regular release schedule) from Azure via ci.go purge.
The commit also pulls in the latest Azure storage code, also switching over from
the old import path (github.com/Azure/azure-sdk-for-go) to the new split one
(github.com/Azure/azure-storage-go).
Diffstat (limited to 'vendor/github.com/Azure/go-autorest/autorest/azure/persist.go')
-rw-r--r-- | vendor/github.com/Azure/go-autorest/autorest/azure/persist.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/vendor/github.com/Azure/go-autorest/autorest/azure/persist.go b/vendor/github.com/Azure/go-autorest/autorest/azure/persist.go new file mode 100644 index 000000000..d5cf62ddc --- /dev/null +++ b/vendor/github.com/Azure/go-autorest/autorest/azure/persist.go @@ -0,0 +1,59 @@ +package azure + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "path/filepath" +) + +// LoadToken restores a Token object from a file located at 'path'. +func LoadToken(path string) (*Token, error) { + file, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("failed to open file (%s) while loading token: %v", path, err) + } + defer file.Close() + + var token Token + + dec := json.NewDecoder(file) + if err = dec.Decode(&token); err != nil { + return nil, fmt.Errorf("failed to decode contents of file (%s) into Token representation: %v", path, err) + } + return &token, nil +} + +// SaveToken persists an oauth token at the given location on disk. +// It moves the new file into place so it can safely be used to replace an existing file +// that maybe accessed by multiple processes. +func SaveToken(path string, mode os.FileMode, token Token) error { + dir := filepath.Dir(path) + err := os.MkdirAll(dir, os.ModePerm) + if err != nil { + return fmt.Errorf("failed to create directory (%s) to store token in: %v", dir, err) + } + + newFile, err := ioutil.TempFile(dir, "token") + if err != nil { + return fmt.Errorf("failed to create the temp file to write the token: %v", err) + } + tempPath := newFile.Name() + + if err := json.NewEncoder(newFile).Encode(token); err != nil { + return fmt.Errorf("failed to encode token to file (%s) while saving token: %v", tempPath, err) + } + if err := newFile.Close(); err != nil { + return fmt.Errorf("failed to close temp file %s: %v", tempPath, err) + } + + // Atomic replace to avoid multi-writer file corruptions + if err := os.Rename(tempPath, path); err != nil { + return fmt.Errorf("failed to move temporary token to desired output location. src=%s dst=%s: %v", tempPath, path, err) + } + if err := os.Chmod(path, mode); err != nil { + return fmt.Errorf("failed to chmod the token file %s: %v", path, err) + } + return nil +} |