Build/release script and fix version annotation.
authorAndrey Petrov <andrey.petrov@shazow.net>
Sun, 10 Jul 2016 21:57:39 +0000 (17:57 -0400)
committerAndrey Petrov <andrey.petrov@shazow.net>
Sun, 10 Jul 2016 21:57:39 +0000 (17:57 -0400)
Makefile
build_release [new file with mode: 0755]
cmd/ssh-chat/cmd.go
host.go

index 735c767fb54220826e20d4bb43acb34842374e62..18f87dfc6ea7b83acf4970f7df40a08c8ae2ee75 100644 (file)
--- 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 (executable)
index 0000000..2a0831c
--- /dev/null
@@ -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 "$@"
index a4e02c3577047524ac0fd5da564a6c08dee096c7..42891e910a2bbcd82602cd71c4f19dc6be0353f6 100644 (file)
@@ -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 ca17f70560d64eb47bd4b5ab32c7fffc67de76c8..20633007f0c4ea5e24c61b59bc454713fb5792d6 100644 (file)
--- 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
                },
        })