add_license_notice.sh
add_license_notice.sh - Overview
This script automates the process of adding a standard license notice to the beginning of Go files within a project, while preserving any existing shebang lines or package declarations. It searches for .go
files (excluding those in the vendor
directory) that do not already contain the specified license notice and prepends the notice to those files.
Detailed Documentation
LICENSE_NOTICE
- Purpose: Defines the license notice text to be added to the files.
- Type: String
- Value:
// Copyright (c) 2022-present, DiceDB contributors
// All rights reserved. Licensed under the BSD 3-Clause License. See LICENSE file in the project root for full license information.
add_license_notice()
- Purpose: Adds the license notice to a single specified file, handling cases where the file starts with a shebang (
#!
) or a package declaration. - Parameters:
file
(String): The path to the file to which the license notice should be added.
- Returns: None.
Implementation Details of add_license_notice()
- Preserve File Permissions:
- It first retrieves the file's permissions using
stat -c "%a" "$file"
and stores them in thepermissions
variable.
- It first retrieves the file's permissions using
- Create Temporary File:
- A temporary file is created using
mktemp
to hold the modified content.
- A temporary file is created using
- Check for Shebang or Package Declaration:
- The script reads the first two lines of the file.
- If the first line starts with
#!
(shebang), it writes the shebang, a blank line, the$LICENSE_NOTICE
, another blank line, and the rest of the file (from the third line onwards) to the temporary file. - If the first line starts with
package
, it writes the$LICENSE_NOTICE
, a blank line, and then the entire original file to the temporary file. - If neither a shebang nor a package declaration is detected, it prepends the
$LICENSE_NOTICE
and a blank line to the entire file content and writes it to the temporary file.
- Replace Original File:
- The original file is replaced with the content of the temporary file using
mv "$temp_file" "$file"
.
- The original file is replaced with the content of the temporary file using
- Restore File Permissions:
- The original file permissions are restored using
chmod "$permissions" "$file"
.
- The original file permissions are restored using
- Confirmation Message:
- The script prints a message to the console:
"Added license notice to $file"
.
- The script prints a message to the console:
Script Logic
- Locate Go Files:
- Uses
find
to locate all.go
files, excluding those in thevendor
directory and those that already contain the license notice. The results are stored in thefiles
array.
find . -type f -name "*.go" ! -path "./vendor/*" ! -exec grep -qF "$LICENSE_NOTICE" {} \; -print
- Uses
- Iterate and Apply:
- Loops through each file in the
files
array and calls theadd_license_notice
function to add the license notice.
for file in "${files[@]}"; do
add_license_notice "$file"
done - Loops through each file in the
- Completion Message:
- Prints a completion message:
"Finished adding license notice to all Go files."
.
- Prints a completion message:
Code Examples
N/A
Getting Started Relevance
YES