-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathglobal_functions.sh
196 lines (180 loc) · 4.95 KB
/
global_functions.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function ask {
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question
read -p "$1 [$prompt] " REPLY
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
function load_on_login {
profile_file="$HOME/.bashrc"
if [[ -f "${profile_file}" ]] &&
! grep '. $HOME/.env/bash.env.sh' "$profile_file" >/dev/null 2>&1
then
echo '[[ -r $HOME/.env/bash.env.sh ]] && . $HOME/.env/bash.env.sh' >> "$profile_file"
echo ".env will now load on login."
else
echo ".env is already setup to load on login."
fi
return 0
}
function load_on_alias {
if [[ -z $1 ]]; then
als="bashenv"
else
als="$1"
fi
profile_file="$HOME/.bashrc"
if [[ -f "${profile_file}" ]] &&
! grep "alias $als=\". $HOME/.env/bash.env.sh\"" "$profile_file" >/dev/null 2>&1
then
echo "alias $als=\". $HOME/.env/bash.env.sh\"" >> "$profile_file"
echo ".env will now load when you execute '$als'."
else
echo ".env is already setup to load on using the '$als' alias."
fi
return 0
}
bigfind() {
if [[ $# -lt 1 ]]; then
echo_warn "Usage: bigfind DIRECTORY"
return
fi
sudo find ${1} -size +10M | xargs sudo du -h | sort -nr
# du -a ${1} | sort -n -r | head -n 10
}
# show the n most used commands. defaults to 10
history_stats() {
if [[ $# -lt 1 ]]; then
NUM=10
else
NUM=$1
fi
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -$NUM
}
gdir() {
mkdir ${1}
cd ${1}
}
upgrade.bash.env() {
$dot_env_path/bin/upgrade.env
}
# Add your public SSH key to a remote host
add_ssh_key_to_host() {
local keypath
if [[ $# -lt 1 ]]; then
echo_warn "Usage: add_ssh_key_to_host [user@]HOSTNAME"
return
fi
if [[ $# -eq 2 ]] && [[ ! -r "${2}" ]]; then
echo "Key cannot be read: ${2}"
exit 1
elif [[ -r "${2}" ]]; then
keypath="${2}"
elif [[ -r "$HOME/.ssh/id_rsa.pub" ]]; then
keypath="$HOME/.ssh/id_rsa.pub"
elif [[ -r "$HOME/.ssh/id_dsa.pub" ]]; then
keypath="$HOME/.ssh/id_dsa.pub"
else
echo "You need to generate a key first: 'ssh-keygen -b 4048 -t rsa'"
exit 1
fi
if ! command ssh-keygen -l -f "${keypath}" 1> /dev/null
then
# use error message from command
exit 1
fi
cat "${keypath}" | ssh $1 "mkdir -p ~/.ssh; cat >> .ssh/authorized_keys"
}
# Propagate your bash.env configurations to a remote host
propagate_env_to_host() {
if [[ $# -lt 1 ]]; then
echo_warn "Usage: propagate_env_to_host [user@]HOSTNAME"
return
fi
host=$1
shift 1
ENVFILE=$HOME/env.tar.gz
PWD=`pwd`
cd $HOME
echo_info "Compressing local environment..."
COPYFILE_DISABLE=1 tar chfvz $ENVFILE --exclude='.DS_Store' --exclude='.env/plugins/elocate/elocatedb' .env/ &> /dev/null
echo_info "Copying environment to $host..."
scp $ENVFILE $host:
if [[ $? != 0 ]]; then echo "Copy failed!"; return; fi
echo_info "Installing environment on $host..."
ssh $host "rm -rf ~/.env/ && gunzip < env.tar.gz |tar xfv -" &> /dev/null
echo_warn "Don't forget to add this your .bashrc file:"
echo_warn 'if [[ -n "$PS1" ]]; then'
echo_warn ' [[ -r $HOME/.env/bash.env.sh ]] && . $HOME/.env/bash.env.sh'
echo_warn 'fi'
cd $PWD
}
# Configure environment settings for your local machine.
configthis.env() {
DIR="$dot_env_path/host/$HOSTNAME"
mkdir -p "$DIR"
touch "$DIR/env.sh"
touch "$DIR/functions.sh"
if [[ ! -f "$DIR/alias.sh" ]]; then
echo "# Add your specific aliases here:\n# Example: alias home='cd \$HOME' " >> "$DIR/alias.sh"
fi
if [[ ! -f "$DIR/path.sh" ]]; then
echo "# Add paths like this:\n# pathmunge \"/Developer/usr/bin\"" >> "$DIR/path.sh"
fi
echo_info "Edit files in [$DIR] to customize your local environment."
ls -1AtF "$DIR"
}
# Configure environment settings for a specified HOSTNAME
confighost.env() {
if [[ $# -lt 1 ]]; then
echo_warn "Usage: confighost.env HOSTNAME"
return
fi
host=$1
shift 1
DIR="$dot_env_path/host/$host"
mkdir -p "$DIR"
touch "$DIR/env.sh"
touch "$DIR/functions.sh"
if [[ ! -f "$DIR/alias.sh" ]]; then
echo "# Add your host specific aliases here:\n# Example: alias home='cd \$HOME' " >> "$DIR/alias.sh"
fi
if [[ ! -f "$DIR/path.sh" ]]; then
echo "# Add paths like this:\n# pathmunge \"/Developer/usr/bin\"" >> "$DIR/path.sh"
fi
echo_info "Edit these files to customize your [$host] environment."
echo_info "When you are finished run 'propagate_env_to_host $host'."
ls -1AtF "$DIR"
}
reset_theme() {
export theme="$ORIGINAL_THEME"
. $dot_env_path/themes/load_theme.sh
}
try_theme() {
if [[ $# -lt 1 ]]; then
echo_warn "Usage: try_theme THEME_NAME"
return
fi
echo_info "Loading theme [$1]"
export ORIGINAL_THEME="$theme"
export theme="$1"
. $dot_env_path/themes/load_theme.sh
}