diff --git a/Makefile b/Makefile index 735c767..18f87df 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,13 @@ KEY = host_key PORT = 2022 SRCS = %.go +VERSION := $(shell git describe --long --tags --dirty --always) +LDFLAGS = LDFLAGS="-X main.Version=$(VERSION)" all: $(BINARY) $(BINARY): deps **/**/*.go **/*.go *.go - go build -ldflags "-X main.buildCommit=`git describe --long --tags --dirty --always`" ./cmd/ssh-chat + go build $(BUILDFLAGS) ./cmd/ssh-chat deps: go get ./... @@ -29,3 +31,8 @@ debug: $(BINARY) $(KEY) test: go test ./... golint ./... + +release: + ENV=GOOS=linux GOARCH=amd64 $(LDFLAGS) ./build_release "github.com/shazow/ssh-chat/cmd/ssh-chat" README.md LICENSE + ENV=GOOS=linux GOARCH=386 $(LDFLAGS) ./build_release "github.com/shazow/ssh-chat/cmd/ssh-chat" README.md LICENSE + ENV=GOOS=darwin GOARCH=amd64 $(LDFLAGS) ./build_release "github.com/shazow/ssh-chat/cmd/ssh-chat" README.md LICENSE diff --git a/build_release b/build_release new file mode 100755 index 0000000..2a0831c --- /dev/null +++ b/build_release @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +usage() { + echo "Build and bundle Go releases with the current dir as the build dir." + echo "Usage: $0 PACKAGE [ASSETS...]" +} + +main() { + set -eo pipefail + [[ "$TRACE" ]] && set -x + + if [[ ! "$1" ]]; then + usage + exit 1 + fi + + if [[ ! "$GOOS" ]]; then + export GOOS="linux" + fi + if [[ ! "$GOARCH" ]]; then + export GOARCH="amd64" + fi + if [[ ! "$BUILDDIR" ]]; then + export BUILDDIR="build" + fi + + build "$@" +} + +build() { + local package="$1"; shift + local assets="$@" + + local bin="$(basename $package)" + local tarball="${bin}-${GOOS}_${GOARCH}.tgz" + local outdir="$BUILDDIR/$bin" + + if [[ -d "$outdir" ]]; then + echo "err: outdir already exists: $PWD/$outdir" + fi + mkdir -p "$outdir" + + go build -ldflags "$LDFLAGS" -o "$outdir/$bin" "$package" + + # Stage asset bundle + if [[ "$assets" ]]; then + ln -f $assets "$outdir" + fi + + # Create tarball + tar -C "$BUILDDIR" -czvf "$BUILDDIR/$tarball" "$bin" + + # Cleanup + rm -rf "$outdir" + + echo "Packaged: $tarball" +} + +main "$@" diff --git a/cmd/ssh-chat/cmd.go b/cmd/ssh-chat/cmd.go index a4e02c3..42891e9 100644 --- a/cmd/ssh-chat/cmd.go +++ b/cmd/ssh-chat/cmd.go @@ -22,9 +22,13 @@ import ( ) import _ "net/http/pprof" +// Version of the binary, assigned during build. +var Version string = "dev" + // Options contains the flag options type Options struct { Verbose []bool `short:"v" long:"verbose" description:"Show verbose logging."` + Version bool `long:"version" description:"Print version and exit."` Identity string `short:"i" long:"identity" description:"Private key to identify server with." default:"~/.ssh/id_rsa"` Bind string `long:"bind" description:"Host and port to listen on." default:"0.0.0.0:2022"` Admin string `long:"admin" description:"File of public keys who are admins."` @@ -63,6 +67,11 @@ func main() { }() } + if options.Version { + fmt.Println(Version) + os.Exit(0) + } + // Figure out the log level numVerbose := len(options.Verbose) if numVerbose > len(logLevels) { @@ -111,6 +120,7 @@ func main() { host := sshchat.NewHost(s, auth) host.SetTheme(message.Themes[0]) + host.Version = Version err = fromFile(options.Admin, func(line []byte) error { key, _, _, _, err := ssh.ParseAuthorizedKey(line) diff --git a/host.go b/host.go index ca17f70..2063300 100644 --- a/host.go +++ b/host.go @@ -13,8 +13,6 @@ import ( "github.com/shazow/ssh-chat/sshd" ) -var buildCommit string - const maxInputLength int = 1024 // GetPrompt will render the terminal prompt string based on the user. @@ -37,6 +35,9 @@ type Host struct { auth *Auth count int + // Version string to print on /version + Version string + // Default theme theme message.Theme } @@ -324,7 +325,7 @@ func (h *Host) InitCommands(c *chat.Commands) { c.Add(chat.Command{ Prefix: "/version", Handler: func(room *chat.Room, msg message.CommandMsg) error { - room.Send(message.NewSystemMsg(buildCommit, msg.From())) + room.Send(message.NewSystemMsg(h.Version, msg.From())) return nil }, })