Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
24e1e6d1
Commit
24e1e6d1
authored
Oct 20, 2014
by
Henry
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config: custom AutoUpdate type for validity
parent
8d41021b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
13 deletions
+96
-13
config/version.go
config/version.go
+59
-12
config/version_test.go
config/version_test.go
+36
-0
updates/updates.go
updates/updates.go
+1
-1
No files found.
config/version.go
View file @
24e1e6d1
package
config
import
(
"errors"
"strconv"
"strings"
"time"
)
...
...
@@ -23,12 +25,8 @@ type Version struct {
// (Note: cannot use time.Duration because marshalling with json breaks it)
CheckPeriod
string
// AutoUpdate is optional and has these these options:
// - "never" do not auto-update
// - "patch" auto-update on new patch versions
// - "minor" auto-update on new minor (or patch) versions (Default)
// - "major" auto-update on any new version
AutoUpdate
string
// AutoUpdate is optional
AutoUpdate
AutoUpdateSetting
}
// supported Version.Check values
...
...
@@ -43,13 +41,62 @@ const (
CheckIgnore
=
"ignore"
)
// supported Version.AutoUpdate values
// BUG(cryptix): make this a custom type that implements json.Unmarshaller() to verify values
// AutoUpdateSetting implements json.Unmarshaler to check values in config
// supported values:
// "never" - do not auto-update
// "patch" - auto-update on new patch versions
// "minor" - auto-update on new minor (or patch) versions (Default)
// "major" - auto-update on any new version
type
AutoUpdateSetting
int
// UnmarshalJSON checks the input against known strings
func
(
s
*
AutoUpdateSetting
)
UnmarshalJSON
(
in
[]
byte
)
error
{
switch
strings
.
ToLower
(
string
(
in
))
{
case
`"never"`
:
*
s
=
UpdateNever
case
`"major"`
:
*
s
=
UpdateMajor
case
`"minor"`
:
*
s
=
UpdateMinor
case
`"patch"`
:
*
s
=
UpdatePatch
default
:
*
s
=
UpdateMinor
return
ErrUnknownAutoUpdateSetting
}
return
nil
}
// MarshalJSON converts the value back to JSON string
func
(
s
AutoUpdateSetting
)
MarshalJSON
()
([]
byte
,
error
)
{
return
[]
byte
(
`"`
+
s
.
String
()
+
`"`
),
nil
}
// String converts valye to human readable string
func
(
s
AutoUpdateSetting
)
String
()
string
{
switch
s
{
case
UpdateNever
:
return
"never"
case
UpdateMajor
:
return
"major"
case
UpdateMinor
:
return
"minor"
case
UpdatePatch
:
return
"patch"
default
:
return
ErrUnknownAutoUpdateSetting
.
Error
()
}
}
// ErrUnknownAutoUpdateSetting is returned when an unknown value is read from the config
var
ErrUnknownAutoUpdateSetting
=
errors
.
New
(
"unknown value for AutoUpdate"
)
const
(
Update
Never
=
"never"
UpdatePatch
=
"patch"
UpdateM
inor
=
"min
or
"
Update
Major
=
"major"
Update
Minor
AutoUpdateSetting
=
iota
// first value so that it is the zero value and thus the default
UpdatePatch
UpdateM
aj
or
Update
Never
)
// defaultCheckPeriod governs h
...
...
config/version_test.go
0 → 100644
View file @
24e1e6d1
package
config
import
(
"strings"
"testing"
)
func
TestAutoUpdateValues
(
t
*
testing
.
T
)
{
var
tval
struct
{
AutoUpdate
AutoUpdateSetting
}
tests
:=
[]
struct
{
input
string
val
AutoUpdateSetting
err
error
}{
{
`{"hello":123}`
,
UpdateMinor
,
nil
},
// default
{
`{"AutoUpdate": "never"}`
,
UpdateNever
,
nil
},
{
`{"AutoUpdate": "patch"}`
,
UpdatePatch
,
nil
},
{
`{"AutoUpdate": "minor"}`
,
UpdateMinor
,
nil
},
{
`{"AutoUpdate": "major"}`
,
UpdateMajor
,
nil
},
{
`{"AutoUpdate": "blarg"}`
,
UpdateMinor
,
ErrUnknownAutoUpdateSetting
},
}
for
i
,
tc
:=
range
tests
{
err
:=
Decode
(
strings
.
NewReader
(
tc
.
input
),
&
tval
)
if
err
!=
tc
.
err
{
t
.
Fatalf
(
"%d failed - got err %q wanted %v"
,
i
,
err
,
tc
.
err
)
}
if
tval
.
AutoUpdate
!=
tc
.
val
{
t
.
Fatalf
(
"%d failed - got val %q where we wanted %q"
,
i
,
tval
.
AutoUpdate
,
tc
.
val
)
}
}
}
updates/updates.go
View file @
24e1e6d1
...
...
@@ -64,7 +64,7 @@ func AbleToApply() error {
// ShouldAutoUpdate decides wether a new version should be applied
// checks against config setting and new version string. returns false in case of error
func
ShouldAutoUpdate
(
setting
,
newVer
string
)
bool
{
func
ShouldAutoUpdate
(
setting
config
.
AutoUpdateSetting
,
newVer
string
)
bool
{
if
setting
==
config
.
UpdateNever
{
return
false
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment