Skip to content

Commit 29bbf0b

Browse files
committed
Update build steps
- Use gox (github.com/mitchellh/gox) for static builds - Use compile time variables for version/build hash - Add shorthand flags
1 parent 5069fa2 commit 29bbf0b

File tree

7 files changed

+102
-71
lines changed

7 files changed

+102
-71
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _testmain.go
3333
fsql
3434
debug
3535
main
36-
build/
36+
dist/
3737

3838
# Allow cmd/fsql
3939
!cmd/fsql

Makefile

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,103 @@
1+
PREFIX ?= $(shell pwd)
2+
13
NAME = fsql
2-
MAIN = ./cmd/fsql
4+
PKG = github.com/kshvmdn/$(NAME)
5+
MAIN = $(PKG)/cmd/$(NAME)
6+
7+
DIST_DIR := ${PREFIX}/dist
8+
DIST_DIRS := find . -type d | sed "s|^\./||" | grep -v \\. | tr '\n' '\0' | xargs -0 -I '{}'
9+
310
SRCS := $(shell find . -type f -name '*.go')
411
PKGS := $(shell go list ./... | grep -v /vendor)
512

6-
build = GOOS=$(1) GOARCH=$(2) go build -o build/$(NAME)$(3) $(MAIN)
7-
tar = cd build && tar -cvzf $(1)_$(2).tar.gz $(NAME)$(3) && rm $(NAME)$(3)
8-
zip = cd build && zip $(1)_$(2).zip $(NAME)$(3) && rm $(NAME)$(3)
13+
VERSION := $(shell cat VERSION)
14+
GITCOMMIT := $(shell git rev-parse --short HEAD)
15+
ifneq ($(shell git status --porcelain --untracked-files=no),)
16+
GITCOMMIT := $(GITCOMMIT)-dirty
17+
endif
918

10-
.PHONY: all coverage clean fmt fmt-save get-tools install lint test vet
11-
.DEFAULT: all
19+
LDFLAGS := ${LDFLAGS} \
20+
-X $(PKG)/meta.GITCOMMIT=${GITCOMMIT} \
21+
-X $(PKG)/meta.VERSION=${VERSION}
1222

13-
all: fsql
23+
.PHONY: build
24+
build: $(SRCS) VERSION
25+
@echo "+ $@"
26+
@go build -ldflags "${LDFLAGS}" -o $(NAME) -v $(MAIN)
1427

28+
.PHONY: install
1529
install:
1630
@echo "+ $@"
1731
@go install $(PKGS)
1832

33+
.PHONY: get-tools
1934
get-tools:
2035
@echo "+ $@"
2136
@go get -u -v github.com/golang/lint/golint
2237

38+
.PHONY: clean
2339
clean:
2440
@echo "+ $@"
25-
rm -rf build ./$(NAME)
26-
mkdir -p build
27-
28-
fsql: $(SRCS)
29-
@echo "+ $@"
30-
@go build -o ./$(NAME) -v $(MAIN)
41+
$(RM) $(NAME)
42+
$(RM) -r $(DIST_DIR)
3143

44+
.PHONY: fmt
3245
fmt:
3346
@echo "+ $@"
3447
@test -z "$$(gofmt -s -l . 2>&1 | grep -v ^vendor/ | tee /dev/stderr)" || \
3548
(echo >&2 "+ please format Go code with 'gofmt -s', or use 'make fmt-save'" && false)
3649

50+
.PHONY: fmt-save
3751
fmt-save:
3852
@echo "+ $@"
3953
@gofmt -s -l . 2>&1 | grep -v ^vendor/ | xargs gofmt -s -l -w
4054

55+
.PHONY: vet
4156
vet:
4257
@echo "+ $@"
4358
@go vet $(PKGS)
4459

60+
.PHONY: lint
4561
lint:
4662
@echo "+ $@"
47-
$(if $(shell which golint || echo ''), , \
63+
$(if $(shell which golint || echo ''),, \
4864
$(error Please install golint: `make get-tools`))
4965
@test -z "$$(golint ./... 2>&1 | grep -v ^vendor/ | grep -v mock/ | tee /dev/stderr)"
5066

67+
.PHONY: test
5168
test:
5269
@echo "+ $@"
53-
@go test -race -v $(PKGS)
70+
@go test -race $(PKGS)
5471

72+
.PHONY: coverage
5573
coverage:
5674
@echo "+ $@"
5775
@for pkg in $(PKGS); do \
5876
go test -test.short -race -coverprofile="../../../$$pkg/coverage.txt" $${pkg} || exit 1; \
5977
done
6078

61-
binaries: darwin linux windows
62-
63-
##### DARWIN BUILDS #####
64-
65-
darwin: build/darwin_amd64.tar.gz
66-
67-
build/darwin_amd64.tar.gz: $(SRCS)
68-
$(call build,darwin,amd64,)
69-
$(call tar,darwin,amd64)
70-
71-
##### LINUX BUILDS #####
72-
73-
linux: build/linux_arm.tar.gz build/linux_arm64.tar.gz build/linux_386.tar.gz build/linux_amd64.tar.gz
74-
75-
build/linux_386.tar.gz: $(SRCS)
76-
$(call build,linux,386,)
77-
$(call tar,linux,386)
78-
79-
build/linux_amd64.tar.gz: $(SRCS)
80-
$(call build,linux,amd64,)
81-
$(call tar,linux,amd64)
82-
83-
build/linux_arm.tar.gz: $(SRCS)
84-
$(call build,linux,arm,)
85-
$(call tar,linux,arm)
86-
87-
build/linux_arm64.tar.gz: $(SRCS)
88-
$(call build,linux,arm64,)
89-
$(call tar,linux,arm64)
90-
91-
##### WINDOWS BUILDS #####
92-
93-
windows: build/windows_386.zip build/windows_amd64.zip
94-
95-
build/windows_386.zip: $(SRCS)
96-
$(call build,windows,386,.exe)
97-
$(call zip,windows,386,.exe)
79+
.PHONY: bootstrap-dist
80+
bootstrap-dist:
81+
@echo "+ $@"
82+
@go get -u -v github.com/franciscocpg/gox
9883

99-
build/windows_amd64.zip: $(SRCS)
100-
$(call build,windows,amd64,.exe)
101-
$(call zip,windows,amd64,.exe)
84+
.PHONY: build-all
85+
build-all: $(SRCS) VERSION
86+
@echo "+ $@"
87+
@gox -verbose \
88+
-ldflags "${LDFLAGS}" \
89+
-os="darwin freebsd netbsd openbsd linux windows" \
90+
-arch="386 amd64 arm arm64" \
91+
-osarch="!darwin/arm !darwin/arm64" \
92+
-output="$(DIST_DIR)/{{.OS}}-{{.Arch}}/{{.Dir}}" $(MAIN)
93+
94+
.PHONY: dist
95+
dist: build-all
96+
@echo "+ $@"
97+
@cd $(DIST_DIR) && \
98+
$(DIST_DIRS) cp ../LICENSE {} && \
99+
$(DIST_DIRS) cp ../README.md {} && \
100+
$(DIST_DIRS) tar -zcf fsql-${VERSION}-{}.tar.gz {} && \
101+
$(DIST_DIRS) zip -r -q fsql-${VERSION}-{}.zip {} && \
102+
$(DIST_DIRS) rm -rf {} && \
103+
cd ..

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,34 @@
1616

1717
[![fsql.gif](./media/fsql.gif)](https://asciinema.org/a/120534)
1818

19-
## Setup / installation
19+
## Installation
2020

21-
Assumes that Go is [installed](https://golang.org/doc/install) and [setup](https://golang.org/doc/install#testing).
21+
#### Binaries
2222

23-
Install with `go get`:
23+
[View latest release](https://github.com/kshvmdn/fsql/releases/latest).
24+
25+
#### Via Go
2426

2527
```sh
2628
$ go get -u -v github.com/kshvmdn/fsql/...
2729
$ which fsql
2830
$GOPATH/bin/fsql
2931
```
3032

31-
Install with [Homebrew](https://brew.sh/):
33+
#### Via Homebrew
3234

33-
```console
35+
```sh
3436
$ brew install fsql
3537
$ which fsql
3638
/usr/local/bin/fsql
3739
```
3840

39-
Install directly via source:
41+
#### Build manually
4042

4143
```sh
4244
$ git clone https://github.com/kshvmdn/fsql.git $GOPATH/src/github.com/kshvmdn/fsql
4345
$ cd $_ # $GOPATH/src/github.com/kshvmdn/fsql
44-
$ make install fsql
46+
$ make
4547
$ ./fsql
4648
```
4749

@@ -56,8 +58,10 @@ View the usage dialogue with the `-help` flag.
5658
```sh
5759
$ fsql -help
5860
usage: fsql [options] query
61+
-i run in interactive mode (shorthand)
5962
-interactive
6063
run in interactive mode (Ctrl+D to exit)
64+
-v print version and exit (shorthand)
6165
-version
6266
print version and exit
6367
```
@@ -327,12 +331,14 @@ Before submitting code, please ensure that tests are passing and the linter is h
327331

328332
```sh
329333
$ make install \
330-
get-tools \
331-
fmt \
334+
get-tools
335+
$ make fmt \
332336
vet \
333-
lint \
334-
test \
337+
lint
338+
$ make test \
335339
coverage
340+
$ make bootstrap-dist \
341+
dist
336342
```
337343

338344
## License

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.1

cmd/fsql/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/kshvmdn/fsql"
11+
"github.com/kshvmdn/fsql/meta"
1112
)
1213

1314
var options struct {
@@ -28,13 +29,17 @@ func main() {
2829
fmt.Printf("usage: %s [options] query\n", os.Args[0])
2930
flag.PrintDefaults()
3031
}
32+
3133
flag.BoolVar(&options.interactive, "interactive", false,
3234
"run in interactive mode (Ctrl+D to exit)")
35+
flag.BoolVar(&options.interactive, "i", false, "run in interactive mode (shorthand)")
3336
flag.BoolVar(&options.version, "version", false, "print version and exit")
37+
flag.BoolVar(&options.version, "v", false,
38+
"print version and exit (shorthand)")
3439
flag.Parse()
3540

3641
if options.version {
37-
fmt.Printf("fsql v%v\n", fsql.Version)
42+
fmt.Printf("%s\n", meta.Version())
3843
os.Exit(0)
3944
}
4045

fsql.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import (
99
"github.com/kshvmdn/fsql/query"
1010
)
1111

12-
// Version holds the current version number.
13-
const Version = "0.2.1"
14-
1512
var q *query.Query
1613
var attrs = [...]string{"mode", "size", "time", "hash", "name"}
1714

meta/meta.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package meta
2+
3+
import "fmt"
4+
5+
// GITCOMMIT indicates which git hash the binary was built off of.
6+
var GITCOMMIT string
7+
8+
// VERSION indicates which version of the binary is running.
9+
var VERSION string
10+
11+
const majorRelease = "0.2.x"
12+
13+
// Version returns the version/commit string.
14+
func Version() string {
15+
version, commit := VERSION, GITCOMMIT
16+
if commit == "" && version == "" {
17+
version, commit = majorRelease, "master"
18+
}
19+
return fmt.Sprintf("fsql version %v, built off %v", version, commit)
20+
}

0 commit comments

Comments
 (0)