From ff7243e0ca5f6a5c9d4d3f0b717ba964e53c7e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Mon, 8 Jan 2024 16:51:27 +0100 Subject: [PATCH] Add prepare-static-check target back --- Makefile | 22 ++++++------- internal/makefile/makefile.go | 62 +++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 0bcbd02..0de3020 100644 --- a/Makefile +++ b/Makefile @@ -49,9 +49,13 @@ comma := , check: FORCE static-check build/cover.html build-all @printf "\e[1;32m>> All checks successful.\e[0m\n" -run-golangci-lint: FORCE - @printf "\e[1;36m>> golangci-lint\e[0m\n" +prepare-static-check: FORCE @if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi + @if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi + @if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi + +run-golangci-lint: FORCE prepare-static-check + @printf "\e[1;36m>> golangci-lint\e[0m\n" @golangci-lint run build/cover.out: FORCE | build @@ -62,7 +66,7 @@ build/cover.html: build/cover.out @printf "\e[1;36m>> go tool cover > build/cover.html\e[0m\n" @go tool cover -html $< -o $@ -static-check: FORCE run-golangci-lint check-license-headers +static-check: FORCE run-golangci-lint check-dependency-licenses check-license-headers build: @mkdir $@ @@ -77,19 +81,15 @@ vendor-compat: FORCE go mod vendor go mod verify -prepare-addlicense: FORCE - @if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi - -license-headers: FORCE prepare-addlicense +license-headers: FORCE prepare-static-check @printf "\e[1;36m>> addlicense\e[0m\n" @addlicense -c "SAP SE" -ignore "vendor/**" -- **/*.go -check-license-headers: FORCE prepare-addlicense +check-license-headers: FORCE prepare-static-check @printf "\e[1;36m>> addlicense\e[0m\n" @bash -c 'shopt -s globstar; addlicense --check -ignore "vendor/**" -- **/*.go' -check-dependency-licenses: FORCE - @if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi +check-dependency-licenses: FORCE prepare-static-check @printf "\e[1;36m>> go-licence-detector\e[0m\n" @go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules .license-scan-rules.json -overrides .license-scan-overrides.jsonl @@ -120,6 +120,7 @@ help: FORCE @printf "\n" @printf "\e[1mTest\e[0m\n" @printf " \e[36mcheck\e[0m Run the test suite (unit tests and golangci-lint).\n" + @printf " \e[36mprepare-static-check\e[0m Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager\n" @printf " \e[36mrun-golangci-lint\e[0m Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.\n" @printf " \e[36mbuild/cover.out\e[0m Run tests and generate coverage report.\n" @printf " \e[36mbuild/cover.html\e[0m Generate an HTML file with source code annotations from the coverage report.\n" @@ -128,7 +129,6 @@ help: FORCE @printf "\e[1mDevelopment\e[0m\n" @printf " \e[36mvendor\e[0m Run go mod tidy, go mod verify, and go mod vendor.\n" @printf " \e[36mvendor-compat\e[0m Same as 'make vendor' but go mod tidy will use '-compat' flag with the Go version from go.mod file as value.\n" - @printf " \e[36mprepare-addlicense\e[0m Install addlicense\n" @printf " \e[36mlicense-headers\e[0m Add license headers to all .go files excluding the vendor directory.\n" @printf " \e[36mcheck-license-headers\e[0m Check license headers in all .go files excluding the vendor directory.\n" @printf " \e[36mcheck-dependency-licenses\e[0m Check all dependency licenses using go-licence-detector.\n" diff --git a/internal/makefile/makefile.go b/internal/makefile/makefile.go index 4c471e6..faad936 100644 --- a/internal/makefile/makefile.go +++ b/internal/makefile/makefile.go @@ -125,6 +125,8 @@ endif test.addDefinition(`space := $(null) $(null)`) test.addDefinition(`comma := ,`) + isSAPCC := strings.HasPrefix(sr.ModulePath, "github.com/sapcc") || strings.HasPrefix(sr.ModulePath, "github.wdf.sap.corp") || strings.HasPrefix(sr.ModulePath, "github.tools.sap") + //add main testing target checkPrerequisites := []string{"static-check", "build/cover.html"} if hasBinaries { @@ -139,15 +141,36 @@ endif }) //add target for installing dependencies for `make check` + prepareStaticRecipe := []string{ + `@if ! hash golangci-lint 2>/dev/null; then` + + ` printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n";` + + ` go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi`, + } + if isSAPCC { + prepareStaticRecipe = append(prepareStaticRecipe, []string{ + `@if ! hash go-licence-detector 2>/dev/null; then` + + ` printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n";` + + ` go install go.elastic.co/go-licence-detector@latest; fi`, + `@if ! hash addlicense 2>/dev/null; then ` + + ` printf "\e[1;36m>> Installing addlicense...\e[0m\n"; ` + + ` go install github.com/google/addlicense@latest; fi`, + }...) + } test.addRule(rule{ - description: "Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.", + description: "Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager", phony: true, - target: "run-golangci-lint", + target: "prepare-static-check", + recipe: prepareStaticRecipe, + }) + + // add target to run golangci-lint + test.addRule(rule{ + description: "Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.", + phony: true, + target: "run-golangci-lint", + prerequisites: []string{"prepare-static-check"}, recipe: []string{ `@printf "\e[1;36m>> golangci-lint\e[0m\n"`, - `@if ! hash golangci-lint 2>/dev/null;` + - ` then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n";` + - ` go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi`, `@golangci-lint run`, }, }) @@ -222,8 +245,6 @@ endif }) } - isSAPCC := strings.HasPrefix(sr.ModulePath, "github.com/sapcc") || strings.HasPrefix(sr.ModulePath, "github.wdf.sap.corp") || strings.HasPrefix(sr.ModulePath, "github.tools.sap") - if isSAPCC { // Default behavior is to check all Go files excluding the vendor directory. patterns := []string{"**/*.go"} @@ -240,20 +261,11 @@ endif ignorePatterns[i] = fmt.Sprintf("-ignore %q", v) } - dev.addRule(rule{ - description: "Install addlicense", - target: "prepare-addlicense", - phony: true, - recipe: []string{ - `@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi`, - }, - }) - dev.addRule(rule{ description: "Add license headers to all .go files excluding the vendor directory.", target: "license-headers", phony: true, - prerequisites: []string{"prepare-addlicense"}, + prerequisites: []string{"prepare-static-check"}, recipe: []string{ `@printf "\e[1;36m>> addlicense\e[0m\n"`, fmt.Sprintf(`@addlicense -c "SAP SE" %s -- %s`, @@ -266,7 +278,7 @@ endif description: "Check license headers in all .go files excluding the vendor directory.", target: "check-license-headers", phony: true, - prerequisites: []string{"prepare-addlicense"}, + prerequisites: []string{"prepare-static-check"}, recipe: []string{ `@printf "\e[1;36m>> addlicense\e[0m\n"`, fmt.Sprintf(`@bash -c 'shopt -s globstar; addlicense --check %s -- %s'`, @@ -282,11 +294,12 @@ endif must.Succeed(os.WriteFile(scanOverridesFile, scanOverrides, 0666)) dev.addRule(rule{ - description: "Check all dependency licenses using go-licence-detector.", - target: "check-dependency-licenses", - phony: true, + description: "Check all dependency licenses using go-licence-detector.", + target: "check-dependency-licenses", + phony: true, + prerequisites: []string{"prepare-static-check"}, recipe: []string{ - `@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi`, + `@printf "\e[1;36m>> go-licence-detector\e[0m\n"`, fmt.Sprintf(`@go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules %s -overrides %s`, licenseRulesFile, scanOverridesFile), @@ -297,7 +310,7 @@ endif //add target for static code checks staticCheckPrerequisites := []string{"run-golangci-lint"} if isSAPCC { - staticCheckPrerequisites = append(staticCheckPrerequisites, "check-license-headers") + staticCheckPrerequisites = append(staticCheckPrerequisites, "check-dependency-licenses", "check-license-headers") } test.addRule(rule{ description: "Run static code checks", @@ -366,8 +379,7 @@ func makeDefaultLinkerFlags(binaryName string, sr core.ScanResult) string { return flags } -// installTarget also returns a bool that tells whether the install target was requested -// in the config. +// installTarget also returns a bool that tells whether the install target was requested in the config. func installTarget(binaries []core.BinaryConfiguration) (rule, bool) { r := rule{ description: "Install all binaries. " +