From f083d6b53f4ce734f8bd1318617e66c1e45a6e3f Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Fri, 23 Jun 2023 14:16:36 +0100 Subject: [PATCH] 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. --- .goreleaser.yml | 2 +- Makefile | 3 ++- scripts/version.sh | 2 +- updater.go | 51 +++++++++++++++++++++++++++------------------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index b4df4c6..79395c4 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 diff --git a/Makefile b/Makefile index 994b7a2..ebe9f18 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/scripts/version.sh b/scripts/version.sh index b934b38..366f205 100755 --- a/scripts/version.sh +++ b/scripts/version.sh @@ -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')" $@ diff --git a/updater.go b/updater.go index a72b9ab..37979fd 100644 --- a/updater.go +++ b/updater.go @@ -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"` @@ -106,14 +112,14 @@ var goarch = map[string]string{ "arm": "armv6", } -// func newDockerBuild() Update { -// var tag string -// if version == "git" { -// tag = "docker-unstable" -// } else { -// tag = "docker-latest" -// } -// } +// func newDockerBuild() Update { +// var tag string +// if version == "git" { +// tag = "docker-unstable" +// } else { +// tag = "docker-latest" +// } +// } type Updater struct { version, commit, tag, url, namespace, name string stable bool @@ -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) { @@ -526,18 +535,18 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int // func newInternalBuild() Update { // tag := "internal" -// func update(path string) err { -// if -// fp, err := os.Executable() -// if err != nil { -// return err -// } -// fullPath, err := filepath.EvalSymlinks(fp) -// if err != nil { -// return err -// } -// newBinary, -// } +// func update(path string) err { +// if +// fp, err := os.Executable() +// if err != nil { +// return err +// } +// fullPath, err := filepath.EvalSymlinks(fp) +// if err != nil { +// return err +// } +// newBinary, +// } func (app *appContext) checkForUpdates() { for { go func() {