1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

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.
This commit is contained in:
Harvey Tindall 2023-06-23 14:16:36 +01:00
parent 7caa5c5d57
commit f083d6b53f
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 34 additions and 24 deletions

View File

@ -49,7 +49,7 @@ builds:
env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0
ldflags: 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: goos:
- linux - linux
- darwin - darwin

View File

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

View File

@ -2,4 +2,4 @@
# sets version environment variable for goreleaser to use # sets version environment variable for goreleaser to use
# scripts/version.sh goreleaser ... # scripts/version.sh goreleaser ...
JFA_GO_VERSION=$(git describe --exact-match HEAD 2> /dev/null || echo 'vgit') 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')" $@

View File

@ -25,6 +25,12 @@ const (
repo = "jfa-go" 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 { type GHRelease struct {
HTMLURL string `json:"html_url"` HTMLURL string `json:"html_url"`
ID int `json:"id"` ID int `json:"id"`
@ -106,14 +112,14 @@ var goarch = map[string]string{
"arm": "armv6", "arm": "armv6",
} }
// func newDockerBuild() Update { // func newDockerBuild() Update {
// var tag string // var tag string
// if version == "git" { // if version == "git" {
// tag = "docker-unstable" // tag = "docker-unstable"
// } else { // } else {
// tag = "docker-latest" // tag = "docker-latest"
// } // }
// } // }
type Updater struct { type Updater struct {
version, commit, tag, url, namespace, name string version, commit, tag, url, namespace, name string
stable bool stable bool
@ -124,6 +130,7 @@ type Updater struct {
} }
func newUpdater(buildroneURL, namespace, repo, version, commit, buildType string) *Updater { 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 bType := off
tag := "" tag := ""
switch buildType { switch buildType {
@ -184,6 +191,7 @@ func (ud *Updater) GetTag() (Tag, int, error) {
return Tag{}, -1, nil return Tag{}, -1, nil
} }
url := fmt.Sprintf("%s/repo/%s/%s/tag/latest/%s", ud.url, ud.namespace, ud.name, ud.tag) 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) req, _ := http.NewRequest("GET", url, nil)
resp, err := ud.httpClient.Do(req) resp, err := ud.httpClient.Do(req)
defer ud.timeoutHandler() defer ud.timeoutHandler()
@ -205,7 +213,8 @@ func (ud *Updater) GetTag() (Tag, int, error) {
} }
func (t *Tag) IsNew() bool { 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) { 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 { // func newInternalBuild() Update {
// tag := "internal" // tag := "internal"
// func update(path string) err { // func update(path string) err {
// if // if
// fp, err := os.Executable() // fp, err := os.Executable()
// if err != nil { // if err != nil {
// return err // return err
// } // }
// fullPath, err := filepath.EvalSymlinks(fp) // fullPath, err := filepath.EvalSymlinks(fp)
// if err != nil { // if err != nil {
// return err // return err
// } // }
// newBinary, // newBinary,
// } // }
func (app *appContext) checkForUpdates() { func (app *appContext) checkForUpdates() {
for { for {
go func() { go func() {