-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcx
executable file
·166 lines (139 loc) · 3.71 KB
/
cx
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
#!/bin/bash
########## COLORS ##########
defaultColor='\033[0m'
red='\033[0;31m'
cyan='\033[0;36m'
blue='\033[0;34m'
########## GLOBAL VARIABLES ##########
# var: number of arguments passed
args_number=$#
# var: list of passed arguments
args=$*
# var: arguments
script=$0
command=$1
alias=$2
option=$3
new_alias=$3
path=$4
# var: array of args
declare -a args_array=("$@")
## path ##
# path of saved shortcuts files
cx_path="$HOME/.cxdir"
# to change
csv_file_path="$cx_path/saved.csv"
# path of errors file
errors_file_path="$cx_path/errors.sh"
# cmd path
list_file_path="$cx_path/cmd/list.sh"
create_file_path="$cx_path/cmd/create.sh"
delete_file_path="$cx_path/cmd/delete.sh"
rename_file_path="$cx_path/cmd/rename.sh"
update_file_path="$cx_path/cmd/update.sh"
change_file_path="$cx_path/cmd/change.sh"
########## CORE FUNCTIONS ##########
# This is more like an entry function.
function cx__main () {
# if has params
if [[ $(cx__hasParams) = "true" ]]; then
case $command in
# user want to [create] a new shortcut
"create") cx__create_main;;
# user want to [delete] a shortcut
"delete") cx__delete_main;;
# user want to [rename] a shortcut
"rename") cx__rename_main;;
# user want to [update] the path asigned to a shortcut
"update") cx__update_main;;
# user want to [list] all shortcuts
"list") cx__list_main;;
# user want to uninstall cxdir
"uninstall") uninstall;;
# user want help about the `cx` command
"--help") show_help_cx;;
# user want to know the software version
"--version" | "-v") echo "cxdir version 0.1.0"
exit 0;;
# user want to use [cx] to change dir
* ) cx__change_dir;;
esac
# if no params
else
(
source "$errors_file_path"
cx__error_no_command $command
);
fi
}
# entry function to create a new shortcut
function cx__create_main () {
(
source $create_file_path
cx__create
)
}
function cx__delete_main () {
(
source $delete_file_path
cx__delete
)
}
function cx__rename_main () {
(
source $rename_file_path
cx__rename
)
}
function cx__update_main () {
(
source $update_file_path
cx__update
)
}
function cx__list_main () {
(
source $list_file_path
cx__list ',' "$(cat $csv_file_path)" "false" "true"
)
}
function cx__change_dir () {
source $change_file_path
cx__change
}
function uninstall () {
if [[ -n $alias ]]; then
echo -e $red"cx:"$defaultColor "\"$alias\"" "is invalid"
return
fi
rm -rf "$cx_path"
sed -i '/cx=/d' "$HOME/.bashrc"
sed -i '/cxdir/d' "$HOME/.bashrc"
echo "uninstalled"
}
########## UTILS AND HELPERS ##########
function cx__hasParams() {
if [[ $args_number -gt 0 ]]; then # no args
echo "true"
else # args found
echo "false"
fi
}
function show_help_cx () {
echo ""
echo "cxdir: cx [<shortcut_name>] - to change directory"
echo "cxdir: cx [--help] [--version]"
echo ""
echo "These are common cxdir command used in various situation:"
echo ""
echo -e "create \t \t \t create a new shortcut"
echo -e "list \t \t \t list all saved shortcuts with their poiting path"
echo -e "rename \t \t \t rename a shortcut"
echo -e "update \t \t \t update the path of a shortcut"
echo -e "delete \t \t \t delete a saved shortcut"
echo -e "uninstall \t \t \t uninstall cxdir"
echo ""
echo "Use 'cx <command> --help' to more about each command"
echo ""
}
cx__main