-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoctapp_helper
executable file
·177 lines (150 loc) · 3.74 KB
/
octapp_helper
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
# octapp_helper - Does miscellaneous things around Octave.app bundling
#
# Synopsis:
#
# octapp_helper <action> [...options...]
#
# octapp_helper clear-brew-logs
# octapp_helper grab-brew-logs [(-c|--comment) <comment>]
#
# octapp_helper <action> [-Y | --dry-run] [-v | --verbose] [...]
#
# Actions:
#
# clear-brew-logs - Clears your Homebrew logs by deleting everything under
# ~/Library/Logs/Homebrew. This blows away all your regular Homebrew logs, in addition
# to whatever was produced by octapp-specific activity!
#
# grab-brew-logs - Grab a copy of your Homebrew logs to a directory under
# build/logs/brew-capture. The `-c` comment option is a short string that is included
# in the subdirectory name in addition to a time stamp and host info. It is optional
# and can be omitted entirely.
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
# Function definitions
#
# (Scroll down to the very bottom to see the main script logic.)
# Utility functions
THIS_PROGRAM=$(basename $0)
function info() {
echo "$*"
}
function error() {
echo >&2 "${THIS_PROGRAM}: ERROR: $*"
}
function die() {
local msg="$1"
error "$msg"
exit 1
}
function verbose() {
if [[ $VERBOSE == 'y' ]]; then
echo "$*"
fi
}
function is_dry_run() {
if [[ $DRY_RUN == 'y' ]]; then
return 0
else
return 1
fi
}
function wet() {
if is_dry_run; then
echo "dry run: would run: $*"
else
verbose "running: $*"
"$@"
return $?
fi
}
# Octapp-specific functions
function usage() {
cat <<EOHELP
${THIS_PROGRAM} - Miscellaneous octapp helper actions
${THIS_PROGRAM} <action> [...options...]
${THIS_PROGRAM} clear-brew-logs
${THIS_PROGRAM} grab-brew-logs [(-c|--comment) <comment>]
Debugging options:
-h, -?, --help
Display this help text and exit.
EOHELP
}
function parse_cli_args() {
local arg
ACTION=''
REMAINDER_ARGS=()
COMMENT=''
DRY_RUN='n'
VERBOSE='n'
while [[ $# -ge 1 ]]; do
arg="$1"; shift
case "$arg" in
-h | --help| -\?)
ACTION='usage' ;;
-c | --comment)
if [[ $# -lt 1 ]]; then
die "option ${arg} requires an argument"
fi
COMMENT="$1"; shift ;;
-v | --verbose)
VERBOSE=y ;;
-Y | --dry-run)
DRY_RUN=y ;;
--)
REMAINDER_ARGS=("$@")
break ;;
*)
if [[ -z "$ACTION" ]]; then
ACTION="$arg"
else
die "Error: Invalid option: $arg. See ${THIS_PROGRAM} --help for help."
fi
;;
esac
done
}
function main () {
local exit_status
if [[ -z "$ACTION" ]]; then
die "the action argument is required. See ${THIS_PROGRAM} --help for help."
fi
case "$ACTION" in
usage)
usage; exit 0 ;;
clear-brew-logs)
clear_brew_logs ;;
grab-brew-logs)
grab_brew_logs ;;
*)
die "Invalid action: ${ACTION}. See ${THIS_PROGRAM} --help for help." ;;
esac
}
function clear_brew_logs() {
wet rm -rf "${HOME}"/Library/Logs/Homebrew/*
info "Cleared Homebrew logs"
}
function grab_brew_logs() {
local logs_dir subdir timestamp host arch
logs_dir="build/logs/brew-capture"
# Better to use the time of starting the last build instead of the current time,
# but that's not readily available.
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
host=$(hostname | tr '[:upper:]' '[:lower:]' | sed 's/\..*$//')
arch=$(uname -m)
if [[ -n "$COMMENT" ]]; then
subdir="${timestamp} - ${COMMENT} - ${host} ${arch}"
else
subdir="${timestamp} - ${host} ${arch}"
fi
grab_dir="${logs_dir}/${subdir}"
wet mkdir -p "$grab_dir"
wet cp -R "${HOME}"/Library/Logs/Homebrew/* "$grab_dir"
info "Captured brew logs to ${grab_dir}"
}
# Main top-level code
parse_cli_args "$@"
main