Skip to main content

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()

  1. Preserve File Permissions:
    • It first retrieves the file's permissions using stat -c "%a" "$file" and stores them in the permissions variable.
  2. Create Temporary File:
    • A temporary file is created using mktemp to hold the modified content.
  3. 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.
  4. Replace Original File:
    • The original file is replaced with the content of the temporary file using mv "$temp_file" "$file".
  5. Restore File Permissions:
    • The original file permissions are restored using chmod "$permissions" "$file".
  6. Confirmation Message:
    • The script prints a message to the console: "Added license notice to $file".

Script Logic

  1. Locate Go Files:
    • Uses find to locate all .go files, excluding those in the vendor directory and those that already contain the license notice. The results are stored in the files array.
    find . -type f -name "*.go" ! -path "./vendor/*" ! -exec grep -qF "$LICENSE_NOTICE" {} \; -print
  2. Iterate and Apply:
    • Loops through each file in the files array and calls the add_license_notice function to add the license notice.
    for file in "${files[@]}"; do
    add_license_notice "$file"
    done
  3. Completion Message:
    • Prints a completion message: "Finished adding license notice to all Go files.".

Code Examples

N/A

Getting Started Relevance

YES