-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
72 lines (61 loc) · 1.59 KB
/
utils.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# Collection of utility functions
# ANSI COLOR CODES. Use bold ones for better readability
# There's more:
# https://gist.github.com/vratiu/9780109
RED='\033[1;31m'
NORMAL='\033[0m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
# Redirect the received string to stderr and exit 1
function exitWithError {
printf "${RED}### ERROR: ### $1\n${NORMAL}" 1>&2;
exit 1;
}
# Requires the calling script to be run with root privileges.
# Sets the $USER env variable to the calling user.
function requireRoot {
if [ "$EUID" -ne 0 ]; then
exitWithError "Please run this script with root privileges."
fi
if [ ! "$SUDO_USER" = '' ]; then
USER=$SUDO_USER
HOME=$(getHomeDirectoryForUser $USER)
fi
}
# Make sure a given user exists on the host.
function ensureUserExists {
if [ -z $1 ]; then
exitWithError "ensureUserExists must be called with a user name."
fi
if [ ! $(id -u $1 2>/dev/null) ]; then
exitWithError "Invalid user '$1' provided."
fi
}
# Return the home directory of a given user,
# or the current user if none is provided.
function getHomeDirectoryForUser {
if [ -z $1 ]; then
user=$USER
else
user=$1
fi
ensureUserExists $user
echo $( getent passwd "$user" | cut -d: -f6 )
}
# Our dotfiles need to be at ~/.dotfiles
function ensureDotfilesExist {
dotfileDirectory=${HOME}/.dotfiles
if [ ! -d ${dotfileDirectory} ]; then
git clone https://github.com/stefanpl/dotfiles ${dotfileDirectory}
fi
}
function logSuccess {
printf "${GREEN}$1\n${NORMAL}"
}
function logError {
printf "${RED}### ERROR: ### $1\n${NORMAL}"
}
function logInfo {
printf "${YELLOW}$1\n${NORMAL}"
}