Skip to content

Commit

Permalink
refactor: license header gen/check
Browse files Browse the repository at this point in the history
* generate license header files usign `gotemplate`
* use `make license-header-check` to check *.go and *.sh files
* add license header check to CI pipeline
  • Loading branch information
chrisgacsal committed Mar 11, 2023
1 parent b943eef commit 6b45c08
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: make license-check

- name: Check license header
run: make license-header-check

- name: Build
run: |
make generate
Expand Down
41 changes: 29 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SHELL = /bin/bash

PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
BIN_DIR := $(PROJECT_DIR)/bin
BOILERPLATE_DIR := $(PROJECT_DIR)/hack/boilerplate

# Image URL to use all building/pushing image targets
TAG ?= $(shell git describe --tags --abbrev=0 --match 'v[0-9].*[0-9].*[0-9]' 2>/dev/null )
Expand Down Expand Up @@ -143,8 +144,8 @@ vet:
cd properties && go vet ./...

# Generate code
generate: bin/controller-gen
cd api && $(CONTROLLER_GEN) object:headerFile=./../hack/boilerplate.go.txt paths="./..."
generate: bin/controller-gen gen-license-header ## Generate source code for APIs, Mocks, etc
cd api && $(CONTROLLER_GEN) object:headerFile=$(BOILERPLATE_DIR)/header.go.generated.txt paths="./..."

# Build the docker image
docker-build:
Expand Down Expand Up @@ -204,23 +205,39 @@ $(BIN_DIR)/addlicense-$(ADDLICENSE_VERSION):
@GOBIN=$(BIN_DIR) go install github.com/google/addlicense@v$(ADDLICENSE_VERSION)
@mv $(BIN_DIR)/addlicense $(BIN_DIR)/addlicense-$(ADDLICENSE_VERSION)

ADDLICENSE_COPYRIGHT_HOLDER := Cisco Systems, Inc. and/or its affiliates
ADDLICENSE_SOURCE_DIRS := api controllers internal pkg properties
ADDLICENSE_SOURCE_DIRS := api controllers internal pkg properties scripts
ADDLICENSE_OPTS_IGNORE := -ignore '**/*.yml' -ignore '**/*.yaml' -ignore '**/*.xml'

.PHONY: license-header-check
license-header-check: bin/addlicense ## Find missing license header in source code files
@bin/addlicense \
-c "$(ADDLICENSE_COPYRIGHT_HOLDER)" \
license-header-check: gen-license-header bin/addlicense ## Find missing license header in source code files
bin/addlicense \
-check \
-s \
-f $(BOILERPLATE_DIR)/header.generated.txt \
$(ADDLICENSE_OPTS_IGNORE) \
$(ADDLICENSE_SOURCE_DIRS)

.PHONY: license-header-fix
license-header-fix: bin/addlicense ## Fix missing license header in source code files
@bin/addlicense \
-c "$(ADDLICENSE_COPYRIGHT_HOLDER)" \
-s \
license-header-fix: gen-license-header bin/addlicense ## Fix missing license header in source code files
bin/addlicense \
-f $(BOILERPLATE_DIR)/header.generated.txt \
$(ADDLICENSE_OPTS_IGNORE) \
$(ADDLICENSE_SOURCE_DIRS)

GOTEMPLATE_VERSION := 3.7.3

bin/gotemplate: $(BIN_DIR)/gotemplate-$(GOTEMPLATE_VERSION)
@ln -sf gotemplate-$(GOTEMPLATE_VERSION) $(BIN_DIR)/gotemplate

$(BIN_DIR)/gotemplate-$(GOTEMPLATE_VERSION):
@mkdir -p $(BIN_DIR)
@GOBIN=$(BIN_DIR) go install github.com/coveooss/gotemplate/v3@v$(GOTEMPLATE_VERSION)
@mv $(BIN_DIR)/gotemplate $(BIN_DIR)/gotemplate-$(GOTEMPLATE_VERSION)

.PHONY: gen-license-header
gen-license-header: bin/gotemplate ## Generate license header used in source code files
cd $(BOILERPLATE_DIR) && \
$(BIN_DIR)/gotemplate run \
--follow-symlinks \
--template-log-level=debug \
--internal-log-level=debug \
--import="vars.yml"
13 changes: 0 additions & 13 deletions hack/boilerplate.go.txt

This file was deleted.

13 changes: 13 additions & 0 deletions hack/boilerplate/header.generated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2023 Cisco Systems, Inc. and/or its affiliates

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
15 changes: 15 additions & 0 deletions hack/boilerplate/header.go.generated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright 2023 Cisco Systems, Inc. and/or its affiliates

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
10 changes: 10 additions & 0 deletions hack/boilerplate/header.go.txt.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Copyright {{ date "2006" now }} {{ .copyright.owner }}

{{ .license.content }}
@-if(.spdx.enabled)
SPDX-FileCopyrightText: {{ date "2006" now }} {{ .copyright.owner }} <{{ .copyright.contact }}>

SPDX-License-Identifier: {{ .license.id }}
@-end
*/
8 changes: 8 additions & 0 deletions hack/boilerplate/header.txt.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright {{ date "2006" now }} {{ .copyright.owner }}

{{ .license.content }}
@-if(.spdx.enabled)
SPDX-FileCopyrightText: {{ date "2006" now }} {{ .copyright.owner }} <{{ .copyright.contact }}>

SPDX-License-Identifier: {{ .license.id }}
@-end
20 changes: 20 additions & 0 deletions hack/boilerplate/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
license:
id: Apache-2.0
content: |-
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
copyright:
owner: Cisco Systems, Inc. and/or its affiliates
contact: https://cisco.com
spdx:
# enable/disable including SPDX fields in license header
enabled: false
14 changes: 14 additions & 0 deletions scripts/image_patch.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#!/usr/bin/env bash
# Copyright 2023 Cisco Systems, Inc. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


: ${1?"Usage: $0 PATCH_FILE IMG_NAME"}
: ${2?"Usage: $0 PATCH_FILE IMG_NAME"}
Expand Down
14 changes: 14 additions & 0 deletions scripts/increment_version.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#!/bin/bash
# Copyright 2023 Cisco Systems, Inc. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


while getopts ":Mmp" opt; do
case $opt in
Expand Down
14 changes: 14 additions & 0 deletions scripts/install_kustomize.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#!/usr/bin/env bash
# Copyright 2023 Cisco Systems, Inc. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


version=3.1.0
opsys=$(echo "$(uname -s)" | awk '{print tolower($0)}')
Expand Down

0 comments on commit 6b45c08

Please sign in to comment.