From b2a45c3abab03bd266a8bf8dd6b6a0955bd73716 Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Wed, 17 Mar 2021 12:25:35 +0100 Subject: [PATCH] Add Makefile Signed-off-by: Julien Riou --- .gitignore | 1 + Makefile | 19 +++++++++++++++++++ README.md | 17 +++++++++++++++++ build.sh | 16 ---------------- cmd/pgterminate/main.go | 18 +++++++++++++++++- go.mod | 9 +++++++++ go.sum | 15 +++++++++++++++ 7 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile delete mode 100755 build.sh create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bae228a --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +BINARY := pgterminate +GOOS := linux +GOARCH := amd64 +APPVERSION := $(shell cat ./VERSION) +GOVERSION := $(shell go version | awk '{print $$3}') +GITCOMMIT := $(shell git log -1 --oneline | awk '{print $$1}') +LDFLAGS = -X main.AppVersion=${APPVERSION} -X main.GoVersion=${GOVERSION} -X main.GitCommit=${GITCOMMIT} + +.PHONY: clean + +build: + go build -ldflags "${LDFLAGS}" -o bin/${BINARY} cmd/${BINARY}/main.go + +release: + go build -ldflags "${LDFLAGS}" -o bin/${BINARY} cmd/${BINARY}/main.go + (cd bin && tar czf ${BINARY}-${APPVERSION}-${GOOS}-${GOARCH}.tar.gz ${BINARY}) + +clean: + rm -rf bin diff --git a/README.md b/README.md index a39bad6..920670e 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,23 @@ There's two ways to configure `pgterminate`: Configuration file options **override** command-line arguments +# Build + +Create binary: +``` +make +``` + +Create release tarball: +``` +make release +``` + +Cleanup: +``` +make clean +``` + # Usage Connect to a remote instance and prompt for password: ``` diff --git a/build.sh b/build.sh deleted file mode 100755 index 14e1dce..0000000 --- a/build.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -BINARY=pgterminate -VERSION=$(cat VERSION) -BUILD_PATH=/tmp/${BINARY}-${VERSION} -ldflags="-X main.AppVersion=${VERSION}" -GOOS=linux -GOARCH=amd64 - -export GOOS -export GOARCH - -go build -ldflags "$ldflags" -o ${BUILD_PATH}/${BINARY} cmd/${BINARY}/main.go -(cd ${BUILD_PATH} && tar czf ${BINARY}-${VERSION}-${GOOS}-${GOARCH}.tar.gz ${BINARY}) - -echo "Archive created:" -ls -l ${BUILD_PATH}/${BINARY}-${VERSION}-${GOOS}-${GOARCH}.tar.gz \ No newline at end of file diff --git a/cmd/pgterminate/main.go b/cmd/pgterminate/main.go index dd0771c..cecc413 100644 --- a/cmd/pgterminate/main.go +++ b/cmd/pgterminate/main.go @@ -20,6 +20,15 @@ import ( // AppVersion stores application version at compilation time var AppVersion string +// AppName to store application name +var AppName string = "pgterminate" + +// GitCommit to set git commit at compilation time (can be empty) +var GitCommit string + +// GoVersion to set Go version at compilation time +var GoVersion string + func main() { var err error config := base.NewConfig() @@ -68,7 +77,7 @@ func main() { if AppVersion == "" { AppVersion = "unknown" } - fmt.Println(AppVersion) + showVersion() return } @@ -179,3 +188,10 @@ func removePid(file string) { base.Panic(err) } } + +func showVersion() { + if GitCommit != "" { + AppVersion = fmt.Sprintf("%s-%s", AppVersion, GitCommit) + } + fmt.Printf("%s version %s (compiled with %s)\n", AppName, AppVersion, GoVersion) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f21c6c2 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/jouir/pgterminate + +go 1.13 + +require ( + github.com/lib/pq v1.10.0 + golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b + gopkg.in/yaml.v2 v2.4.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..564e5f3 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E= +github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b h1:wSOdpTq0/eI46Ez/LkDwIsAKA71YP2SRKBODiRWM0as= +golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=