updater: include build date, check against updates

build time is included in the binary, so the buildrone release date is
compared to it when deciding if something is an update or not.
db
Harvey Tindall 1 year ago
parent 7caa5c5d57
commit f083d6b53f
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -49,7 +49,7 @@ builds:
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X main.version={{.Env.JFA_GO_VERSION}} -X main.commit={{.ShortCommit}} -X main.updater=binary -X main.cssVersion={{.Env.JFA_GO_CSS_VERSION}}
- -s -w -X main.version={{.Env.JFA_GO_VERSION}} -X main.commit={{.ShortCommit}} -X main.updater=binary -X main.cssVersion={{.Env.JFA_GO_CSS_VERSION}} -X main.buildTimeUnix={{.Env.JFA_GO_BUILD_TIME}}
goos:
- linux
- darwin

@ -11,9 +11,10 @@ CSSVERSION ?= v3
VERSION ?= $(shell git describe --exact-match HEAD 2> /dev/null || echo vgit)
VERSION := $(shell echo $(VERSION) | sed 's/v//g')
COMMIT ?= $(shell git rev-parse --short HEAD || echo unknown)
BUILDTIME ?= $(shell date +%s)
UPDATER ?= off
LDFLAGS := -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.cssVersion=$(CSSVERSION)
LDFLAGS := -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.cssVersion=$(CSSVERSION) -X main.buildTimeUnix=$(BUILDTIME)
ifeq ($(UPDATER), on)
LDFLAGS := $(LDFLAGS) -X main.updater=binary
else ifneq ($(UPDATER), off)

@ -2,4 +2,4 @@
# sets version environment variable for goreleaser to use
# scripts/version.sh goreleaser ...
JFA_GO_VERSION=$(git describe --exact-match HEAD 2> /dev/null || echo 'vgit')
JFA_GO_CSS_VERSION="v3" JFA_GO_NFPM_EPOCH=$(git rev-list --all --count) JFA_GO_VERSION="$(echo $JFA_GO_VERSION | sed 's/v//g')" $@
JFA_GO_CSS_VERSION="v3" JFA_GO_NFPM_EPOCH=$(git rev-list --all --count) JFA_GO_BUILD_TIME=$(date +%s) JFA_GO_VERSION="$(echo $JFA_GO_VERSION | sed 's/v//g')" $@

@ -25,6 +25,12 @@ const (
repo = "jfa-go"
)
var buildTimeUnix string
var buildTime time.Time = func() time.Time {
i, _ := strconv.ParseInt(buildTimeUnix, 10, 64)
return time.Unix(i, 0)
}()
type GHRelease struct {
HTMLURL string `json:"html_url"`
ID int `json:"id"`
@ -124,6 +130,7 @@ type Updater struct {
}
func newUpdater(buildroneURL, namespace, repo, version, commit, buildType string) *Updater {
// fmt.Printf(`Updater intializing with "%s", "%s", "%s", "%s", "%s", "%s"\n`, buildroneURL, namespace, repo, version, commit, buildType)
bType := off
tag := ""
switch buildType {
@ -184,6 +191,7 @@ func (ud *Updater) GetTag() (Tag, int, error) {
return Tag{}, -1, nil
}
url := fmt.Sprintf("%s/repo/%s/%s/tag/latest/%s", ud.url, ud.namespace, ud.name, ud.tag)
// fmt.Printf("Pinging URL \"%s\" for updates\n", url)
req, _ := http.NewRequest("GET", url, nil)
resp, err := ud.httpClient.Do(req)
defer ud.timeoutHandler()
@ -205,7 +213,8 @@ func (ud *Updater) GetTag() (Tag, int, error) {
}
func (t *Tag) IsNew() bool {
return t.Version[:7] != commit && t.Ready
// fmt.Printf("Build Time: %+v, Release Date: %+v", buildTime, t.ReleaseDate)
return t.Version[:7] != commit && t.Ready && t.ReleaseDate.After(buildTime)
}
func (ud *Updater) getRelease() (release GHRelease, status int, err error) {

Loading…
Cancel
Save