Commit 24e1e6d1 authored by Henry's avatar Henry

config: custom AutoUpdate type for validity

parent 8d41021b
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 (
UpdateNever = "never"
UpdatePatch = "patch"
UpdateMinor = "minor"
UpdateMajor = "major"
UpdateMinor AutoUpdateSetting = iota // first value so that it is the zero value and thus the default
UpdatePatch
UpdateMajor
UpdateNever
)
// defaultCheckPeriod governs h
......
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)
}
}
}
......@@ -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
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment