From 09385713de5d5466c57084bfb4988cef04e4c4be Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 3 Jun 2020 20:32:53 +0300 Subject: [PATCH] Include commit information in CI builds. Fixes #158 --- .gitlab-ci.yml | 12 +++++++----- build.sh | 2 ++ commands.go | 17 +++++++++++++++++ main.go | 32 ++++++++++++++++++++++++++++++++ user.go | 2 +- 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100755 build.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a9d52d2..edea4cc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,8 +14,9 @@ stages: - apk add build-base olm-dev@edge_community - mkdir -p .cache - export GOPATH="$CI_PROJECT_DIR/.cache" + - export GO_LDFLAGS="-X main.Tag=$CI_COMMIT_TAG -X main.Commit=$CI_COMMIT_REF_NAME -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`'" script: - - go build -o mautrix-whatsapp + - go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp artifacts: paths: - mautrix-whatsapp @@ -43,13 +44,14 @@ build static: before_script: - mkdir -p .cache - export GOPATH="$CI_PROJECT_DIR/.cache" + - export GO_LDFLAGS="-X main.Tag=$CI_COMMIT_TAG -X main.Commit=$CI_COMMIT_REF_NAME -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`'" variables: CGO_ENABLED: 0 script: - - GOOS=linux GOARCH=amd64 go build -o mautrix-whatsapp_linux_amd64 - - GOOS=linux GOARCH=arm64 go build -o mautrix-whatsapp_linux_arm64 - - GOOS=linux GOARCH=arm go build -o mautrix-whatsapp_linux_arm - - GOOS=linux GOARCH=386 go build -o mautrix-whatsapp_linux_386 + - GOOS=linux GOARCH=amd64 go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_amd64 + - GOOS=linux GOARCH=arm64 go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_arm64 + - GOOS=linux GOARCH=arm go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_arm + - GOOS=linux GOARCH=386 go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_386 artifacts: paths: - mautrix-whatsapp_linux_amd64 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..2409c5b --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/bin/sh +go build -ldflags "-X main.Tag=$(git describe --exact-match --tags 2>/dev/null) -X main.Commit=$(git rev-parse HEAD) -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`'" "$@" diff --git a/commands.go b/commands.go index cc7b09b..10b5a9f 100644 --- a/commands.go +++ b/commands.go @@ -105,6 +105,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandLogoutMatrix(ce) case "help": handler.CommandHelp(ce) + case "version": + handler.CommandVersion(ce) case "reconnect": handler.CommandReconnect(ce) case "disconnect": @@ -174,6 +176,21 @@ func (handler *CommandHandler) CommandDevTest(_ *CommandEvent) { } +const cmdVersionHelp = `version - View the bridge version` + +func (handler *CommandHandler) CommandVersion(ce *CommandEvent) { + version := fmt.Sprintf("%s+dev.unknown", Version) + if Tag == Version { + version = fmt.Sprintf("[%s](%s/releases/%s) (%s)", Version, URL, Tag, BuildTime) + } else if len(Commit) > 8 { + if !strings.HasSuffix(Version, "+dev") { + Version += "+dev" + } + version = fmt.Sprintf("%s.[%s](%s/commit/%s) (%s)", Version, Commit[:8], URL, Commit, BuildTime) + } + ce.Reply(fmt.Sprintf("[%s](%s) %s", Name, URL, version)) +} + const cmdSetPowerLevelHelp = `set-pl [user ID] - Change the power level in a portal room. Only for bridge admins.` func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) { diff --git a/main.go b/main.go index ec63b35..d59aaa8 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "os/signal" + "strings" "sync" "syscall" "time" @@ -38,11 +39,30 @@ import ( "maunium.net/go/mautrix-whatsapp/types" ) +var ( + // These are static + Name = "mautrix-whatsapp" + URL = "https://github.com/tulir/mautrix-whatsapp" + // This is changed when making a release + Version = "0.1.0+dev" + // These are filled at build time with the -X linker flag + Tag = "unknown" + Commit = "unknown" + BuildTime = "unknown" +) + +func init() { + if len(Tag) > 0 && Tag[0] == 'v' { + Tag = Tag[1:] + } +} + var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String() //var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String() var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String() var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool() +var version = flag.MakeFull("v", "version", "View bridge version and quit.", "false").Bool() var ignoreUnsupportedDatabase = flag.Make().LongKey("ignore-unsupported-database").Usage("Run even if database is too new").Default("false").Bool() var migrateFrom = flag.Make().LongKey("migrate-db").Usage("Source database type and URI to migrate from.").Bool() var wantHelp, _ = flag.MakeHelpFlag() @@ -347,6 +367,18 @@ func (bridge *Bridge) Main() { } else if *migrateFrom { bridge.MigrateDatabase() return + } else if *version { + if Tag == Version { + fmt.Printf("%s %s (%s)\n", Name, Tag, BuildTime) + } else if len(Commit) > 8 { + if !strings.HasSuffix(Version, "+dev") { + Version += "+dev" + } + fmt.Printf("%s %s.%s (%s)\n", Name, Version, Commit[:8], BuildTime) + } else { + fmt.Printf("%s %s+dev.unknown\n", Name, Version) + } + return } bridge.Init() diff --git a/user.go b/user.go index fed3dd7..9e33cb8 100644 --- a/user.go +++ b/user.go @@ -235,7 +235,7 @@ func (user *User) Connect(evenIfNoSession bool) bool { return false } user.Conn = whatsappExt.ExtendConn(conn) - _ = user.Conn.SetClientName("Mautrix-WhatsApp bridge", "mx-wa", "0.1.0") + _ = user.Conn.SetClientName("Mautrix-WhatsApp bridge", "mx-wa", Version) user.log.Debugln("WhatsApp connection successful") user.Conn.AddHandler(user) return user.RestoreSession()