-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-template.sh
executable file
·109 lines (89 loc) · 2.68 KB
/
init-template.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup_failure SIGINT SIGTERM ERR
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
TEMPLATE_AUTHOR_NAME="netr0m"
TEMPLATE_REPO_NAME="ansible-role-template"
TEMPLATE_ROLE_NAME="template"
TEMPLATE_FILES_WITH_REFS=(
"meta/main.yml" "molecule/default/converge.yml"
"LICENSE" "README.md" "example_playbook.yml"
".github/workflows/ci.yml" ".github/workflows/lint.yml"
".github/workflows/molecule.yml"
)
ROLE_AUTHOR=""
REPO_NAME=""
ROLE_NAME=""
__TEMPLATE_ORIGIN="[email protected]:$TEMPLATE_AUTHOR_NAME/$TEMPLATE_REPO_NAME.git"
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-r <repository_name>] -a <author> -n <role_name>
Initialize the template, replacing all references to this role's author with the specified author,
and the references to this role's name with the specified role name.
-h, --help Print this help and exit
-r, --repo The name of the repository. Defaults to the value of '--name' if absent.
-a, --author The username of the author of the new role (you?)
-n, --name The name of the new role
EOF
exit
}
cleanup_failure() {
trap - SIGINT SIGTERM ERR
failed_at=$(date +%d-%m-%Y-%H:%M:%S)
git stash save "failed/${failed_at}"
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1}
msg "$msg"
exit "$code"
}
parse_params() {
while :; do
case "${1-}" in
-h | --help) usage ;;
-a | --author)
ROLE_AUTHOR="${2-}"
shift
;;
-n | --name)
ROLE_NAME="${2-}"
shift
;;
-r | --repo)
REPO_NAME="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
[[ -z "${ROLE_AUTHOR-}" ]] && die "Missing required parameter '--author'"
[[ -z "${ROLE_NAME-}" ]] && die "Missing required parameter '--name'"
[[ -z "${REPO_NAME-}" ]] && REPO_NAME="$ROLE_NAME"
return 0
}
parse_params "$@"
set_upstream() {
msg "Adding '$__TEMPLATE_ORIGIN' as upstream remote"
git remote add upstream "$__TEMPLATE_ORIGIN"
}
replace_refs() {
for filepath in "${TEMPLATE_FILES_WITH_REFS[@]}"; do
msg "Replacing references in $filepath.."
sed -i "s/$TEMPLATE_AUTHOR_NAME/$ROLE_AUTHOR/g" "$SCRIPT_DIR/$filepath"
sed -i "s/$TEMPLATE_REPO_NAME/$REPO_NAME/g" "$SCRIPT_DIR/$filepath"
sed -i "s/$TEMPLATE_ROLE_NAME/$ROLE_NAME/g" "$SCRIPT_DIR/$filepath"
done
msg "Replacing title in README.md"
sed -i "s/$TEMPLATE_ROLE_NAME/$ROLE_NAME/ig" "$SCRIPT_DIR/README.md"
}
main() {
set_upstream
replace_refs
}
main