-
Notifications
You must be signed in to change notification settings - Fork 7
/
settings.go
86 lines (72 loc) · 1.93 KB
/
settings.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
)
var (
settingPath string
)
type settings struct {
ActiveTenant string `json:"activeTenant"`
}
func defaultSettingsPath() string {
if settingPath != "" {
return settingPath
}
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%s/.armclient/settings.json", usr.HomeDir)
}
func setDefaultSettingsPath(path string) {
settingPath = path
}
func saveSettings(setting settings) error {
path := defaultSettingsPath()
dir := filepath.Dir(path)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory %s: %v", dir, err)
}
newFile, err := ioutil.TempFile(dir, "tmp")
if err != nil {
return fmt.Errorf("failed to create the temp file: %v", err)
}
tempPath := newFile.Name()
if err := json.NewEncoder(newFile).Encode(setting); err != nil {
return fmt.Errorf("failed to encode to file %s: %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 file to desired output location. src=%s dst=%s: %v", tempPath, path, err)
}
if err := os.Chmod(path, 0600); err != nil {
return fmt.Errorf("failed to chmod the file %s: %v", path, err)
}
return nil
}
func readSettings() (setting settings, err error) {
path := defaultSettingsPath()
if _, err := os.Stat(path); err == nil {
file, err := os.Open(path)
if err != nil {
return settings{}, fmt.Errorf("failed to open file %s: %v", path, err)
}
defer file.Close()
dec := json.NewDecoder(file)
if err = dec.Decode(&setting); err != nil {
return settings{}, fmt.Errorf("failed to decode contents of file %s: %v", path, err)
}
return setting, nil
}
return settings{}, nil
}