forked from ControlxFreak/go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nickname
executable file
·199 lines (183 loc) · 6.28 KB
/
nickname
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
197
198
199
# --------------------------------------------------------------------- #
#!/bin/bash
# nickname
# A handy helper function to create the .config/nickname.conf file.
# The goal of this configuration file is to generate nicknames for
# directories so the go2 function can easily lookup a nickname and
# change the directory to that associated location.
#
# Anthony Trezza
# 12 March 2018
#
# License:
# MIT License
#
# Copyright (c) 2018 Anthony Trezza
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Change Log:
# - 12 March 2018 trezza nickname's birthday! <(^.^)>
# - 08 May 2018 trezza converted to getopts. Woah, that's handy.
# - 09 May 2018 trezza added MIT license
# TODO:
# - Input checking to prevent some weird super hacker from embedding
# something malicious... and/or from stupid people :)
# --------------------------------------------------------------------- #
# 0. Define variables and functions
CONFDIR="$HOME/.config/nickname"
CONFILE="$CONFDIR/nickname.conf"
NICKNAMEDIR=$(pwd)
NICKNAME=""
function usage() {
echo "Usage:"
echo " $0 [-hp]"
echo " $0 mynickname"
echo " $0 -d <path/to/directory> mynickname"
echo " -h Display Help."
echo " -p Print the contents of the nickname file to the console."
echo " -d Directory you wish to nickname [default=pwd]"
} # usage()
function help() {
echo "$0:"
echo " A handy tool for creating directory nickname associations that get stored in the $CONFILE file."
echo " This configuration file will ultimately be used by the go2 tool to help change directory faster and easier."
echo ""
usage
echo ""
echo "Example:"
echo " To nickname the directory '$HOME/bin/repo' as 'myrepo'"
echo " $0 -d $HOME/bin/repo myrepo"
echo " To see the contents of your nickname file"
echo " $0 -p"
} # help()
function print() {
echo "Nickname associations:"
# Read through the file and check for the directory or nickname
while IFS='' read -r line || [[ -n "$line" ]]; do
index=0
for entry in $(echo $line | sed "s/,/ /g"); do
if [ $index == 0 ]; then
ENTRY_NN="$entry"
elif [ $index == 1 ]; then
ENTRY_DIR="$entry"
fi
((index = index + 1))
done
echo "$ENTRY_NN -> $ENTRY_DIR"
done < "$CONFILE"
} # print()
function verify_config {
# Read through the file and check for the directory or nickname
while IFS='' read -r line || [[ -n "$line" ]]; do
index=0
for entry in $(echo $line | sed "s/,/ /g"); do
if [ $index == 0 ]; then
ENTRY_NN="$entry"
elif [ $index == 1 ]; then
ENTRY_DIR="$entry"
fi
((index = index + 1))
done
if [ "$ENTRY_NN" == "$NICKNAME" ]; then
echo "Nickname already in use."
echo "$NICKNAME -> $ENTRY_DIR"
exit 1;
fi
if [ "$ENTRY_DIR" == "$NICKNAMEDIR" ]; then
echo "Directory already in use."
echo "$ENTRY_NN -> $ENTRY_DIR"
echo "Do you want to renickname this directory?"
read yn </dev/tty
case $yn in
"Yes"|"yes"|"y"|"Y")
sed -i "/$ENTRY_NN/d" $CONFILE
echo "$ENTRY_NN -> $ENTRY_DIR Nickname Deleted."
return
;;
"No"|"no"|"n"|"N")
echo "Exiting..."
exit 0
;;
* )
echo "Invalid Response."
echo "Exiting..."
exit 1
;;
esac
fi
done < "$CONFILE"
}
# 1. Parse the options
if [ "$#" -eq 0 ]; then
echo "No Input Provided."
usage
exit 1
fi
while getopts ":hpd:" opt; do
case ${opt} in
h )
help
exit 0
;;
p )
print
exit 0
;;
d )
NICKNAMEDIR=${OPTARG}
if [ ! -d "$NICKNAMEDIR" ]; then
echo "Input Nickname Dirctory: $NICKNAMEDIR Does Not Exist"
exit 1
fi
;;
\? )
echo "Invalid Input: -$OPTARG"
usage
exit 1
;;
: )
echo "Invalid Option: -$OPTARG requires an argument"
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
# 2. Grab the nickname
NICKNAME=$1 # TODO check.
# 3. Check to see if the config file exists, if it doesn't, make it!
if [ ! -d "$CONFDIR" ]; then
echo "Creating $CONFILE"
mkdir "$CONFDIR"
touch "$CONFILE"
fi
if [ ! -f "$CONFILE" ];then
echo "Creating $CONFILE"
touch "$CONFILE"
fi
# 4. Read and check the config file to ensure the directory
verify_config
# 5. If we made it here, then we must be ready to append to the file!
echo "Creating nickname mapping: $NICKNAME -> $NICKNAMEDIR"
echo "$NICKNAME,$NICKNAMEDIR" >> $CONFILE
echo "Complete!"