From 9b651eb701f2c2423166022d8fe9b9df067fc387 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Tue, 18 May 2021 15:57:19 +0200 Subject: [PATCH] Implement distribution-specific builds Make more things configurable at compile time so building binaries for distributions is easier while complying with the license. --- .gitignore | 1 - Makefile | 8 ++++++-- main.go | 8 ++++++-- scripts/embedversion.go | 24 ------------------------ scripts/signer.go | 9 ++++----- ui.go | 2 +- update.go | 20 +++++++++++++------- update_noop.go | 18 ------------------ 8 files changed, 30 insertions(+), 60 deletions(-) delete mode 100644 scripts/embedversion.go delete mode 100644 update_noop.go diff --git a/.gitignore b/.gitignore index 8c3de2f..1bc9b1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ bin/ -version.go licenses.go diff --git a/Makefile b/Makefile index 2d58f65..1801e1e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ +UPDATE_URL=https://noisetorch.epicgamer.org +UPDATE_PUBKEY=3mL+rBi4yBZ1wGimQ/oSQCjxELzgTh+673H4JdzQBOk= +VERSION := $(shell git describe --tags) + dev: rnnoise mkdir -p bin/ go generate - go build -o bin/noisetorch + go build -ldflags '-X main.version=${VERSION}' -o bin/noisetorch release: rnnoise mkdir -p bin/ mkdir -p tmp/ @@ -14,7 +18,7 @@ release: rnnoise mkdir -p tmp/.local/bin/ go generate - CGO_ENABLED=0 GOOS=linux go build -tags release -a -ldflags '-s -w -extldflags "-static"' . + CGO_ENABLED=0 GOOS=linux go build -tags release -a -ldflags '-s -w -extldflags "-static" -X main.version=${VERSION} -X main.distribution=official -X main.updateURL=${UPDATE_URL} -X main.publicKeyString=${UPDATE_PUBKEY}' . upx noisetorch mv noisetorch tmp/.local/bin/ cd tmp/; \ diff --git a/main.go b/main.go index 0ec62c6..c072bea 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ import ( "github.com/aarzilli/nucular/style" ) -//go:generate go run scripts/embedversion.go //go:generate go run scripts/embedlicenses.go //go:embed c/ladspa/rnnoise_ladspa.so @@ -42,6 +41,11 @@ type device struct { const appName = "NoiseTorch" +var version = "unknown" // will be changed by build +var distribution = "custom" // ditto +var updateURL = "" // ditto +var publicKeyString = "" // ditto + func main() { opt := parseCLIOpts() @@ -50,7 +54,7 @@ func main() { } else { log.SetOutput(ioutil.Discard) } - log.Printf("Application starting. Version: %s\n", version) + log.Printf("Application starting. Version: %s (%s)\n", version, distribution) log.Printf("CAP_SYS_RESOURCE: %t\n", hasCapSysResource(getCurrentCaps())) initializeConfigIfNot() diff --git a/scripts/embedversion.go b/scripts/embedversion.go deleted file mode 100644 index ce0a756..0000000 --- a/scripts/embedversion.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "os" - "os/exec" - "strings" -) - -func main() { - cmd := exec.Command("git", "describe", "--tags") - ret, err := cmd.Output() - - if err != nil { - panic("Couldn't read git tags to embed version number") - } - version := strings.TrimSpace(string(ret)) - - out, _ := os.Create("version.go") - defer out.Close() - - out.Write([]byte("package main\n\n//THIS FILE IS AUTOMATICALLY GENERATED BY `go generate` DO NOT EDIT!\n\nvar version=\"")) - out.Write([]byte(version)) - out.Write([]byte("\"\n")) -} diff --git a/scripts/signer.go b/scripts/signer.go index 8c90821..aff95e9 100644 --- a/scripts/signer.go +++ b/scripts/signer.go @@ -22,8 +22,8 @@ func main() { var doSign bool flag.BoolVar(&doSign, "s", false, "Sign the release tar") - var doVerify bool - flag.BoolVar(&doVerify, "v", false, "Verify the signature of the tar") + var publicKeyString string + flag.StringVar(&publicKeyString, "k", "", "Public key to verify against (runs verifier if set)") flag.Parse() @@ -56,9 +56,8 @@ func main() { os.Exit(0) } - if doVerify { - pubStr := "3mL+rBi4yBZ1wGimQ/oSQCjxELzgTh+673H4JdzQBOk=" - pub, err := base64.StdEncoding.DecodeString(pubStr) + if publicKeyString != "" { + pub, err := base64.StdEncoding.DecodeString(publicKeyString) if err != nil { panic(err) } diff --git a/ui.go b/ui.go index af4421a..bbab17f 100644 --- a/ui.go +++ b/ui.go @@ -345,7 +345,7 @@ func versionView(ctx *ntcontext, w *nucular.Window) { w.Row(50).Dynamic(1) w.Label("Version", "CB") w.Row(50).Dynamic(1) - w.Label(version, "CB") + w.Label(fmt.Sprintf("%s (%s)", version, distribution), "CB") w.Row(50).Dynamic(1) w.Spacing(1) w.Row(20).Dynamic(2) diff --git a/update.go b/update.go index aedf33c..baeb8c8 100644 --- a/update.go +++ b/update.go @@ -1,5 +1,3 @@ -// +build release - package main import ( @@ -14,9 +12,6 @@ import ( "strings" ) -var updateURL = "https://noisetorch.epicgamer.org" -var publicKeyString = "3mL+rBi4yBZ1wGimQ/oSQCjxELzgTh+673H4JdzQBOk=" - type updateui struct { serverVersion string available bool @@ -24,7 +19,14 @@ type updateui struct { updatingText string } +func updateable() bool { + return updateURL != "" && publicKeyString != "" +} + func updateCheck(ctx *ntcontext) { + if !updateable() { + return + } log.Println("Checking for updates") bodybuf, err := fetchFile("version.txt") if err != nil { @@ -41,6 +43,9 @@ func updateCheck(ctx *ntcontext) { } func update(ctx *ntcontext) { + if !updateable() { + return + } sig, err := fetchFile("NoiseTorch_x64.tgz.sig") if err != nil { log.Println("Couldn't fetch signature", err) @@ -94,8 +99,9 @@ func fetchFile(file string) ([]byte, error) { func publickey() []byte { pub, err := base64.StdEncoding.DecodeString(publicKeyString) - if err != nil { - panic(err) // it's hardcoded, we should never hit this, panic if we do + if err != nil { // Should only happen when distributor ships an invalid public key + log.Fatalf("Error while reading public key: %s\nContact the distribution '%s' about this error.\n", err, distribution) + os.Exit(1) } return pub } diff --git a/update_noop.go b/update_noop.go deleted file mode 100644 index 91fdc38..0000000 --- a/update_noop.go +++ /dev/null @@ -1,18 +0,0 @@ -// +build !release - -package main - -type updateui struct { - serverVersion string - available bool - triggered bool - updatingText string -} - -func updateCheck(ctx *ntcontext) { - // noop for non-release versions -} - -func update(ctx *ntcontext) { - // noop for non-release versions -}