mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-03 21:08:17 +08:00
config: custom AutoUpdate type for validity
This commit is contained in:
@ -1,7 +1,9 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,12 +25,8 @@ type Version struct {
|
|||||||
// (Note: cannot use time.Duration because marshalling with json breaks it)
|
// (Note: cannot use time.Duration because marshalling with json breaks it)
|
||||||
CheckPeriod string
|
CheckPeriod string
|
||||||
|
|
||||||
// AutoUpdate is optional and has these these options:
|
// AutoUpdate is optional
|
||||||
// - "never" do not auto-update
|
AutoUpdate AutoUpdateSetting
|
||||||
// - "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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// supported Version.Check values
|
// supported Version.Check values
|
||||||
@ -43,13 +41,62 @@ const (
|
|||||||
CheckIgnore = "ignore"
|
CheckIgnore = "ignore"
|
||||||
)
|
)
|
||||||
|
|
||||||
// supported Version.AutoUpdate values
|
// AutoUpdateSetting implements json.Unmarshaler to check values in config
|
||||||
// BUG(cryptix): make this a custom type that implements json.Unmarshaller() to verify values
|
// 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 (
|
const (
|
||||||
UpdateNever = "never"
|
UpdateMinor AutoUpdateSetting = iota // first value so that it is the zero value and thus the default
|
||||||
UpdatePatch = "patch"
|
UpdatePatch
|
||||||
UpdateMinor = "minor"
|
UpdateMajor
|
||||||
UpdateMajor = "major"
|
UpdateNever
|
||||||
)
|
)
|
||||||
|
|
||||||
// defaultCheckPeriod governs h
|
// defaultCheckPeriod governs h
|
||||||
|
36
config/version_test.go
Normal file
36
config/version_test.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
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
|
// ShouldAutoUpdate decides wether a new version should be applied
|
||||||
// checks against config setting and new version string. returns false in case of error
|
// 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 {
|
if setting == config.UpdateNever {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user