-
Notifications
You must be signed in to change notification settings - Fork 8
/
check_dotfiles_variable.sh
53 lines (44 loc) · 1.54 KB
/
check_dotfiles_variable.sh
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
#! /usr/bin/env bash
# get path to dotfiles repo root (should be same as $DOTFILES)
#
# Returns:
# {string} `_COMPUTED_DOTFILES` - path to this repo root
get_dotfiles_repo_root() {
local dir
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"
cd "$dir" || return
git rev-parse --show-toplevel
}
# checks if DOTFILES environment variable is set, sets it or quits based on user
# input if not
#
# Returns:
# {string} `DOTFILES` - exports DOTFILES environment variable if user chooses
# to set it
check_dotfiles_variable() {
local computed_dotfiles
computed_dotfiles="$(get_dotfiles_repo_root)"
# bail if $DOTFILES already set
[[ -z $DOTFILES ]] || return 0
# interactively confirm $DOTFILES path when not headless
if [[ -z $DOTFILES_SETUP_HEADLESS ]]; then
printf "This script requires a \$DOTFILES environment variable holding the \
path\nto the repo this script is running from. This path appears to be\n\
'%s'.\n\nIs this correct (y/any other key)? " "$computed_dotfiles"
# `-r` treats backslash as a literal, `-n` accepts one character of input
read -r -n 1 maybe_continue
if [[ $maybe_continue == 'y' ]]; then
export DOTFILES="$computed_dotfiles"
printf "\n\nDOTFILES set to '%s'. Note that this export exists\
\nonly within this script.\n" "$DOTFILES"
else
printf "\n\nTo continue, set DOTFILES \
(\`export DOTFILES=<path-to-dotfiles-repo>\`),\nthen rerun this script.\n\
Goodbye.\n"
exit
fi
else
export DOTFILES="$computed_dotfiles"
fi
}
check_dotfiles_variable "$@"