helm/.common.sh
2015-12-23 02:19:03 -05:00

31 lines
686 B
Bash

#!/bin/bash
DOTFILES="$HOME/.dotfiles"
CACHE_DIR="$HOME/.cache"
is-callable() { command -v "$1" >/dev/null; }
is-interactive() { [[ $- == *i* ]]; }
is-root() { [[ "$UID" -eq 0 ]]; }
is-ssh() { [[ "$SSH_CONNECTION" != '' ]]; }
load() { source "$DOTFILES/$1"; }
iload() { is-interactive && load "$1"; }
cache() {
if [[ -z "$CACHE_DIR" ]]; then
>&2 echo "Cache not set!"
return 1
fi
[[ ! -d "$CACHE_DIR" ]] && mkdir -p "$CACHE_DIR"
is-callable "$1" || return 1
local cache="${CACHE_DIR}/$1-$(basename $SHELL)"
if ! [[ -f "$cache" && -s "$cache" ]]; then
echo "Caching $1"
$@ > "$cache" 2> /dev/null
fi
source $cache
}