plugins/osx-timemachine: `shellcheck` && `shfmt`

Use bash functionality rather than external binaries. Alsö, if $SUDO_ASKPASS is set then pass -A to sudo.
pull/2008/head
John D Pell 2021-07-26 02:02:01 -07:00
parent caa0d48c60
commit 03653fc141
2 changed files with 63 additions and 52 deletions

View File

@ -103,6 +103,7 @@ plugins/available/jump.plugin.bash
plugins/available/less-pretty-cat.plugin.bash plugins/available/less-pretty-cat.plugin.bash
plugins/available/node.plugin.bash plugins/available/node.plugin.bash
plugins/available/nodenv.plugin.bash plugins/available/nodenv.plugin.bash
plugins/available/osx-timemachine.plugin.bash
plugins/available/osx.plugin.bash plugins/available/osx.plugin.bash
plugins/available/percol.plugin.bash plugins/available/percol.plugin.bash
plugins/available/plenv.plugin.bash plugins/available/plenv.plugin.bash

View File

@ -1,20 +1,26 @@
cite about-plugin # shellcheck shell=bash
about-plugin 'OS X Time Machine functions' about-plugin 'OS X Time Machine functions'
if [[ "${OSTYPE}" != 'darwin'* ]]; then
_log_warning "This plugin only works with Mac OS X"
return 1
fi
function time-machine-destination() { function time-machine-destination() {
group "osx-timemachine" group "osx-timemachine"
about "Shows the OS X Time Machine destination/mount point" about "Shows the OS X Time Machine destination/mount point"
echo $(tmutil destinationinfo | grep "Mount Point" | sed -e 's/Mount Point : \(.*\)/\1/g') tmutil destinationinfo | grep "Mount Point" | sed -e 's/Mount Point : \(.*\)/\1/g'
} }
function time-machine-list-machines() { function time-machine-list-machines() {
group "osx-timemachine" group "osx-timemachine"
about "Lists the OS X Time Machine machines on the backup volume" about "Lists the OS X Time Machine machines on the backup volume"
local tmdest="$(time-machine-destination)/Backups.backupdb" local tmdest
tmdest="$(time-machine-destination)/Backups.backupdb"
find "$tmdest" -maxdepth 1 -mindepth 1 -type d | grep -v "/\." | while read line ; do find "$tmdest" -maxdepth 1 -mindepth 1 -type d | grep -v "/\." | while read -r line; do
echo "${line##*/}" echo "${line##*/}"
done done
} }
@ -26,10 +32,11 @@ function time-machine-list-all-backups() {
example "time-machine-list-all-backups my-laptop" example "time-machine-list-all-backups my-laptop"
# Use the local hostname if none provided # Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} local COMPUTERNAME BACKUP_LOCATION
local BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME" COMPUTERNAME=${1:-$(scutil --get ComputerName)}
BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"
find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d | while read line ; do find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d | while read -r line; do
echo "$line" echo "$line"
done done
} }
@ -41,19 +48,22 @@ function time-machine-list-old-backups() {
example "time-machine-list-old-backups my-laptop" example "time-machine-list-old-backups my-laptop"
# Use the local hostname if none provided # Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} local COMPUTERNAME BACKUP_LOCATION
local BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME" COMPUTERNAME=${1:-$(scutil --get ComputerName)}
BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"
# List all but the most recent one # List all but the most recent one
find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d -name 2\* | sed \$d | while read line ; do find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d -name 2\* | sed \$d | while read -r line; do
echo "$line" echo "$line"
done done
} }
# Taken from here: http://stackoverflow.com/a/30547074/1228454 # Taken from here: http://stackoverflow.com/a/30547074/1228454
function _tm_startsudo() { function _tm_startsudo() {
sudo -v sudo -v # validate without running a command.
( while true; do sudo -v; sleep 50; done; ) & (while sudo "-${SUDO_ASKPASS:+A}v"; do
sleep 50
done) &
SUDO_PID="$!" SUDO_PID="$!"
trap _tm_stopsudo SIGINT SIGTERM trap _tm_stopsudo SIGINT SIGTERM
} }
@ -70,15 +80,15 @@ function time-machine-delete-old-backups() {
example "time-machine-delete-old-backups my-laptop" example "time-machine-delete-old-backups my-laptop"
# Use the local hostname if none provided # Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} local COMPUTERNAME=${1:-$(scutil --get ComputerName)} _old_backup
# Ask for sudo credentials only once # Ask for sudo credentials only once
_tm_startsudo _tm_startsudo
echo "$(time-machine-list-old-backups "$COMPUTERNAME")" | while read i ; do while read -r _old_backup; do
# Delete the backup # Delete the backup
sudo tmutil delete "$i" sudo tmutil delete "$_old_backup"
done done <<< "$(time-machine-list-old-backups "$COMPUTERNAME")"
_tm_stopsudo _tm_stopsudo
} }