-
Notifications
You must be signed in to change notification settings - Fork 52
/
wtool-common
124 lines (111 loc) · 2.73 KB
/
wtool-common
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
#!/bin/bash
logo(){
echo " __ __ _____ _
/ / /\ \ \/__ \___ ___ | |
\ \/ \/ / / /\/ _ \ / _ \| |
\ /\ / / / | (_) | (_) | |
\/ \/ \/ \___/ \___/|_|
"
}
pcolumn (){
echo -e $1 |awk -F'\t' '{printf "%-15s%s\n",$1,$2}'
}
ptitle (){
length=`echo "$1" |wc -c`
prefix_count=$(((30-length)/2))
for i in `seq 0 $prefix_count` ;do
echo -n '='
done
echo -n " $1 "
postfix_count=$((length % 2 != 0 ? prefix_count+1: prefix_count))
for i in `seq 0 $postfix_count` ;do
echo -n '='
done
printf '\r\n'
}
find_command(){
for folder in `ls -l $1 |grep ^d |awk '{print $9}'` ;do
if [ ! -d $1/$folder/tools ]; then
continue;
fi
commands=`ls -l $1/$folder/tools |grep -v '^d' |awk '{print $9}'`
if [ "$commands" = "" ]; then
continue;
fi
ptitle $folder
for file in $commands ;do
desp=`grep '^###' $1/$folder/tools/$file|cut -c 4-`
if [ "$desp" != "" ]; then
pcolumn "$file\t:$desp"
echo "$file" >> $HOME/.wtool/cmds.cache
fi
done
echo ''
done
}
test_exists(){
if [ -e $2 ]; then
echo "$1/$2 already exist!"
while(true);do
echo -n "(q)uit or (r)eplace?"
read choice < /dev/tty
if [ "$choice" = "q" ];then
exit 0;
elif [ "$choice" = "r" ];then
rm -fr $1/$2
break;
else
echo "$choice is not valid!"
fi
done
fi
}
list_module() {
rm -f $HOME/.wtool/cmds.cache 2>/dev/null
mkdir $HOME/.wtool 2>/dev/null
echo 'Available commands:'
find_command $WTOOL_DIR
find_command $WTOOL_DIR/custom
exit 0;
}
add_module() {
if [ "$#" -lt "2" ];then
echo "Usage : wtool addmodule group/project"
exit 1;
fi
folder_name=$2
folder_name=${folder_name//\//_}
cd $WTOOL_DIR/custom
test_exists $WTOOL_DIR/custom $folder_name
type "git" 1>/dev/null 2>/dev/null
if [ $? -eq 0 ];then
git clone https://github.com/$2.git --depth 1 $folder_name
else
mkdir $folder_name &&cd $folder_name
curl -q -L https://github.com/$2/archive/master.zip >master.zip &&unzip master.zip && rm -f master.zip
fi
echo "add module $folder_name finished"
cd .. && list_module
exit 0
}
remove_module() {
if [ "$#" -lt "2" ];then
echo "Usage : wtool rmmodule group/project"
exit 1;
fi
folder_name=$2
folder_name=${folder_name//\//_}
cd $WTOOL_DIR/custom
rm -rf ./$folder_name
echo "remove module $folder_name finished"
list_module
exit 0
}
uninstall() {
echo -n "Uninstall wtool,(y)es or (n)o?"
read choice < /dev/tty
if [ "$choice" = "y" ] && [ "$WTOOL_DIR" != "/" ];then
cd $WTOOL_DIR && make uninstall && rm -rf $WTOOL_DIR && echo "wtool uninstall finished. Bye~"
fi
exit 0
}