Add build system and version info

This commit is contained in:
James Mills 2018-03-25 14:37:32 -07:00
parent 9146db7952
commit c799a79c2e
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
8 changed files with 147 additions and 11 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
*~
*.bak

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
*~
*~*
*.bak
cmd/msgbus/msgbus
cmd/msgbusd/msgbusd

35
Dockerfile Normal file
View File

@ -0,0 +1,35 @@
# Build
FROM golang:alpine AS build
ARG TAG
ARG BUILD
ENV LIBRARY msgbus
ENV SERVER msgbusd
ENV CLIENT msgbus
ENV REPO prologic/$LIBRARY
RUN apk add --update git make build-base && \
rm -rf /var/cache/apk/*
WORKDIR /go/src/github.com/$REPO
COPY . /go/src/github.com/$REPO
RUN make TAG=$TAG BUILD=$BUILD build
# Runtime
FROM scratch
ENV LIBRARY msgbud
ENV SERVER msgbusd
ENV CLIENT msgbus
ENV REPO prologic/$LIBRARY
LABEL msgbud.app main
COPY --from=build /go/src/github.com/${REPO}/cmd/${SERVER}/${SERVER} /${SERVER}
COPY --from=build /go/src/github.com/${REPO}/cmd/${CLIENT}/${CLIENT} /${CLIENT}
EXPOSE 8000/tcp
ENTRYPOINT ["/msgbusd"]
CMD []

44
Makefile Normal file
View File

@ -0,0 +1,44 @@
.PHONY: dev build image test deps clean
CGO_ENABLED=0
COMMIT=`git rev-parse --short HEAD`
LIBRARY=msgbus
SERVER=msgbusd
CLIENT=msgbus
REPO?=prologic/$(LIBRARY)
TAG?=latest
BUILD?=-dev
BUILD_TAGS="netgo static_build"
BUILD_LDFLAGS="-w -X github.com/$(REPO).GitCommit=$(COMMIT) -X github.com/$(REPO)/Build=$(BUILD)"
all: dev
dev: build
@./cmd/$(SERVER)/$(SERVER)
deps:
@go get ./...
build: clean deps
@echo " -> Building $(SERVER) $(TAG)$(BUILD) ..."
@cd cmd/$(SERVER) && \
go build -tags $(BUILD_TAGS) -installsuffix netgo \
-ldflags $(BUILD_LDFLAGS) .
@echo "Built $$(./cmd/$(SERVER)/$(SERVER) -v)"
@echo
@echo " -> Building $(CLIENT) $(TAG)$(BUILD) ..."
@cd cmd/$(CLIENT) && \
go build -tags $(BUILD_TAGS) -installsuffix netgo \
-ldflags $(BUILD_LDFLAGS) .
@echo "Built $$(./cmd/$(CLIENT)/$(CLIENT) -v)"
image:
@docker build --build-arg TAG=$(TAG) --build-arg BUILD=$(BUILD) -t $(REPO):$(TAG) .
@echo "Image created: $(REPO):$(TAG)"
test:
@go test -v -cover -race $(TEST_ARGS)
clean:
@rm -rf $(APP)

View File

@ -7,14 +7,17 @@ import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/prologic/msgbus"
)
var configFile string
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "msgbus",
Short: "Command-line client for msgbus",
Use: "msgbus",
Version: msgbus.FullVersion(),
Short: "Command-line client for msgbus",
Long: `This is the command-line client for the msgbus daemon msgbusd.
This lets you publish, subscribe and pull messages from a running msgbusd

View File

@ -2,26 +2,37 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/prologic/msgbus"
)
var (
bind string
ttl time.Duration
)
func main() {
var (
version bool
bind string
ttl time.Duration
)
flag.BoolVar(&version, "v", false, "display version information")
func init() {
flag.StringVar(&bind, "bind", ":8000", "interface and port to bind to")
flag.DurationVar(&ttl, "ttl", 60*time.Second, "default ttl")
}
func main() {
flag.Parse()
if version {
fmt.Printf(msgbus.FullVersion())
os.Exit(0)
}
options := msgbus.Options{DefaultTTL: ttl}
http.Handle("/", msgbus.NewMessageBus(&options))
log.Printf("msgbusd listening on %s", bind)
log.Printf("%s listening on %s", msgbus.Package, bind)
log.Fatal(http.ListenAndServe(bind, nil))
}

24
version.go Normal file
View File

@ -0,0 +1,24 @@
package msgbus
import (
"fmt"
)
var (
//Package package name
Package = "msgbus"
// Version release version
Version = "0.1.0"
// Build will be overwritten automatically by the build system
Build = "dev"
// GitCommit will be overwritten automatically by the build system
GitCommit = "HEAD"
)
// FullVersion display the full version, build and commit hash
func FullVersion() string {
return fmt.Sprintf("%s-%s-%s@%s", Package, Version, Build, GitCommit)
}

15
version_test.go Normal file
View File

@ -0,0 +1,15 @@
package msgbus
import (
"testing"
)
func TestFullVersion(t *testing.T) {
version := FullVersion()
expected := Version + Build + " (" + GitCommit + ")"
if version != expected {
t.Fatalf("invalid version returned: %s", version)
}
}