-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·131 lines (113 loc) · 3.01 KB
/
run
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
set -m
function print_help {
echo -e "Usage: $0 [operation]\n"
echo -e "Supported operations:"
echo -e "\tupdate\t\t\tRecursively searches for and runs all update.sh scripts."
echo -e "\trestore\t\t\tRecursively searches for and runs all restore.sh scripts."
echo -e "\t-i | --interactive\tInteractive mode (ask before removing logs)."
echo -e "\t-h | --help\t\tPrint this help message."
}
if [[ -z "$1" ]]; then
echo "Error: No operation provided."
print_help
exit 1
fi
for i in "$@"; do
case $i in
-h|--help)
print_help
exit 0
shift
;;
-i|--interactive)
interactive="true"
shift
;;
update)
script_name="update.sh"
shift
;;
restore)
script_name="restore.sh"
shift
;;
*)
echo -e "Error: Unknown operation provided.\n"
print_help
exit 1
;;
esac
done
# escape spaces in $PWD path
pwd=$(pwd | sed -e 's/\ /\\\ /g')
if [[ $script_name == "update.sh" ]]; then
config_helper="$pwd/.helpers/update-if-exists.sh"
elif [[ $script_name == "restore.sh" ]]; then
config_helper="$pwd/.helpers/restore-if-exists.sh"
fi
config_files_name="config_files.sh"
function exec_with_config_files {
# cd into directory where config_files.sh is located
local oldpwd="$PWD"
cd "$(dirname $1)"
# load config files
source "./$config_files_name"
# execute copy files helper for each file
for file in ${config_files[@]}; do
eval "$config_helper" "$file"
done
# cleanup imported config_files
unset config_files
# cd back to where function started
cd "$oldpwd"
}
function exec_script {
local oldpwd="$PWD"
cd "$(dirname $1)"
eval "./$script_name"
cd "$oldpwd"
}
# $1: glob for searched file
# $2: function to call with found file as argument
function find_and_exec {
local file_glob="$1"
local callback=$2
find . -name "$file_glob" | while read file; do
"$callback" "$file"
done
}
# Recursively search for update.sh/restore.sh and exec scripts.
find_and_exec "$script_name" exec_script
# Recursively search for config_files.sh and exec scripts.
find_and_exec "$config_files_name" exec_with_config_files
echo -e "\n"
# -----------------------------------------------------------------------------
# Post update/restore
# -----------------------------------------------------------------------------
# If update, add an auto-update commit
if [[ "$script_name" == "update.sh" ]]; then
git add -A && git commit -m "auto-update on $(date "+%Y/%m/%d %H:%M:%S")" &> /dev/null && echo "Generated auto-update commit." || echo "Nothing to update."
fi
function remove_logs {
rm -f ./**/*.log
}
# If restore, ask to remove all .log files
if [[ "$script_name" == "restore.sh" ]]; then
if [[ "$interactive" != "true" ]]; then
remove_logs
else
while true; do
read -p "Remove all .log files? (y/n) " yn
case $yn in
[Yy]* )
remove_logs
break
;;
[Nn]* )
break
;;
esac
done
fi
fi