mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 03:28:25 +08:00
config/version: Update_ -> AutoUpdate_
This commit is contained in:
@ -42,27 +42,36 @@ const (
|
||||
)
|
||||
|
||||
// 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
|
||||
|
||||
// AutoUpdateSetting values
|
||||
const (
|
||||
AutoUpdateNever AutoUpdateSetting = iota // do not auto-update
|
||||
AutoUpdatePatch // only on new patch versions
|
||||
AutoUpdateMinor // on new minor or patch versions (Default)
|
||||
AutoUpdateMajor // on all, even Major, version changes
|
||||
)
|
||||
|
||||
// ErrUnknownAutoUpdateSetting is returned when an unknown value is read from the config
|
||||
var ErrUnknownAutoUpdateSetting = errors.New("unknown value for AutoUpdate")
|
||||
|
||||
// defaultCheckPeriod governs h
|
||||
var defaultCheckPeriod = time.Hour * 48
|
||||
|
||||
// UnmarshalJSON checks the input against known strings
|
||||
func (s *AutoUpdateSetting) UnmarshalJSON(in []byte) error {
|
||||
|
||||
switch strings.ToLower(string(in)) {
|
||||
case `"never"`:
|
||||
*s = UpdateNever
|
||||
*s = AutoUpdateNever
|
||||
case `"major"`:
|
||||
*s = UpdateMajor
|
||||
*s = AutoUpdateMajor
|
||||
case `"minor"`:
|
||||
*s = UpdateMinor
|
||||
*s = AutoUpdateMinor
|
||||
case `"patch"`:
|
||||
*s = UpdatePatch
|
||||
*s = AutoUpdatePatch
|
||||
default:
|
||||
*s = UpdateMinor
|
||||
*s = AutoUpdateMinor
|
||||
return ErrUnknownAutoUpdateSetting
|
||||
}
|
||||
return nil
|
||||
@ -76,32 +85,19 @@ func (s AutoUpdateSetting) MarshalJSON() ([]byte, error) {
|
||||
// String converts valye to human readable string
|
||||
func (s AutoUpdateSetting) String() string {
|
||||
switch s {
|
||||
case UpdateNever:
|
||||
case AutoUpdateNever:
|
||||
return "never"
|
||||
case UpdateMajor:
|
||||
case AutoUpdateMajor:
|
||||
return "major"
|
||||
case UpdateMinor:
|
||||
case AutoUpdateMinor:
|
||||
return "minor"
|
||||
case UpdatePatch:
|
||||
case AutoUpdatePatch:
|
||||
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 (
|
||||
UpdateMinor AutoUpdateSetting = iota // first value so that it is the zero value and thus the default
|
||||
UpdatePatch
|
||||
UpdateMajor
|
||||
UpdateNever
|
||||
)
|
||||
|
||||
// defaultCheckPeriod governs h
|
||||
var defaultCheckPeriod = time.Hour * 48
|
||||
|
||||
func (v *Version) checkPeriodDuration() time.Duration {
|
||||
d, err := strconv.Atoi(v.CheckPeriod)
|
||||
if err != nil {
|
||||
|
@ -14,12 +14,12 @@ func TestAutoUpdateValues(t *testing.T) {
|
||||
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},
|
||||
{`{"hello":123}`, AutoUpdateNever, nil}, // zero value
|
||||
{`{"AutoUpdate": "never"}`, AutoUpdateNever, nil},
|
||||
{`{"AutoUpdate": "patch"}`, AutoUpdatePatch, nil},
|
||||
{`{"AutoUpdate": "minor"}`, AutoUpdateMinor, nil},
|
||||
{`{"AutoUpdate": "major"}`, AutoUpdateMajor, nil},
|
||||
{`{"AutoUpdate": "blarg"}`, AutoUpdateMinor, ErrUnknownAutoUpdateSetting},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
|
@ -150,7 +150,7 @@ func Apply(rel *check.Result) 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 config.AutoUpdateSetting, newVer string) bool {
|
||||
if setting == config.UpdateNever {
|
||||
if setting == config.AutoUpdateNever {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
|
||||
|
||||
switch setting {
|
||||
|
||||
case config.UpdatePatch:
|
||||
case config.AutoUpdatePatch:
|
||||
if n[0] < c[0] {
|
||||
return false
|
||||
}
|
||||
@ -176,14 +176,14 @@ func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
|
||||
|
||||
return n[2] > c[2]
|
||||
|
||||
case config.UpdateMinor:
|
||||
case config.AutoUpdateMinor:
|
||||
if n[0] != c[0] {
|
||||
return false
|
||||
}
|
||||
|
||||
return n[1] > c[1] || (n[1] == c[1] && n[2] > c[2])
|
||||
|
||||
case config.UpdateMajor:
|
||||
case config.AutoUpdateMajor:
|
||||
for i := 0; i < 3; i++ {
|
||||
if n[i] < c[i] {
|
||||
return false
|
||||
@ -222,7 +222,7 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
|
||||
// there is an update available
|
||||
|
||||
// if we autoupdate
|
||||
if cfg.Version.AutoUpdate != config.UpdateNever {
|
||||
if cfg.Version.AutoUpdate != config.AutoUpdateNever {
|
||||
// and we should auto update
|
||||
if ShouldAutoUpdate(cfg.Version.AutoUpdate, u.Version) {
|
||||
log.Noticef("Applying update %s", u.Version)
|
||||
|
@ -21,29 +21,29 @@ func TestShouldAutoUpdate(t *testing.T) {
|
||||
currV, newV string
|
||||
should bool
|
||||
}{
|
||||
{config.UpdateNever, "0.0.1", "1.0.0", false},
|
||||
{config.UpdateNever, "0.0.1", "0.1.0", false},
|
||||
{config.UpdateNever, "0.0.1", "0.0.1", false},
|
||||
{config.UpdateNever, "0.0.1", "0.0.2", false},
|
||||
{config.AutoUpdateNever, "0.0.1", "1.0.0", false},
|
||||
{config.AutoUpdateNever, "0.0.1", "0.1.0", false},
|
||||
{config.AutoUpdateNever, "0.0.1", "0.0.1", false},
|
||||
{config.AutoUpdateNever, "0.0.1", "0.0.2", false},
|
||||
|
||||
{config.UpdatePatch, "0.0.1", "1.0.0", false},
|
||||
{config.UpdatePatch, "0.0.1", "0.1.0", false},
|
||||
{config.UpdatePatch, "0.0.1", "0.0.1", false},
|
||||
{config.UpdatePatch, "0.0.2", "0.0.1", false},
|
||||
{config.UpdatePatch, "0.0.1", "0.0.2", true},
|
||||
{config.AutoUpdatePatch, "0.0.1", "1.0.0", false},
|
||||
{config.AutoUpdatePatch, "0.0.1", "0.1.0", false},
|
||||
{config.AutoUpdatePatch, "0.0.1", "0.0.1", false},
|
||||
{config.AutoUpdatePatch, "0.0.2", "0.0.1", false},
|
||||
{config.AutoUpdatePatch, "0.0.1", "0.0.2", true},
|
||||
|
||||
{config.UpdateMinor, "0.1.1", "1.0.0", false},
|
||||
{config.UpdateMinor, "0.1.1", "0.2.0", true},
|
||||
{config.UpdateMinor, "0.1.1", "0.1.2", true},
|
||||
{config.UpdateMinor, "0.2.1", "0.1.9", false},
|
||||
{config.UpdateMinor, "0.1.2", "0.1.1", false},
|
||||
{config.AutoUpdateMinor, "0.1.1", "1.0.0", false},
|
||||
{config.AutoUpdateMinor, "0.1.1", "0.2.0", true},
|
||||
{config.AutoUpdateMinor, "0.1.1", "0.1.2", true},
|
||||
{config.AutoUpdateMinor, "0.2.1", "0.1.9", false},
|
||||
{config.AutoUpdateMinor, "0.1.2", "0.1.1", false},
|
||||
|
||||
{config.UpdateMajor, "1.0.0", "2.0.0", true},
|
||||
{config.UpdateMajor, "1.0.0", "1.1.0", true},
|
||||
{config.UpdateMajor, "1.0.0", "1.0.1", true},
|
||||
{config.UpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
|
||||
{config.UpdateMajor, "2.5.0", "2.4.0", false},
|
||||
{config.UpdateMajor, "2.0.2", "2.0.1", false},
|
||||
{config.AutoUpdateMajor, "1.0.0", "2.0.0", true},
|
||||
{config.AutoUpdateMajor, "1.0.0", "1.1.0", true},
|
||||
{config.AutoUpdateMajor, "1.0.0", "1.0.1", true},
|
||||
{config.AutoUpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
|
||||
{config.AutoUpdateMajor, "2.5.0", "2.4.0", false},
|
||||
{config.AutoUpdateMajor, "2.0.2", "2.0.1", false},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
|
Reference in New Issue
Block a user