Quick tip: Add licence header to all code files
It is often desired in open source code to add a licence header to each code file. Most of the time, authors would do this once and often forget to add the header on new files. We can use a tool called addlicense
to help manage adding a license header to source code with ease.
Installation
addlicense is a static binary that can be installed from source or run as a container using a tool like docker.
go install github.com/google/addlicense@v1.0.0
# Ensure that $GOBIN is added on the PATH to access the binary called addlicense
Or pull the docker image
docker pull ghcr.io/google/addlicense:v1.0.0
Usage
To add a license header to all files in the current directory and all child directories, run
addlicense -c '<YOUR_NAME/COMPANY_NAME>' -l <licence_type> ./**/*.<file_extension>
If you are using docker image instead of the binary, run
docker run -it ghcr.io/google/addlicense:v1.0.0 -v ${PWD}:/src '<YOUR_NAME/COMPANY_NAME>' -l <licence_type> ./**/*.<file_extension>
Example
Add Apache licence header to all Go, Python and Javascript files in the current directory tree.
addlicense -c 'Harsimran Singh Maan' -l apache ./**/*.go ./**/*.py ./**/*.js
Result
You shall see all Go, Python and Javascript file include a header like shown below at the top of each code file.
Integrate with CI
You can integrate with your CI system, by adding a check for the missing licence headers. It prints the name of the files missing the licence headers and exits with a non-zero status code if the licence header is missing in a file.
addlicense -check -l apache ./**/*.go ./**/*.py ./**/*.js
Source Code: https://github.com/harsimranmaan/medium.com/tree/main/quick-tips/addlicense