github.com/dhaiducek/policy-generator-plugin@v1.99.99/Makefile (about) 1 # Copyright (c) 2021 Red Hat, Inc. 2 # Copyright Contributors to the Open Cluster Management project 3 4 PWD := $(shell pwd) 5 LOCAL_BIN ?= $(PWD)/bin 6 7 # Keep an existing GOPATH, make a private one if it is undefined 8 GOPATH_DEFAULT := $(PWD)/.go 9 export GOPATH ?= $(GOPATH_DEFAULT) 10 GOBIN_DEFAULT := $(GOPATH)/bin 11 export GOBIN ?= $(GOBIN_DEFAULT) 12 export PATH := $(LOCAL_BIN):$(GOBIN):$(PATH) 13 TESTARGS_DEFAULT := "-v" 14 export TESTARGS ?= $(TESTARGS_DEFAULT) 15 export DEPENDENCY_OVERRIDE ?= false 16 17 # Kustomize plugin configuration 18 XDG_CONFIG_HOME ?= ${HOME}/.config 19 KUSTOMIZE_PLUGIN_HOME ?= $(XDG_CONFIG_HOME)/kustomize/plugin 20 API_PLUGIN_PATH ?= $(KUSTOMIZE_PLUGIN_HOME)/policy.open-cluster-management.io/v1/policygenerator 21 22 # Kustomize arguments 23 SOURCE_DIR ?= examples/ 24 25 # go-get-tool will 'go install' any package $1 and install it to LOCAL_BIN. 26 define go-get-tool 27 @set -e ;\ 28 echo "Checking installation of $(1)" ;\ 29 GOBIN=$(LOCAL_BIN) go install $(1) 30 endef 31 32 include build/common/Makefile.common.mk 33 34 ############################################################ 35 # clean section 36 ############################################################ 37 38 .PHONY: clean 39 clean: 40 -rm $(LOCAL_BIN)/* 41 -rm $(API_PLUGIN_PATH)/PolicyGenerator 42 -rm build_output/* 43 -rm PolicyGenerator 44 45 ############################################################ 46 # build section 47 ############################################################ 48 # Parse the version using git, with fallbacks as follows: 49 # - git describe (i.e. vX.Y.Z-<extra_commits>-<sha>) 50 # - <branch>-<sha> 51 # - <sha>-dev 52 # - Go BuildInfo version 53 # - Unversioned binary 54 GIT_VERSION := $(shell git describe --dirty 2>/dev/null) 55 ifndef GIT_VERSION 56 GIT_BRANCH := $(shell git branch --show-current) 57 GIT_SHA := $(shell git rev-parse --short HEAD) 58 ifdef GIT_BRANCH 59 GIT_VERSION := $(GIT_BRANCH)-$(GIT_SHA) 60 else ifdef GIT_SHA 61 GIT_VERSION := $(GIT_SHA)-dev 62 endif 63 endif 64 GO_LDFLAGS ?= -X 'main.Version=$(GIT_VERSION)' 65 66 .PHONY: build 67 build: layout 68 go build -ldflags="$(GO_LDFLAGS)" -o $(API_PLUGIN_PATH)/ ./cmd/PolicyGenerator 69 70 .PHONY: build-binary 71 build-binary: 72 go build -ldflags="$(GO_LDFLAGS)" ./cmd/PolicyGenerator 73 74 .PHONY: build-release 75 build-release: 76 @if [ $(shell git status --porcelain | wc -l) -gt 0 ]; then \ 77 echo "There are local modifications in the repo" > /dev/stderr; \ 78 exit 1; \ 79 fi 80 @mkdir -p build_output 81 GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -ldflags="$(GO_LDFLAGS)" -o build_output/linux-amd64-PolicyGenerator ./cmd/PolicyGenerator 82 GOOS=darwin CGO_ENABLED=0 GOARCH=amd64 go build -ldflags="$(GO_LDFLAGS)" -o build_output/darwin-amd64-PolicyGenerator ./cmd/PolicyGenerator 83 GOOS=windows CGO_ENABLED=0 GOARCH=amd64 go build -ldflags="$(GO_LDFLAGS)" -o build_output/windows-amd64-PolicyGenerator.exe ./cmd/PolicyGenerator 84 85 .PHONY: generate 86 generate: 87 @KUSTOMIZE_PLUGIN_HOME=$(KUSTOMIZE_PLUGIN_HOME) kustomize build --enable-alpha-plugins $(SOURCE_DIR) 88 89 .PHONY: layout 90 layout: 91 mkdir -p $(API_PLUGIN_PATH) 92 93 ############################################################ 94 # format section 95 ############################################################ 96 97 .PHONY: fmt 98 fmt: 99 go fmt ./... 100 101 ############################################################ 102 # lint section 103 ############################################################ 104 105 .PHONY: lint-dependencies 106 lint-dependencies: 107 $(call go-get-tool,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2) 108 109 .PHONY: lint 110 lint: lint-dependencies lint-all 111 112 ############################################################ 113 # test section 114 ############################################################ 115 GOSEC = $(LOCAL_BIN)/gosec 116 117 .PHONY: test 118 test: 119 @go test $(TESTARGS) ./... 120 121 .PHONY: test-coverage 122 test-coverage: TESTARGS = -json -cover -covermode=atomic -coverprofile=coverage.out 123 test-coverage: test 124 125 .PHONY: gosec 126 gosec: 127 $(call go-get-tool,github.com/securego/gosec/v2/cmd/gosec@v2.15.0) 128 129 .PHONY: gosec-scan 130 gosec-scan: gosec 131 $(GOSEC) -fmt sonarqube -out gosec.json -no-fail -exclude-dir=.go ./...