/
settings.go
130 lines (116 loc) · 3.18 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package crane
import (
"encoding/json"
"errors"
"fmt"
uuid "github.com/hashicorp/go-uuid"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"time"
)
var settings *Settings
type Settings struct {
filename string
UUID string `json:"uuid"`
Version string `json:"version"`
LatestVersion string `json:"latest_version"`
NextUpdateCheck time.Time `json:"next_update_check"`
CheckForUpdates bool `json:"check_for_updates"`
}
// Determine crane settings base path.
// On windows, this is %APPDATA%\\crane
// On unix, this is ${XDG_CONFIG_HOME}/crane (which usually
// is ${HOME}/.config)
func settingsPath() (string, error) {
settingsPath := os.Getenv("CRANE_SETTINGS_PATH")
if len(settingsPath) > 0 {
return settingsPath, nil
}
if runtime.GOOS == "windows" {
settingsPath = os.Getenv("APPDATA")
if len(settingsPath) > 0 {
return fmt.Sprintf("%s/crane", settingsPath), nil
}
return "", errors.New("Cannot detect settings path!")
}
settingsPath = os.Getenv("XDG_CONFIG_HOME")
if len(settingsPath) > 0 {
return fmt.Sprintf("%s/crane", settingsPath), nil
}
homeDir := os.Getenv("HOME")
if len(homeDir) > 0 {
return fmt.Sprintf("%s/.config/crane", homeDir), nil
}
return "", errors.New("Cannot detect settings path!")
}
func createSettings(filename string) error {
uuid, _ := uuid.GenerateUUID()
settings = &Settings{
filename: filename,
UUID: uuid,
Version: Version,
LatestVersion: Version,
NextUpdateCheck: time.Now().Add(autoUpdateCheckInterval()),
CheckForUpdates: true,
}
msg := fmt.Sprintf("Writing settings file to %s\n", filename)
printInfof(msg)
return settings.Write(filename)
}
func readSettings() error {
// Determine settings path
sp, err := settingsPath()
if err != nil {
return err
}
// Create settings path if it does not exist yet
if _, err := os.Stat(sp); err != nil {
os.MkdirAll(sp, os.ModePerm)
if _, err := os.Stat(sp); err != nil {
return err
}
}
// Create settings file if it does not exist yet
filename := filepath.Join(sp, "config.json")
if _, err := os.Stat(filename); err != nil {
return createSettings(filename)
}
// read settings of file
settings = &Settings{filename: filename}
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return json.Unmarshal(data, settings)
}
func (s *Settings) ShouldCheckForUpdates() bool {
if !s.CheckForUpdates {
return false
}
return time.Now().After(settings.NextUpdateCheck)
}
// If version in settings does not match version of binary,
// we assume that the binary was updated and update the
// settings file with the new information.
func (s *Settings) CorrectVersion() error {
if Version != s.Version {
s.Version = Version
return s.Update(Version)
}
return nil
}
func (s *Settings) Update(latestVersion string) error {
s.NextUpdateCheck = time.Now().Add(autoUpdateCheckInterval())
s.LatestVersion = latestVersion
return s.Write(s.filename)
}
func (s *Settings) DelayNextUpdateCheck() error {
s.NextUpdateCheck = time.Now().Add(time.Hour)
return s.Write(s.filename)
}
func (s *Settings) Write(filename string) error {
contents, _ := json.Marshal(s)
return ioutil.WriteFile(filename, contents, 0644)
}