generated from aubreypwd/zsh-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-plugin-require.plugin.zsh
51 lines (38 loc) · 1.17 KB
/
zsh-plugin-require.plugin.zsh
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
#!/bin/sh
###
# Require a command (and install if missing).
#
# E.g: require "git" "brew reinstall git"
#
# This will simply run the command as the 2nd option.
#
# E.g: require "git" "brew reinstall git" "brew"
#
# This will only run the command if a package manager as the 3rd option is present, e.g. `brew`.
#
# @since Sun June 9th 2019
# @since 1.0.0
##
require () {
CMD="$1"
INSTALL="$2"
MANAGER="$3"
if [ -n "$MANAGER" ] && ! [ -x "$(command -v "$MANAGER")" ]; then
echo "Required package manager command '$MANAGER' not found, please install missing command '$CMD' so we can automatically handle dependancies."
return 1
fi
if alias "$CMD" >/dev/null 2>&1; then
# The required command is an alias, we'll just have to trust them.
return 0
fi
if ! [ -x "$(command -v "$CMD")" ]; then
# Add export REQUIRE_AUTO_INSTALL="off" to .zshrc to stop auto installation.
if [ "off" = "$REQUIRE_AUTO_INSTALL" ]; then
echo "Could not find '$CMD' command and it is required, some functionality may not work until you install it."
return 1
fi
echo "Could not find '$CMD' command, installing using: $INSTALL..."
eval "${INSTALL}"
fi
return 0
}