version.go 3.64 KB
Newer Older
1 2
package config

3
import (
4
	"errors"
5
	"strconv"
6
	"strings"
7 8
	"time"
)
9

10
// CurrentVersionNumber is the current application's version literal
11
const CurrentVersionNumber = "0.3.9-dev"
12

13 14
// CurrentCommit is the current git commit, this is set as a ldflag in the Makefile
var CurrentCommit string
15

16 17
// Version regulates checking if the most recent version is run
type Version struct {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	// Current is the ipfs version for which config was generated
	Current string

	// Check signals how to react on updates:
	// - "ignore" for not checking
	// - "warn" for issuing a warning and proceeding
	// - "error" for exiting with an error
	Check string

	// CheckDate is a timestamp for the last time API endpoint was checked for updates
	CheckDate time.Time

	// CheckPeriod is the time duration over which the update check will not be performed
	// (Note: cannot use time.Duration because marshalling with json breaks it)
	CheckPeriod string
Henry's avatar
Henry committed
33

34 35
	// AutoUpdate is optional
	AutoUpdate AutoUpdateSetting
36 37 38 39
}

// supported Version.Check values
const (
40 41 42 43 44 45 46 47
	// CheckError value for Version.Check to raise error and exit if version is obsolete
	CheckError = "error"

	// CheckWarn value for Version.Check to show warning message if version is obsolete
	CheckWarn = "warn"

	// CheckIgnore value for Version.Check to not perform update check
	CheckIgnore = "ignore"
48 49
)

50 51 52
// AutoUpdateSetting implements json.Unmarshaler to check values in config
type AutoUpdateSetting int

53 54 55 56 57 58 59 60 61 62 63 64 65 66
// 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

67 68 69 70 71
// UnmarshalJSON checks the input against known strings
func (s *AutoUpdateSetting) UnmarshalJSON(in []byte) error {

	switch strings.ToLower(string(in)) {
	case `"never"`:
72
		*s = AutoUpdateNever
73
	case `"major"`:
74
		*s = AutoUpdateMajor
75
	case `"minor"`:
76
		*s = AutoUpdateMinor
77
	case `"patch"`:
78
		*s = AutoUpdatePatch
79
	default:
80
		*s = AutoUpdateMinor
81 82 83 84 85 86 87 88 89 90 91 92 93
		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 {
94
	case AutoUpdateNever:
95
		return "never"
96
	case AutoUpdateMajor:
97
		return "major"
98
	case AutoUpdateMinor:
99
		return "minor"
100
	case AutoUpdatePatch:
101 102 103 104 105 106
		return "patch"
	default:
		return ErrUnknownAutoUpdateSetting.Error()
	}
}

107 108 109
func (v *Version) checkPeriodDuration() time.Duration {
	d, err := strconv.Atoi(v.CheckPeriod)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
		log.Warning("config.Version.CheckPeriod parse error. Using default.")
111 112 113 114 115 116 117 118 119 120
		return defaultCheckPeriod
	}
	return time.Duration(d)
}

// ShouldCheckForUpdate returns if update check API endpoint is needed for this specific runtime
func (v *Version) ShouldCheckForUpdate() bool {

	period := v.checkPeriodDuration()
	if v.Check == CheckIgnore || v.CheckDate.Add(period).After(time.Now()) {
121 122 123 124 125
		return false
	}
	return true
}

126 127 128 129 130 131 132 133 134
// VersionDefaultValue returns the default version config value (for init).
func VersionDefaultValue() Version {
	return Version{
		Current:     CurrentVersionNumber,
		Check:       "error",
		CheckPeriod: strconv.Itoa(int(defaultCheckPeriod)),
		AutoUpdate:  AutoUpdateMinor,
	}
}