-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathadd_license_notice.sh
executable file
·67 lines (54 loc) · 1.86 KB
/
add_license_notice.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
LICENSE_NOTICE="// 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."
# Function to add license notice while preserving shebang and package declaration
add_license_notice() {
local file="$1"
local temp_file
local permissions
# Preserve file permissions
permissions=$(stat -c "%a" "$file")
# Create a temporary file
temp_file=$(mktemp)
# Read the first two lines to check for shebang and package declaration
read -r first_line < "$file"
read -r second_line < <(sed -n '2p' "$file")
if [[ "$first_line" =~ ^#! ]]; then
# If the first line is a shebang, preserve it
{
echo "$first_line"
echo ""
echo "$LICENSE_NOTICE"
echo ""
tail -n +2 "$file"
} > "$temp_file"
elif [[ "$first_line" =~ ^package ]]; then
# If the first line is a package declaration, insert the license above it
{
echo "$LICENSE_NOTICE"
echo ""
cat "$file"
} > "$temp_file"
else
# Just prepend the license notice
{
echo "$LICENSE_NOTICE"
echo ""
cat "$file"
} > "$temp_file"
fi
# Replace original file with modified one
mv "$temp_file" "$file"
# Restore file permissions
chmod "$permissions" "$file"
echo "Added license notice to $file"
}
export -f add_license_notice
export LICENSE_NOTICE
# Find all Go files that don't already contain the license
mapfile -t files < <(find . -type f -name "*.go" ! -path "./vendor/*" ! -exec grep -qF "$LICENSE_NOTICE" {} \; -print)
# Process files
for file in "${files[@]}"; do
add_license_notice "$file"
done
echo "Finished adding license notice to all Go files."