From f1e6a30931bdd1759ca722baa0f9509760ec1c8c Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Thu, 20 Jun 2019 14:36:26 +0300 Subject: [PATCH] Fix version/channel linking --- Makefile | 2 +- home/control.go | 2 +- home/control_update.go | 8 ++++---- home/home.go | 27 ++++++++++++++++----------- main.go | 8 +++++++- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 195c69c9..b15dca3a 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ $(STATIC): $(JSFILES) client/node_modules $(TARGET): $(STATIC) *.go home/*.go dhcpd/*.go dnsfilter/*.go dnsforward/*.go GOOS=$(NATIVE_GOOS) GOARCH=$(NATIVE_GOARCH) GO111MODULE=off go get -v github.com/gobuffalo/packr/... PATH=$(GOPATH)/bin:$(PATH) packr -z - CGO_ENABLED=0 go build -ldflags="-s -w -X home.VersionString=$(GIT_VERSION) -X home.updateChannel=$(CHANNEL)" -asmflags="-trimpath=$(PWD)" -gcflags="-trimpath=$(PWD)" + CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=$(GIT_VERSION) -X main.channel=$(CHANNEL)" -asmflags="-trimpath=$(PWD)" -gcflags="-trimpath=$(PWD)" PATH=$(GOPATH)/bin:$(PATH) packr clean clean: diff --git a/home/control.go b/home/control.go index 62b2ecd6..48b31a2d 100644 --- a/home/control.go +++ b/home/control.go @@ -109,7 +109,7 @@ func handleStatus(w http.ResponseWriter, r *http.Request) { "bootstrap_dns": config.DNS.BootstrapDNS, "upstream_dns": config.DNS.UpstreamDNS, "all_servers": config.DNS.AllServers, - "version": VersionString, + "version": versionString, "language": config.Language, } diff --git a/home/control_update.go b/home/control_update.go index 70681d28..f910b055 100644 --- a/home/control_update.go +++ b/home/control_update.go @@ -43,7 +43,7 @@ func getVersionResp(data []byte) []byte { } _, ok := versionJSON[fmt.Sprintf("download_%s_%s", runtime.GOOS, runtime.GOARCH)] - if ok && ret["new_version"] != VersionString && VersionString >= selfUpdateMinVersion { + if ok && ret["new_version"] != versionString && versionString >= selfUpdateMinVersion { ret["can_autoupdate"] = true } @@ -145,12 +145,12 @@ func getUpdateInfo(jsonData []byte) (*updateInfo, error) { return nil, fmt.Errorf("Invalid JSON") } - if u.newVer == VersionString { + if u.newVer == versionString { return nil, fmt.Errorf("No need to update") } u.updateDir = filepath.Join(workDir, fmt.Sprintf("agh-update-%s", u.newVer)) - u.backupDir = filepath.Join(workDir, fmt.Sprintf("agh-backup-%s", VersionString)) + u.backupDir = filepath.Join(workDir, fmt.Sprintf("agh-backup-%s", versionString)) _, pkgFileName := filepath.Split(u.pkgURL) if len(pkgFileName) == 0 { @@ -360,7 +360,7 @@ func getPackageFile(u *updateInfo) error { // Perform an update procedure func doUpdate(u *updateInfo) error { log.Info("Updating from %s to %s. URL:%s Package:%s", - VersionString, u.newVer, u.pkgURL, u.pkgName) + versionString, u.newVer, u.pkgURL, u.pkgName) _ = os.Mkdir(u.updateDir, 0755) diff --git a/home/home.go b/home/home.go index dbd35601..c6f0f11d 100644 --- a/home/home.go +++ b/home/home.go @@ -25,15 +25,6 @@ import ( "github.com/gobuffalo/packr" ) -// VersionString will be set through ldflags, contains current version -var VersionString = "undefined" - -// updateChannel can be set via ldflags -var updateChannel = "release" -var versionCheckURL = "https://static.adguard.com/adguardhome/" + updateChannel + "/version.json" - -const versionCheckPeriod = time.Hour * 8 - var httpServer *http.Server var httpsServer struct { server *http.Server @@ -48,8 +39,22 @@ const ( configSyslog = "syslog" ) +// Update-related variables +var ( + versionString string + updateChannel string + versionCheckURL string +) + +const versionCheckPeriod = time.Hour * 8 + // main is the entry point -func Main() { +func Main(version string, channel string) { + // Init update-related global variables + versionString = version + updateChannel = channel + versionCheckURL = "https://static.adguard.com/adguardhome/" + updateChannel + "/version.json" + // config can be specified, which reads options from there, but other command line flags have to override config values // therefore, we must do it manually instead of using a lib args := loadOptions() @@ -81,7 +86,7 @@ func run(args options) { enableTLS13() // print the first message after logger is configured - log.Printf("AdGuard Home, version %s, channel %s\n", VersionString, updateChannel) + log.Printf("AdGuard Home, version %s, channel %s\n", versionString, updateChannel) log.Debug("Current working directory is %s", config.ourWorkingDir) if args.runningAsService { log.Info("AdGuard Home is running as a service") diff --git a/main.go b/main.go index 3a9d35b9..717fa8e3 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,12 @@ import ( "github.com/AdguardTeam/AdGuardHome/home" ) +// version will be set through ldflags, contains current version +var version = "undefined" + +// channel can be set via ldflags +var channel = "release" + func main() { - home.Main() + home.Main(version, channel) }