aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/go-autorest/autorest/azure/persist.go
blob: d5cf62ddc7babf24061d420c0a659e6579091097 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
}