diff --git a/Makefile b/Makefile index baf69cafbf8..b5b7281e5ed 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: deps test build-modules build -.PHONY: deps test build-modules build image +.PHONY: deps test build-modules build image format # deps will clean out the vendor directory and use go mod for a fresh install deps: @@ -29,3 +29,7 @@ build: test # image will build a docker image image: docker build -t prebid-server . + +# format runs format +format: + ./scripts/format.sh -f true \ No newline at end of file diff --git a/README.md b/README.md index 9b657f35db6..cb64ed9a3b1 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,15 @@ go build . ./prebid-server ``` +Run format: +``` +make format +``` +or +```bash +./scripts/format.sh -f true +``` + Load the landing page in your browser at `http://localhost:8000/`. For the full API reference, see [the endpoint documentation](https://docs.prebid.org/prebid-server/endpoints/pbs-endpoint-overview.html) diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100755 index 00000000000..d5f33e517e4 --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +die() { echo -e "$@" 1>&2 ; exit 1; } + +AUTOFMT=true +while getopts 'f:' OPTION; do + case "$OPTION" in + f) + AUTOFMT="$OPTARG" + ;; + esac +done + +# Build a list of all the top-level directories in the project. +for DIRECTORY in */ ; do + GOGLOB="$GOGLOB ${DIRECTORY%/}" +done +GOGLOB="${GOGLOB/ docs/}" +GOGLOB="${GOGLOB/ vendor/}" + +# Check that there are no formatting issues +GOFMT_LINES=`gofmt -s -l $GOGLOB | tr '\\\\' '/' | wc -l | xargs` +if $AUTOFMT; then + # if there are files with formatting issues, they will be automatically corrected using the gofmt -w command + if [[ $GOFMT_LINES -ne 0 ]]; then + FMT_FILES=`gofmt -s -l $GOGLOB | tr '\\\\' '/' | xargs` + for FILE in $FMT_FILES; do + echo "Running: gofmt -s -w $FILE" + `gofmt -s -w $FILE` + done + fi +else + test $GOFMT_LINES -eq 0 || die "gofmt needs to be run, ${GOFMT_LINES} files have issues. Below is a list of files to review:\n`gofmt -s -l $GOGLOB`" +fi \ No newline at end of file diff --git a/validate.sh b/validate.sh index b81ade344d2..becd42ceb50 100755 --- a/validate.sh +++ b/validate.sh @@ -17,29 +17,7 @@ while true; do esac done -die() { echo -e "$@" 1>&2 ; exit 1; } - -# Build a list of all the top-level directories in the project. -for DIRECTORY in */ ; do - GOGLOB="$GOGLOB ${DIRECTORY%/}" -done -GOGLOB="${GOGLOB/ docs/}" -GOGLOB="${GOGLOB/ vendor/}" - -# Check that there are no formatting issues -GOFMT_LINES=`gofmt -s -l $GOGLOB | tr '\\\\' '/' | wc -l | xargs` -if $AUTOFMT; then - # if there are files with formatting issues, they will be automatically corrected using the gofmt -w command - if [[ $GOFMT_LINES -ne 0 ]]; then - FMT_FILES=`gofmt -s -l $GOGLOB | tr '\\\\' '/' | xargs` - for FILE in $FMT_FILES; do - echo "Running: gofmt -s -w $FILE" - `gofmt -s -w $FILE` - done - fi -else - test $GOFMT_LINES -eq 0 || die "gofmt needs to be run, ${GOFMT_LINES} files have issues. Below is a list of files to review:\n`gofmt -s -l $GOGLOB`" -fi +./scripts/format.sh -f $AUTOFMT # Run the actual tests. Make sure there's enough coverage too, if the flags call for it. if $COVERAGE; then