#!/bin/bash
# exec - Command execution support library
set -e
######
# The ``run`` Function
# run() - Encapsulates the running of commands in a way that allows
# them to be logged, simulated, and handled gracefully when not present.
#
# This function should be called whenever invoking an external program,
# calling functions that makes changes to disk, and any other location
# where such handling would be necessary to preserve its feature set.
#
# $1 - Name of command to run
# $@ - Command arguments
run() {
info "Running $@ ..."
if $pretend; then
return
fi
local cmd=$1
shift
if type "$cmd" >/dev/null 2>&1; then
$cmd "$@"
else
debug=true
error "$cmd: unknown command"
fi
}
######
# ``sudo`` Commands
# run_sudo() - Same as ``run()``, but run under ``sudo -H``
run_sudo() { run sudo -H -- "$@"; }
# run_sudo_as_user() - Same as ``run_sudo()``, but run as a given user ($1)
run_sudo_as_user() {
local user=$1
shift
run sudo -u $user -H -- "$@"
}
# run_sudo_install() - Same as ``run_sudo()``, but runs ``install`` on
# with the given mode ($1), owner ($2), and group ($3).
run_sudo_install() {
min_args 5 "$@"
local mode=$1
local owner=$2
local group=$3
shift 3
run_sudo install --mode $mode --owner $owner --group $group "$@"
}
# seed_sudo() - Updates ``sudo`` timestamp, making it unlikely that
# subsequent calls to ``sudo`` will require a password. This may also
# be called periodically to ensure the timestamp remains valid across
# long command invocations
seed_sudo() { sudo -s </dev/null; }
pipe_to_sudo() { sudo -s; }
# sudo_pipe_to_file() - Pipes stdin to a temporary file and then uses
# sudo to move it the named file ($1).
# $1 - Name of target file
sudo_pipe_to_file() {
has_args 1 "$@"
tmp=$(cmd_tempfile)
run cat >$tmp
run_sudo mv $tmp $1
}
######
# External Commands
# run_editor() - Runs $EDITOR with the provided arguments
run_editor() {
[ "$EDITOR" ] || error "\$EDITOR is not set"
run $EDITOR "$@"
}
# run_pager() - Runs $PAGER with the provided arguments
run_pager() {
[ "$PAGER" ] || error "\$PAGER is not set"
run clear
run $PAGER "$@"
}
Generated on Sat Jul 8 19:33:35 PDT 2017 by mcsh d14 v0.22.0.