-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathm
More file actions
executable file
·94 lines (74 loc) · 2.14 KB
/
m
File metadata and controls
executable file
·94 lines (74 loc) · 2.14 KB
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
#!/usr/bin/env bash
# Resolve script path, even if it's a symlink
SOURCE="${BASH_SOURCE[0]:-$0}"
while [ -L "$SOURCE" ]; do
DIR="$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE"
done
MCLI_PATH="$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)"
# export MCLI_PATH
confirm() {
read -r -p "${1:-Are you sure? [y/N]} " response
case $response in
[yY][eE][sS]|[yY]) return 0 ;;
*) return 1 ;;
esac
}
get_version(){
if ! command -v git >/dev/null 2>&1; then
echo "m-cli version: unknown (git not found)"
return 1
fi
local git_tag=$(git -C ${MCLI_PATH} describe --tags --exact-match HEAD 2>/dev/null)
local git_hash=$(git -C ${MCLI_PATH} rev-parse --short HEAD 2>/dev/null)
if [ -n "$git_tag" ]; then
echo "m-cli version: $git_tag ($git_hash)"
else
echo "m-cli version: $git_hash (not tagged)"
fi
}
update_mcli(){
confirm "Do you want to update m-cli? [y/n]: " || exit 0
INSTALL_DIR=${MCLI_PATH} bash ${MCLI_PATH}/install.sh
}
uninstall_mcli(){
confirm "Do you want to uninstall m-cli? [y/n]: " || exit 0
sudo rm -rf ${MCLI_PATH} 2>/dev/null \
sudo rm -f "/usr/local/bin/m" 2>/dev/null \
sudo rm -f "${HOME}/.local/bin/m" 2>/dev/null \
echo "Done !"
}
usage(){
cat <<__EOF__
Swiss Army Knife for macOS !
Usage: m [Options] COMMAND [COMMAND_OPTIONS] [ARGS..]
Options:
--help Show this help message
--update Update 'm-cli' to the latest version
--uninstall Uninstall 'm-cli'
--version Show the version of 'm-cli'
COMMANDS:
__EOF__
for i in "$MCLI_PATH"/plugins/*; do
[ -f "$i" ] && [ ! -L "$i" ] && echo " ${i##*/}"
done
}
case $1 in
--update)
update_mcli && exit 0
;;
--uninstall)
uninstall_mcli && exit 0
;;
--version)
get_version && exit 0
;;
--help)
usage && exit 0
;;
esac
COMMAND=${1}
shift;
[ ! -f ${MCLI_PATH}/plugins/${COMMAND} ] && usage && exit 1
${MCLI_PATH}/plugins/${COMMAND} "$@"