config/version: Update_ -> AutoUpdate_
Juan Batiz-Benet committed
Nov 4, 2014 at 18:57 UTC
6ba45221d65de381be00db406bdaa1a0c56d8c11
4 files changed
+57
-61
config/version.go
+23
-27
@@ -42,27 +42,36 @@ const (
42
)
43
44
// AutoUpdateSetting implements json.Unmarshaler to check values in config
45
-// supported values:
46
-// "never" - do not auto-update
47
-// "patch" - auto-update on new patch versions
48
-// "minor" - auto-update on new minor (or patch) versions (Default)
49
-// "major" - auto-update on any new version
45
type AutoUpdateSetting int
46
47
+// AutoUpdateSetting values
48
+const (
49
+ AutoUpdateNever AutoUpdateSetting = iota // do not auto-update
50
+ AutoUpdatePatch // only on new patch versions
51
+ AutoUpdateMinor // on new minor or patch versions (Default)
52
+ AutoUpdateMajor // on all, even Major, version changes
53
+)
54
+
55
+// ErrUnknownAutoUpdateSetting is returned when an unknown value is read from the config
56
+var ErrUnknownAutoUpdateSetting = errors.New("unknown value for AutoUpdate")
57
+
58
+// defaultCheckPeriod governs h
59
+var defaultCheckPeriod = time.Hour * 48
60
+
61
// UnmarshalJSON checks the input against known strings
62
func (s *AutoUpdateSetting) UnmarshalJSON(in []byte) error {
63
64
switch strings.ToLower(string(in)) {
65
case `"never"`:
57
- *s = UpdateNever
66
+ *s = AutoUpdateNever
67
case `"major"`:
59
- *s = UpdateMajor
68
+ *s = AutoUpdateMajor
69
case `"minor"`:
61
- *s = UpdateMinor
70
+ *s = AutoUpdateMinor
71
case `"patch"`:
63
- *s = UpdatePatch
72
+ *s = AutoUpdatePatch
73
default:
65
- *s = UpdateMinor
74
+ *s = AutoUpdateMinor
75
return ErrUnknownAutoUpdateSetting
76
}
77
return nil
@@ -76,32 +85,19 @@ func (s AutoUpdateSetting) MarshalJSON() ([]byte, error) {
85
// String converts valye to human readable string
86
func (s AutoUpdateSetting) String() string {
87
switch s {
79
- case UpdateNever:
88
+ case AutoUpdateNever:
89
return "never"
81
- case UpdateMajor:
90
+ case AutoUpdateMajor:
91
return "major"
83
- case UpdateMinor:
92
+ case AutoUpdateMinor:
93
return "minor"
85
- case UpdatePatch:
94
+ case AutoUpdatePatch:
95
return "patch"
96
default:
97
return ErrUnknownAutoUpdateSetting.Error()
98
}
99
}
100
92
-// ErrUnknownAutoUpdateSetting is returned when an unknown value is read from the config
93
-var ErrUnknownAutoUpdateSetting = errors.New("unknown value for AutoUpdate")
94
-
95
-const (
96
- UpdateMinor AutoUpdateSetting = iota // first value so that it is the zero value and thus the default
97
- UpdatePatch
98
- UpdateMajor
99
- UpdateNever
100
-)
101
-
102
-// defaultCheckPeriod governs h
103
-var defaultCheckPeriod = time.Hour * 48
104
-
101
func (v *Version) checkPeriodDuration() time.Duration {
102
d, err := strconv.Atoi(v.CheckPeriod)
103
if err != nil {
config/version_test.go
+6
-6
@@ -14,12 +14,12 @@ func TestAutoUpdateValues(t *testing.T) {
14
val AutoUpdateSetting
15
err error
16
}{
17
- {`{"hello":123}`, UpdateMinor, nil}, // default
18
- {`{"AutoUpdate": "never"}`, UpdateNever, nil},
19
- {`{"AutoUpdate": "patch"}`, UpdatePatch, nil},
20
- {`{"AutoUpdate": "minor"}`, UpdateMinor, nil},
21
- {`{"AutoUpdate": "major"}`, UpdateMajor, nil},
22
- {`{"AutoUpdate": "blarg"}`, UpdateMinor, ErrUnknownAutoUpdateSetting},
17
+ {`{"hello":123}`, AutoUpdateNever, nil}, // zero value
18
+ {`{"AutoUpdate": "never"}`, AutoUpdateNever, nil},
19
+ {`{"AutoUpdate": "patch"}`, AutoUpdatePatch, nil},
20
+ {`{"AutoUpdate": "minor"}`, AutoUpdateMinor, nil},
21
+ {`{"AutoUpdate": "major"}`, AutoUpdateMajor, nil},
22
+ {`{"AutoUpdate": "blarg"}`, AutoUpdateMinor, ErrUnknownAutoUpdateSetting},
23
}
24
25
for i, tc := range tests {
updates/updates.go
+5
-5
@@ -150,7 +150,7 @@ func Apply(rel *check.Result) error {
150
// ShouldAutoUpdate decides wether a new version should be applied
151
// checks against config setting and new version string. returns false in case of error
152
func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
153
- if setting == config.UpdateNever {
153
+ if setting == config.AutoUpdateNever {
154
return false
155
}
156
@@ -165,7 +165,7 @@ func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
165
166
switch setting {
167
168
- case config.UpdatePatch:
168
+ case config.AutoUpdatePatch:
169
if n[0] < c[0] {
170
return false
171
}
@@ -176,14 +176,14 @@ func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
176
177
return n[2] > c[2]
178
179
- case config.UpdateMinor:
179
+ case config.AutoUpdateMinor:
180
if n[0] != c[0] {
181
return false
182
}
183
184
return n[1] > c[1] || (n[1] == c[1] && n[2] > c[2])
185
186
- case config.UpdateMajor:
186
+ case config.AutoUpdateMajor:
187
for i := 0; i < 3; i++ {
188
if n[i] < c[i] {
189
return false
@@ -222,7 +222,7 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
222
// there is an update available
223
224
// if we autoupdate
225
- if cfg.Version.AutoUpdate != config.UpdateNever {
225
+ if cfg.Version.AutoUpdate != config.AutoUpdateNever {
226
// and we should auto update
227
if ShouldAutoUpdate(cfg.Version.AutoUpdate, u.Version) {
228
log.Noticef("Applying update %s", u.Version)
updates/updates_test.go
+23
-23
@@ -21,29 +21,29 @@ func TestShouldAutoUpdate(t *testing.T) {
21
currV, newV string
22
should bool
23
}{
24
- {config.UpdateNever, "0.0.1", "1.0.0", false},
25
- {config.UpdateNever, "0.0.1", "0.1.0", false},
26
- {config.UpdateNever, "0.0.1", "0.0.1", false},
27
- {config.UpdateNever, "0.0.1", "0.0.2", false},
28
-
29
- {config.UpdatePatch, "0.0.1", "1.0.0", false},
30
- {config.UpdatePatch, "0.0.1", "0.1.0", false},
31
- {config.UpdatePatch, "0.0.1", "0.0.1", false},
32
- {config.UpdatePatch, "0.0.2", "0.0.1", false},
33
- {config.UpdatePatch, "0.0.1", "0.0.2", true},
34
-
35
- {config.UpdateMinor, "0.1.1", "1.0.0", false},
36
- {config.UpdateMinor, "0.1.1", "0.2.0", true},
37
- {config.UpdateMinor, "0.1.1", "0.1.2", true},
38
- {config.UpdateMinor, "0.2.1", "0.1.9", false},
39
- {config.UpdateMinor, "0.1.2", "0.1.1", false},
40
-
41
- {config.UpdateMajor, "1.0.0", "2.0.0", true},
42
- {config.UpdateMajor, "1.0.0", "1.1.0", true},
43
- {config.UpdateMajor, "1.0.0", "1.0.1", true},
44
- {config.UpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
45
- {config.UpdateMajor, "2.5.0", "2.4.0", false},
46
- {config.UpdateMajor, "2.0.2", "2.0.1", false},
24
+ {config.AutoUpdateNever, "0.0.1", "1.0.0", false},
25
+ {config.AutoUpdateNever, "0.0.1", "0.1.0", false},
26
+ {config.AutoUpdateNever, "0.0.1", "0.0.1", false},
27
+ {config.AutoUpdateNever, "0.0.1", "0.0.2", false},
28
+
29
+ {config.AutoUpdatePatch, "0.0.1", "1.0.0", false},
30
+ {config.AutoUpdatePatch, "0.0.1", "0.1.0", false},
31
+ {config.AutoUpdatePatch, "0.0.1", "0.0.1", false},
32
+ {config.AutoUpdatePatch, "0.0.2", "0.0.1", false},
33
+ {config.AutoUpdatePatch, "0.0.1", "0.0.2", true},
34
+
35
+ {config.AutoUpdateMinor, "0.1.1", "1.0.0", false},
36
+ {config.AutoUpdateMinor, "0.1.1", "0.2.0", true},
37
+ {config.AutoUpdateMinor, "0.1.1", "0.1.2", true},
38
+ {config.AutoUpdateMinor, "0.2.1", "0.1.9", false},
39
+ {config.AutoUpdateMinor, "0.1.2", "0.1.1", false},
40
+
41
+ {config.AutoUpdateMajor, "1.0.0", "2.0.0", true},
42
+ {config.AutoUpdateMajor, "1.0.0", "1.1.0", true},
43
+ {config.AutoUpdateMajor, "1.0.0", "1.0.1", true},
44
+ {config.AutoUpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
45
+ {config.AutoUpdateMajor, "2.5.0", "2.4.0", false},
46
+ {config.AutoUpdateMajor, "2.0.2", "2.0.1", false},
47
}
48
49
for i, tc := range tests {