...
1#!/usr/bin/env bash
2
3year=$(date +"%Y")
4copyright=$"// Copyright (C) MongoDB, Inc. $year-present.
5//
6// Licensed under the Apache License, Version 2.0 (the \"License\"); you may
7// not use this file except in compliance with the License. You may obtain
8// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
9"
10
11add_copyright() {
12 file=$1
13
14 # Check if first 24 bytes match first 24 bytes of copyright notice.
15 local line=$(head -c 24 $file)
16 if [ "$line" == "// Copyright (C) MongoDB" ]; then
17 if [ ! -z "$verbose" ]; then
18 echo "$file already has copyright notice" >&2
19 fi
20 return
21 fi
22
23 # Check if first 14 bytes matches the prefix "// Copied from"
24 local line=$(head -c 14 $file)
25 if [ "$line" == "// Copied from" ]; then
26 if [ ! -z "$verbose" ]; then
27 echo "$file has a third-party copyright notice" >&2
28 fi
29 return
30 fi
31
32 if [ ! -z "$add" ]; then
33 echo "$copyright" | cat - $file > temp && mv temp $file
34 return
35 fi
36
37 echo "Missing copyright notice in \"$file\". Run \"make add-license\" to add missing licenses."
38 exit 1
39}
40
41# Options are:
42# -a : Add licenses that are missing.
43# -v : Verbose. Print all files as they're checked.
44while getopts at:vt: flag
45do
46 case "${flag}" in
47 a) add=1;;
48 v) verbose=1;;
49 esac
50done
51
52# Find all .go files not in the vendor directory and try to write a license notice.
53GO_FILES=$(find . -path ./vendor -prune -o -type f -name "*.go" -print)
54
55for file in $GO_FILES
56do
57 add_copyright "$file"
58done
View as plain text