From 02c2eb93f24c5013f2080ddb5715aa1575c5b96a Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Tue, 30 Oct 2018 20:08:48 +0100 Subject: [PATCH] move cmd into pkg/cli and simplify entry point. add goreleaser configuration for automated dist releases. --- .gitignore | 1 + .goreleaser.yml | 8 ++++--- Makefile | 31 ++++++++-------------------- README.md | 6 +++--- main.go | 24 +++++++++++++++++++++ package-lock.json | 4 ++-- cmd/fathom/main.go => pkg/cli/cli.go | 14 +++++-------- {cmd/fathom => pkg/cli}/server.go | 2 +- {cmd/fathom => pkg/cli}/stats.go | 2 +- {cmd/fathom => pkg/cli}/user.go | 2 +- 10 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 main.go rename cmd/fathom/main.go => pkg/cli/cli.go (87%) rename {cmd/fathom => pkg/cli}/server.go (99%) rename {cmd/fathom => pkg/cli}/stats.go (99%) rename {cmd/fathom => pkg/cli}/user.go (99%) diff --git a/.gitignore b/.gitignore index b0d724f7..b41dd5bb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ node_modules !.env.example coverage.out build +dist *.db fathom !cmd/fathom diff --git a/.goreleaser.yml b/.goreleaser.yml index fa38dd95..989148f3 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,8 +1,10 @@ -# This is an example goreleaser.yaml file with some sane defaults. -# Make sure to check the documentation at http://goreleaser.com +# Documentation http://goreleaser.com +before: + hooks: + - make assets/dist builds: - - main: ./cmd/fathom/main.go + main: main.go goos: - linux goarch: diff --git a/Makefile b/Makefile index f999a29d..d5d88163 100644 --- a/Makefile +++ b/Makefile @@ -1,37 +1,24 @@ -DIST := build EXECUTABLE := fathom -LDFLAGS += -extldflags "-static" -X "main.Version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -MAIN_PKG := ./cmd/fathom +LDFLAGS += -extldflags "-static" -X "main.version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -X "main.commit=$(shell git rev-parse HEAD)" +MAIN_PKG := ./main.go PACKAGES ?= $(shell go list ./... | grep -v /vendor/) -JS_SOURCES ?= $(shell find assets/src/. -name "*.js" -type f) +ASSET_SOURCES ?= $(shell find assets/src/. -type f) GO_SOURCES ?= $(shell find . -name "*.go" -type f) -SQL_SOURCES ?= $(shell find . -name "*.sql" -type f) -ENV ?= $(shell export $(cat .env | xargs)) GOPATH=$(shell go env GOPATH) -$(EXECUTABLE): $(GO_SOURCES) assets/build - go build -o $@ $(MAIN_PKG) - .PHONY: all all: build -.PHONY: install -install: $(wildcard *.go) $(GOPATH)/bin/packr - $(GOPATH)/bin/packr install -v -ldflags '-w $(LDFLAGS)' $(MAIN_PKG) - .PHONY: build build: $(EXECUTABLE) +$(EXECUTABLE): $(GO_SOURCES) assets/build + go build -o $@ $(MAIN_PKG) + .PHONY: docker docker: $(GO_SOURCES) GOOS=linux GOARCH=amd64 $(GOPATH)/bin/packr build -v -ldflags '-w $(LDFLAGS)' -o $(EXECUTABLE) $(MAIN_PKG) -.PHONY: dist -dist: assets/dist - GOOS=linux GOARCH=amd64 $(GOPATH)/bin/packr build -v -ldflags '-w $(LDFLAGS)' -o build/fathom-linux-amd64 $(MAIN_PKG) - GOOS=linux GOARCH=arm64 $(GOPATH)/bin/packr build -v -ldflags '-w $(LDFLAGS)' -o build/fathom-linux-arm64 $(MAIN_PKG) - GOOS=linux GOARCH=386 $(GOPATH)/bin/packr build -v -ldflags '-w $(LDFLAGS)' -o build/fathom-linux-386 $(MAIN_PKG) - $(GOPATH)/bin/packr: GOBIN=$(GOPATH)/bin go get github.com/gobuffalo/packr/... @@ -39,17 +26,17 @@ $(GOPATH)/bin/packr: npm: if [ ! -d "node_modules" ]; then npm install; fi -assets/build: $(JS_SOURCES) npm +assets/build: $(ASSET_SOURCES) npm ./node_modules/gulp/bin/gulp.js -assets/dist: $(JS_SOURCES) npm +assets/dist: $(ASSET_SOURCES) npm NODE_ENV=production ./node_modules/gulp/bin/gulp.js .PHONY: clean clean: go clean -i ./... packr clean - rm -rf $(EXECUTABLE) $(DIST) + rm -rf $(EXECUTABLE) .PHONY: fmt fmt: diff --git a/README.md b/README.md index 478e2e4e..ea238a20 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ For getting a development version of Fathom up & running, go through the followi 1. Ensure you have [Go](https://golang.org/doc/install#install) and [NPM](https://www.npmjs.com) installed 1. Download the code: `git clone https://github.com/usefathom/fathom.git $GOPATH/src/github.com/usefathom/fathom` -1. Compile the project: `make build` +1. Compile the project into an executable: `make build` 1. (Optional) Set [custom configuration values](docs/Configuration.md) -1. (Optional) Register a user account: `fathom user add --email= --password=` -1. Start the webserver: `fathom server` and then visit **http://localhost:8080** to access your analytics dashboard +1. (Optional) Register a user account: `./fathom user add --email= --password=` +1. Start the webserver: `./fathom server` and then visit **http://localhost:8080** to access your analytics dashboard To install and run Fathom in production, [see the installation instructions](docs/Installation%20instructions.md). diff --git a/main.go b/main.go new file mode 100644 index 00000000..eb00dec1 --- /dev/null +++ b/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + + "github.com/usefathom/fathom/pkg/cli" +) + +var ( + version = "dev" + commit = "none" + date = "unknown" +) + +func main() { + err := cli.Run(version) + if err != nil { + fmt.Print(err) + os.Exit(1) + } + + os.Exit(0) +} diff --git a/package-lock.json b/package-lock.json index dbc6f3f6..1b3d5d63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6090,7 +6090,7 @@ }, "pretty-hrtime": { "version": "1.0.3", - "resolved": "http://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", + "resolved": "https://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", "integrity": "sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=", "dev": true }, @@ -6510,7 +6510,7 @@ }, "safe-regex": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", "integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", "dev": true, "requires": { diff --git a/cmd/fathom/main.go b/pkg/cli/cli.go similarity index 87% rename from cmd/fathom/main.go rename to pkg/cli/cli.go index 565c8009..7f64f2b5 100644 --- a/cmd/fathom/main.go +++ b/pkg/cli/cli.go @@ -1,4 +1,4 @@ -package main +package cli import ( "os" @@ -15,13 +15,10 @@ type App struct { config *config.Config } -// Version of build, supplied at compile time -var Version = "latest-development" - // CLI application var app *App -func main() { +func Run(v string) error { // force all times in UTC, regardless of server timezone os.Setenv("TZ", "") @@ -29,7 +26,7 @@ func main() { app = &App{cli.NewApp(), nil, nil} app.Name = "Fathom" app.Usage = "simple & transparent website analytics" - app.Version = Version + app.Version = v app.HelpName = "fathom" app.Flags = []cli.Flag{ cli.StringFlag{ @@ -52,11 +49,10 @@ func main() { err := app.Run(os.Args) if err != nil { - log.Fatal(err) - os.Exit(1) + return err } - os.Exit(0) + return nil } func before(c *cli.Context) error { diff --git a/cmd/fathom/server.go b/pkg/cli/server.go similarity index 99% rename from cmd/fathom/server.go rename to pkg/cli/server.go index 8ab3dd25..5759fa9b 100644 --- a/cmd/fathom/server.go +++ b/pkg/cli/server.go @@ -1,4 +1,4 @@ -package main +package cli import ( "net/http" diff --git a/cmd/fathom/stats.go b/pkg/cli/stats.go similarity index 99% rename from cmd/fathom/stats.go rename to pkg/cli/stats.go index 29f18fce..a9e72b82 100644 --- a/cmd/fathom/stats.go +++ b/pkg/cli/stats.go @@ -1,4 +1,4 @@ -package main +package cli import ( "encoding/json" diff --git a/cmd/fathom/user.go b/pkg/cli/user.go similarity index 99% rename from cmd/fathom/user.go rename to pkg/cli/user.go index e95d1236..7ad4bf87 100644 --- a/cmd/fathom/user.go +++ b/pkg/cli/user.go @@ -1,4 +1,4 @@ -package main +package cli import ( "errors"