Add .firestarterrc support, fix rotating logs in a dry run

This commit is contained in:
Salt 2019-06-21 18:49:03 -05:00
parent 488ff83b41
commit 28878bd911
2 changed files with 21 additions and 10 deletions

View File

@ -28,6 +28,8 @@ By setting a dummy execution line, one can effectively prevent an entire configu
command -v lemonbar command -v lemonbar
~/.bin/lemonbar.sh | lemonbar ~/.bin/lemonbar.sh | lemonbar
After all these programs have been started, the last thing firestarter does is execute `~/.firestarterrc`, if it exists.
# Logging # Logging
All STDOUT and STDERR messages from these commands are saved to a logfile in `~/.local/share/firestarter/logs` under the same name as the configuration file. By default, these logfiles are rotated every time you log in. All STDOUT and STDERR messages from these commands are saved to a logfile in `~/.local/share/firestarter/logs` under the same name as the configuration file. By default, these logfiles are rotated every time you log in.

View File

@ -11,6 +11,7 @@
_name="firestarter" _name="firestarter"
_configdir="${XDG_CONFIG_HOME:-$HOME/.config}/$_name" _configdir="${XDG_CONFIG_HOME:-$HOME/.config}/$_name"
_logdir="${XDG_DATA_HOME:-$HOME/.local/share}/$_name/logs" _logdir="${XDG_DATA_HOME:-$HOME/.local/share}/$_name/logs"
_firestarterrc="$HOME/.firestarterrc"
# Basic functions # Basic functions
print() { print() {
@ -276,7 +277,7 @@ https://gitlab.com/rehashedsalt/firestarter
EOF EOF
} }
step_check() { step_check() {
if [ -z "$dryrun" ]; then if [ -z "$_dryrun" ]; then
for pid in $(pgrep firestarter); do for pid in $(pgrep firestarter); do
if [ "$pid" != "$BASHPID" ]; then if [ "$pid" != "$BASHPID" ]; then
err "Firestarter is already running" 61 err "Firestarter is already running" 61
@ -287,7 +288,7 @@ step_check() {
} }
step_preexecute() { step_preexecute() {
# Special things that can't use simple configuration files # Special things that can't use simple configuration files
[ "$dryrun" = "1" ] && return 0 [ "$_dryrun" = "1" ] && return 0
# dbus # dbus
if \ if \
@ -359,11 +360,13 @@ step_preexecute() {
fi fi
} }
step_execute() { step_execute() {
# Ensure we have a config directory
if ! [ -d "$_configdir" ]; then if ! [ -d "$_configdir" ]; then
if ! mkdir -p "$_configdir" > /dev/null 2>&1; then if ! mkdir -p "$_configdir" > /dev/null 2>&1; then
err "Failed to create configuration directory \"$_configdir\"" 52 err "Failed to create configuration directory \"$_configdir\"" 52
fi fi
fi fi
# Ensure we can log if we have to
if [ -n "$FS_NOLOG" ]; then if [ -n "$FS_NOLOG" ]; then
log "No logs will be created per FS_NOLOG" log "No logs will be created per FS_NOLOG"
elif ! [ -d "$_logdir" ]; then elif ! [ -d "$_logdir" ]; then
@ -371,6 +374,7 @@ step_execute() {
err "Failed to create log directory \"$_logdir\"" 53 err "Failed to create log directory \"$_logdir\"" 53
fi fi
fi fi
# Parse configs
for file in "$_configdir"/*; do for file in "$_configdir"/*; do
if ! [ -f "$file" ]; then if ! [ -f "$file" ]; then
log "No configuration files found; generating defaults" log "No configuration files found; generating defaults"
@ -387,11 +391,11 @@ step_execute() {
read -r execline read -r execline
if bash -c "$checkline" > /dev/null 2>&1; then if bash -c "$checkline" > /dev/null 2>&1; then
log "Found target for \"$filename\": \"$execline\"" log "Found target for \"$filename\": \"$execline\""
if ! [ "$_dryrun" = "1" ]; then
if [ -f "$logfile" ]; then if [ -f "$logfile" ]; then
[ -f "$logfile.old" ] && rm "$logfile.old" [ -f "$logfile.old" ] && rm "$logfile.old"
mv "$logfile" "$logfile.old" mv "$logfile" "$logfile.old"
fi fi
if ! [ "$dryrun" = "1" ]; then
bash -c "$execline" > "$logfile" 2>&1 & bash -c "$execline" > "$logfile" 2>&1 &
fi fi
break break
@ -400,9 +404,14 @@ step_execute() {
fi fi
done < "$file" done < "$file"
done done
# And then source in a user script if it exists
if [ -r "$_firestarterrc" ] && [ -z "$_dryrun" ]; then
log "Sourcing .firestarterrc"
"$_firestarterrc"
fi
} }
step_wait() { step_wait() {
[ "$dryrun" = "1" ] && exit 0 [ "$_dryrun" = "1" ] && exit 0
log "Waiting for programs to exit" log "Waiting for programs to exit"
log "Send any termination signal to firestarter to log out" log "Send any termination signal to firestarter to log out"
trap step_logout EXIT trap step_logout EXIT
@ -429,9 +438,9 @@ main() {
while getopts ":dgh" opt; do while getopts ":dgh" opt; do
case $opt in case $opt in
d) d)
if ! [ "$dryrun" = "1" ]; then if ! [ "$_dryrun" = "1" ]; then
log "Performing a dry run" log "Performing a dry run"
dryrun=1 _dryrun=1
fi fi
;; ;;
g) g)