1@echo off
2setlocal
3
4set _EXITCODE=0
5
6REM If no target is provided, default to test.
7if [%1]==[] goto test
8
9set _TARGETS=bin,dev,generate,test,testacc,testrace,vet
10
11REM Run target.
12for %%a in (%_TARGETS%) do (if x%1==x%%a goto %%a)
13goto usage
14
15REM bin generates the releaseable binaries for Vault
16:bin
17	call :generate
18	call .\scripts\windows\build.bat "%CD%"
19	goto :eof
20
21REM dev creates binaries for testing Vault locally. These are put
22REM into ./bin/ as well as %GOPATH%/bin
23:dev
24	call :generate
25	call .\scripts\windows\build.bat "%CD%" VAULT_DEV
26	goto :eof
27
28REM generate runs `go generate` to build the dynamically generated
29REM source files.
30:generate
31	go list ./... | findstr /v vendor | go generate
32	goto :eof
33
34REM test runs the unit tests and vets the code.
35:test
36	call :testsetup
37	go test %_TEST% %TESTARGS% -timeout=30s -parallel=4
38	call :setMaxExitCode %ERRORLEVEL%
39	echo.
40	goto vet
41
42REM testacc runs acceptance tests.
43:testacc
44	call :testsetup
45	if x%_TEST% == x./... goto testacc_fail
46	if x%_TEST% == x.\... goto testacc_fail
47	set VAULT_ACC=1
48	go test %_TEST% -v %TESTARGS% -timeout 45m
49	exit /b %ERRORLEVEL%
50:testacc_fail
51	echo ERROR: Set %%TEST%% to a specific package.
52	exit /b 1
53
54REM testrace runs the race checker.
55:testrace
56	call :testsetup
57	go test -race %_TEST% %TESTARGS%
58	exit /b %ERRORLEVEL%
59
60REM testsetup calls `go generate` and defines the variables VAULT_ACC
61REM and _TEST. VAULT_ACC is always cleared. _TEST defaults to the value
62REM of the TEST environment variable, provided that TEST is defined,
63REM otherwise _TEST it is set to "./...".
64:testsetup
65	call :generate
66	set VAULT_ACC=
67	set _TEST=./...
68	if defined TEST set _TEST=%TEST%
69	goto :eof
70
71REM vet runs the Go source code static analysis tool `vet` to find
72REM any common errors.
73:vet
74	set _VETARGS=-asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr
75	if defined VETARGS set _VETARGS=%VETARGS%
76
77	go tool vet 2>nul
78	if %ERRORLEVEL% equ 3 go get golang.org/x/tools/cmd/vet
79
80	set _vetExitCode=0
81	set _VAULT_PKG_DIRS=%TEMP%\vault-pkg-dirs.txt
82
83	go list -f {{.Dir}} ./... | findstr /v vendor >"%_VAULT_PKG_DIRS%"
84	REM Skip the first row, which is the main vault package (.*github.com/hashicorp/vault$)
85	for /f "delims= skip=1" %%d in ("%_VAULT_PKG_DIRS%") do (
86		go tool vet %_VETARGS% "%%d"
87		if ERRORLEVEL 1 set _vetExitCode=1
88		call :setMaxExitCode %_vetExitCode%
89	)
90	del /f "%_VAULT_PKG_DIRS%" 2>NUL
91	if %_vetExitCode% equ 0 exit /b %_EXITCODE%
92	echo.
93	echo Vet found suspicious constructs. Please check the reported constructs
94	echo and fix them if necessary before submitting the code for reviewal.
95	exit /b %_EXITCODE%
96
97:setMaxExitCode
98	if %1 gtr %_EXITCODE% set _EXITCODE=%1
99	goto :eof
100
101:usage
102	echo usage: make [target]
103	echo.
104	echo target is in {%_TARGETS%}.
105	echo target defaults to test if none is provided.
106	exit /b 2
107	goto :eof
108