Skip to content

Commit

Permalink
Introduce a Makefile
Browse files Browse the repository at this point in the history
Heavily borrowed from other providers but with a few modifications and
fixes.

Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Jan 21, 2020
1 parent 72c28b6 commit 96f7635
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*tfstate*
terraform-provider-megaport
crash.log
vendor/
cover.out
133 changes: 133 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
SWEEP?=staging
TEST?=./...
GOFMT_FILES?=$$(find . -not -path "./vendor/*" -type f -name '*.go')
PKG_NAME=megaport
WEBSITE_REPO=github.com/hashicorp/terraform-website

default: build

build: fmtcheck
go install

depscheck:
@echo "==> Checking source code with go mod tidy..."
@go mod tidy
@git diff --exit-code -- go.mod go.sum || \
(echo; echo "Unexpected difference in go.mod/go.sum files. Run 'go mod tidy' command or revert any go.mod/go.sum changes and commit."; exit 1)
@echo "==> Checking source code with go mod vendor..."
@go mod vendor
@git diff --compact-summary --exit-code -- vendor || \
(echo; echo "Unexpected difference in vendor/ directory. Run 'go mod vendor' command or revert any go.mod/go.sum/vendor changes and commit."; exit 1)

docscheck:
@echo "==> Extracting provider json schema..."
$(eval PROVIDER_SCHEMA=$(shell PROVIDER_NAME=$(PKG_NAME) $(CURDIR)/scripts/providerjsonschema.sh))
@echo "==> Checking docs with tfproviderdocs..."
@docker run \
--interactive \
--rm \
--tty \
--volume "$(shell pwd)/website:/terraform-provider-megaport/website" \
--volume "$(PROVIDER_SCHEMA):/provider-schema" \
--workdir /terraform-provider-megaport \
bflad/tfproviderdocs \
check \
-providers-schema-json=/provider-schema/schema.json
@rm -rf $(PROVIDER_SCHEMA)

fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w $(GOFMT_FILES)

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

lint:
@echo "==> Checking code against linters..."
@docker run \
--interactive \
--rm \
--tty \
--volume $(shell pwd):/src \
--workdir /src \
golangci/golangci-lint:latest-alpine \
golangci-lint \
run \
-v \
--timeout=2m \
./...

providerlint:
@echo "==> Checking provider with tfproviderlint..."
@docker run \
--interactive \
--rm \
--tty \
--volume $(shell pwd):/src \
bflad/tfproviderlint:latest \
-c 0 \
./...

sweep:
@echo "WARNING: This will destroy infrastructure. Use only in development accounts."
go test $(TEST) -v -sweep=$(SWEEP) $(SWEEPARGS)

test: fmtcheck
ifdef TEST_COVER
$(eval TESTARGS=$(TESTARGS) -coverprofile=cover.out)
endif
go test $(TEST) -v $(TESTARGS) -count=1 -timeout=1m -parallel=2
ifdef TEST_COVER
go tool cover -html=cover.out
rm cover.out
endif

testacc: fmtcheck
ifdef TEST_COVER
$(eval TESTARGS=$(TESTARGS) -coverprofile=cover.out)
endif
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -count=1 -timeout=5m -parallel=1
ifdef TEST_COVER
go tool cover -html=cover.out
rm cover.out
endif

website: website-setup
@$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME)

websitefmtcheck:
@echo "==> Checking docs formatting..."
@docker run \
--interactive \
--rm \
--tty \
--volume $(shell pwd)/website:/src/website \
--volume $(shell pwd)/scripts:/src/scripts \
--workdir /src \
--entrypoint /bin/sh \
node:alpine \
-c 'apk add --quiet --no-cache bash terraform && scripts/websitefmtcheck.sh'

website-lint:
@echo "==> Checking docs against linters..."
@docker run \
--interactive \
--rm \
--tty \
--volume $(shell pwd)/website:/src/website \
--workdir /src \
golang:alpine \
/bin/sh -c 'GO111MODULE=on go install github.com/client9/misspell/cmd/misspell && misspell -error -source=text website'

website-setup:
ifeq (,$(wildcard $(GOPATH)/src/$(WEBSITE_REPO)))
echo "$(WEBSITE_REPO) not found in your GOPATH (necessary for layouts and assets), get-ting..."
git clone https://$(WEBSITE_REPO) $(GOPATH)/src/$(WEBSITE_REPO)
endif
@ln -sf ../../../../ext/providers/$(PKG_NAME)/website/docs $(GOPATH)/src/$(WEBSITE_REPO)/content/source/docs/providers/$(PKG_NAME)
@ln -sf ../../../ext/providers/$(PKG_NAME)/website/$(PKG_NAME).erb $(GOPATH)/src/$(WEBSITE_REPO)/content/source/layouts/

website-test: website-setup
@$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider-test PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME)

.PHONY: build depscheck docscheck fmt fmtcheck lint providerlint sweep test testacc website websitefmtcheck website-lint website-setup website-test
13 changes: 13 additions & 0 deletions scripts/gofmtcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Check gofmt
echo "==> Checking that code complies with gofmt requirements..."
gofmt_files=$(find . -name '*.go' | grep -v vendor | xargs gofmt -l -s)
if [[ -n ${gofmt_files} ]]; then
echo 'gofmt needs running on the following files:'
echo "${gofmt_files}"
echo "You can use the command: \`make fmt\` to reformat code."
exit 1
fi

exit 0
15 changes: 15 additions & 0 deletions scripts/providerjsonschema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -eou pipefail

tmpd=$(mktemp -d)
go build -o $tmpd .
(
cd $tmpd
echo "provider ${PROVIDER_NAME} {}" > main.tf
terraform init 2>&1 >/dev/null
terraform providers schema -json > schema.json
rm main.tf
rm terraform-provider-${PROVIDER_NAME}
)
echo $tmpd
19 changes: 19 additions & 0 deletions scripts/websitefmtcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -eou pipefail

npm list codedown > /dev/null 2>&1 || npm install --no-save codedown > /dev/null 2>&1

problems=false
for f in $(find website -name '*.md'); do
if [ "${1-}" = "diff" ]; then
echo "$f"
cat "$f" | node_modules/.bin/codedown hcl | terraform fmt -diff=true -
else
cat "$f" | node_modules/.bin/codedown hcl | terraform fmt -check=true - || (problems=true && echo "Formatting errors in $f")
fi
done

if [ "$problems" = true ] ; then
exit 1
fi

0 comments on commit 96f7635

Please sign in to comment.