From 2a5ff48e3d0342c1978adf97c63414fa06c8b9ed Mon Sep 17 00:00:00 2001 From: CJ Horton Date: Wed, 16 Aug 2023 12:49:09 -0700 Subject: [PATCH] run copyright header check recursively We have a few directories in this repo that will be remaining MPL licensed (the provider protocol definitions). This is something that the copywrite tool does not support out of the box - we need to run the tool for each directory that contains a .copywrite.hcl file in order to make sure that new files have the correct header. To avoid adding yet more boilerplate with a `copyright_headers.go` file in each package, let's move the copyright check to its own make target and script the process of discovering all directories with copywrite config. --- .github/workflows/checks.yml | 4 ++-- Makefile | 3 +++ copyright_headers.go | 13 ------------- scripts/copyright.sh | 17 +++++++++++++++++ tools.go | 1 + 5 files changed, 23 insertions(+), 15 deletions(-) delete mode 100644 copyright_headers.go create mode 100755 scripts/copyright.sh diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index b5e7b7632c15..196763cb8942 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -182,9 +182,9 @@ jobs: - name: "Code consistency checks" run: | - make fmtcheck importscheck generate staticcheck exhaustive protobuf + make fmtcheck importscheck generate staticcheck exhaustive protobuf copyright if [[ -n "$(git status --porcelain)" ]]; then - echo >&2 "ERROR: Generated files are inconsistent. Run 'make generate' and 'make protobuf' locally and then commit the updated files." + echo >&2 "ERROR: Generated files are inconsistent. Run 'make generate protobuf copyright' locally and then commit the updated files." git >&2 status --porcelain exit 1 fi diff --git a/Makefile b/Makefile index 84a5dfabf52e..f2e17a119371 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,9 @@ staticcheck: exhaustive: "$(CURDIR)/scripts/exhaustive.sh" +copyright: + "$(CURDIR)/scripts/copyright.sh" + # Run this if working on the website locally to run in watch mode. website: $(MAKE) -C website website diff --git a/copyright_headers.go b/copyright_headers.go deleted file mode 100644 index f0d87a108003..000000000000 --- a/copyright_headers.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -//go:build tools -// +build tools - -package main - -import ( - _ "github.com/hashicorp/copywrite" -) - -//go:generate go run github.com/hashicorp/copywrite headers diff --git a/scripts/copyright.sh b/scripts/copyright.sh new file mode 100755 index 000000000000..8ce102a89fef --- /dev/null +++ b/scripts/copyright.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: BUSL-1.1 + +# This script checks that all files have the appropriate copyright headers, +# according to their nearest .copywrite.hcl config file. The copyright tool +# does not natively support repos with multiple licenses, so we have to +# script this ourselves. + +set -euo pipefail + +# Find all directories containing a .copywrite.hcl config file +directories=$(find . -type f -name '.copywrite.hcl' -execdir pwd \;) + +for dir in $directories; do + cd $dir && go run github.com/hashicorp/copywrite headers +done \ No newline at end of file diff --git a/tools.go b/tools.go index 6a3eeb8389c6..702c7c8937ee 100644 --- a/tools.go +++ b/tools.go @@ -11,6 +11,7 @@ package tools // Go toolchain to see that we need to include them in go.mod and go.sum. import ( + _ "github.com/hashicorp/copywrite" _ "github.com/nishanths/exhaustive/cmd/exhaustive" _ "golang.org/x/tools/cmd/stringer" _ "honnef.co/go/tools/cmd/staticcheck"