Skip to content

Commit

Permalink
Merge branch 'tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksherron committed Feb 16, 2020
2 parents 79de0d7 + 88a7202 commit bc7aebe
Show file tree
Hide file tree
Showing 14 changed files with 1,111 additions and 270 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ bin
vendor
dist
dist.bk
internal/test_data
scripts/local
scripts/local
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ language: go
go:
- "1.13.x"

services:
- postgresql

env:
- GO111MODULE=on

install: true

script: go run *.go version
script: go test ./... && go test github.com/nicksherron/bashhub-server/internal -postgres-uri "postgres://postgres:@localhost:5432?sslmode=disable"
54 changes: 17 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,41 @@ VERSION=$(shell git tag | sort --version-sort -r | head -1)
GIT_COMMIT=$(shell git rev-parse HEAD)
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
BUILD_DATE=$(shell date '+%Y-%m-%d-%H:%M:%S')
IMAGE_NAME := "nicksherron/bashhub-server"
IMAGE_NAME="nicksherron/bashhub-server"

default: test

default: help

help:
@echo 'Management commands for bashhub-server:'
@echo
@echo 'Usage:'
@echo ' make build Compile the project.'
@echo ' make get-deps runs dep ensure, mostly used for ci.'
@echo ' make build-alpine Compile optimized for alpine linux.'
@echo ' make package Build final docker image with just the go binary inside'
@echo ' make tag Tag image created by package with latest, git commit and version'
@echo ' make docker-build Build docker image'
@echo ' make clean Clean the directory tree'
@echo ' make test Run tests on a compiled project.'
@echo ' make push Push tagged images to registry'
@echo ' make clean Clean the directory tree.'
@echo ' make test-postgres Start postgres in ephemeral docker container and run backend tests.'
@echo ' make test-all Run test and test-postgres.'
@echo

build:
@echo "building ${BIN_NAME} ${VERSION}"
@echo "GOPATH=${GOPATH}"
go build -ldflags "-X github.com/nicksherron/bashhub-server/cmd.Version=${VERSION} -X github.com/nicksherron/bashhub-server/cmd.GitCommit=${GIT_COMMIT} -X github.com/nicksherron/bashhub-server/cmd.BuildDate=${BUILD_DATE}" -o bin/${BIN_NAME}

get-deps:
dep ensure

@echo "building $(BIN_NAME) $(VERSION)"
@echo "GOPATH=$(GOPATH)"
go build -ldflags "-X github.com/nicksherron/bashhub-server/cmd.Version=$(VERSION) -X github.com/nicksherron/bashhub-server/cmd.GitCommit=$(GIT_COMMIT) -X github.com/nicksherron/bashhub-server/cmd.BuildDate=$(BUILD_DATE)" -o bin/${BIN_NAME}

docker-build:
docker build --no-cache=true --build-arg VERSION=${VERSION} --build-arg BUILD_DATE=${BUILD_DATE} --build-arg GIT_COMMIT=${GIT_COMMIT} -t $(IMAGE_NAME) .

clean:
@test ! -e bin/$(BIN_NAME) || rm bin/$(BIN_NAME)

build-alpine:
@echo "building ${BIN_NAME} ${VERSION}"
@echo "GOPATH=${GOPATH}"
go build -ldflags '-w -linkmode external -extldflags "-static" -X github.com/nicksherron/bashhub-server/cmd.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/cmd.BuildDate=${BUILD_DATE}' -o bin/${BIN_NAME}

package:
@echo "building image ${BIN_NAME} ${VERSION} $(GIT_COMMIT)"
docker build --build-arg VERSION=${VERSION} --build-arg GIT_COMMIT=$(GIT_COMMIT) -t $(IMAGE_NAME):local .
test:
go test ./...

tag:
@echo "Tagging: latest ${VERSION} $(GIT_COMMIT)"
docker tag $(IMAGE_NAME) $(IMAGE_NAME):$(GIT_COMMIT)
docker tag $(IMAGE_NAME) $(IMAGE_NAME):${VERSION}
docker tag $(IMAGE_NAME) $(IMAGE_NAME):latest
test-postgres:
scripts/test_postgres.sh

push: docker-build tag
@echo "Pushing docker image to registry: latest ${VERSION} $(GIT_COMMIT)"
docker push $(IMAGE_NAME):$(GIT_COMMIT)
docker push $(IMAGE_NAME):${VERSION}
docker push $(IMAGE_NAME):latest
test-all: test test-postgres

clean:
@test ! -e bin/${BIN_NAME} || rm bin/${BIN_NAME}

test:
go test ./...

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Available Commands:
Flags:
-a, --addr string Ip and port to listen and serve on. (default "http://0.0.0.0:8080")
--db string DB location (sqlite or postgres)
--db string db location (sqlite or postgres)
-h, --help help for this command
--log string Set filepath for HTTP log. "" logs to stderr.
Expand Down
23 changes: 12 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ import (
"github.com/spf13/cobra"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var (
logFile string
dbPath string
addr string
rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cmd.Flags().Parse(args)
checkBhEnv()
startupMessage()
internal.Run()
internal.Run(dbPath, logFile, addr)
},
}
)
Expand All @@ -54,9 +55,9 @@ func Execute() {

func init() {
cobra.OnInitialize()
rootCmd.PersistentFlags().StringVar(&internal.LogFile, "log", "", `Set filepath for HTTP log. "" logs to stderr`)
rootCmd.PersistentFlags().StringVar(&internal.DbPath, "db", dbPath(), "DB location (sqlite or postgres)")
rootCmd.PersistentFlags().StringVarP(&internal.Addr, "addr", "a", listenAddr(), "Ip and port to listen and serve on")
rootCmd.PersistentFlags().StringVar(&logFile, "log", "", `Set filepath for HTTP log. "" logs to stderr`)
rootCmd.PersistentFlags().StringVar(&dbPath, "db", sqlitePath(), "db location (sqlite or postgres)")
rootCmd.PersistentFlags().StringVarP(&addr, "addr", "a", listenAddr(), "Ip and port to listen and serve on")

}

Expand All @@ -74,10 +75,10 @@ func startupMessage() {
\__ \ __/ | \ V / __/ |
|___/\___|_| \_/ \___|_|
`, Version, internal.Addr)
`, Version, addr)
color.HiGreen(banner)
fmt.Print("\n")
log.Printf("Listening and serving HTTP on %v", internal.Addr)
log.Printf("Listening and serving HTTP on %v", addr)
fmt.Print("\n")
}

Expand All @@ -92,7 +93,7 @@ func listenAddr() string {

}

func dbPath() string {
func sqlitePath() string {
dbFile := "data.db"
f := filepath.Join(appDir(), dbFile)
return f
Expand All @@ -118,7 +119,7 @@ func checkBhEnv() {
msg := fmt.Sprintf(`
WARNING: BH_URL is set to https://bashhub.com on this machine
If you will be running bashhub-client locally be sure to add
export BH_URL=%v to your .bashrc or .zshrc`, internal.Addr)
fmt.Println(msg, "\n")
export BH_URL=%v to your .bashrc or .zshrc`, addr)
fmt.Println(msg)
}
}
Loading

0 comments on commit bc7aebe

Please sign in to comment.