Skip to content

Commit

Permalink
move cmd into pkg/cli and simplify entry point. add goreleaser config…
Browse files Browse the repository at this point in the history
…uration for automated dist releases.
  • Loading branch information
dannyvankooten committed Oct 30, 2018
1 parent e6704fa commit 02c2eb9
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules
!.env.example
coverage.out
build
dist
*.db
fathom
!cmd/fathom
Expand Down
8 changes: 5 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
31 changes: 9 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,55 +1,42 @@
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/...

.PHONY: npm
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:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<email> --password=<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=<email> --password=<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).

Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions cmd/fathom/main.go → pkg/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"os"
Expand All @@ -15,21 +15,18 @@ 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", "")

// setup CLI app
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{
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/fathom/server.go → pkg/cli/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion cmd/fathom/stats.go → pkg/cli/stats.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion cmd/fathom/user.go → pkg/cli/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"errors"
Expand Down

0 comments on commit 02c2eb9

Please sign in to comment.