|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2019 The Knative Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +# shellcheck disable=SC1090 |
| 22 | +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script codegen-library.sh)" |
| 23 | + |
| 24 | +USAGE=$(cat <<EOF |
| 25 | +Add boilerplate.<ext>.txt to all .<ext> files missing it in a directory. |
| 26 | +
|
| 27 | +Usage: (from repository root) |
| 28 | + ./hack/boilerplate/add-boilerplate.sh <ext> <DIR> |
| 29 | +
|
| 30 | +Example: (from repository root) |
| 31 | + ./hack/boilerplate/add-boilerplate.sh go cmd |
| 32 | +
|
| 33 | +As of now, only .go files are supported. |
| 34 | +EOF |
| 35 | +) |
| 36 | + |
| 37 | +if [ -z "${1:-}" ] || [ -z "${2:-}" ]; then |
| 38 | + error Invalid arguments |
| 39 | + echo "${USAGE}" 1>&2 |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +if ! [[ "$1" = "go" ]]; then |
| 44 | + error Unsupported file extension |
| 45 | + echo "${USAGE}" 1>&2 |
| 46 | + exit 2 |
| 47 | +fi |
| 48 | + |
| 49 | +cnt=0 |
| 50 | +while read -r file; do |
| 51 | + if grep -q -E "^Copyright [[:digit:]]+ The Knative Authors$" "$file"; then |
| 52 | + continue |
| 53 | + fi |
| 54 | + cat "$(boilerplate)" > "$file".bck |
| 55 | + echo '' >> "$file".bck |
| 56 | + cat "$file" >> "$file".bck |
| 57 | + mv "$file".bck "$file" |
| 58 | + log License added to "$file" |
| 59 | + cnt=$(( cnt + 1)) |
| 60 | +done < <(find "$2" -type f -name "*.$1") |
| 61 | + |
| 62 | +log License added to $cnt files |
0 commit comments