-
Notifications
You must be signed in to change notification settings - Fork 26
/
valet
executable file
·147 lines (126 loc) · 4.35 KB
/
valet
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
#!/usr/bin/env bash
set -e
SOURCE="${BASH_SOURCE[0]}"
SUDO_COMMANDS="uninstall install start restart stop unsecure secure use isolate unisolate proxy unproxy"
HOME_PATH=$HOME
function check_dependencies() {
local RED='\033[1;31m'
local NC='\033[0m'
local msg=''
for cmd in "jq" "xsel" "certutil"; do
local str=''
if ! [[ -x "$(command -v $cmd)" ]]; then
printf -v str " - %s\n" "$cmd"
local msg+="$str"
fi
done
if [[ $msg != '' ]]; then
printf "%sYou have missing Valet dependencies:\n" "${RED}"
printf "%s" "$msg"
printf "\nPlease refer to https://valetlinux.plus/ on how to install them.%s\n" "${NC}"
exit 1
fi
}
function verify_ngrok_auth() {
if ! [[ -f "${HOME}/.config/ngrok/ngrok.yml" ]]
then
if [[ -f "${HOME}/.ngrok2/ngrok.yml" ]]
then
sudo -u "$USER" "$DIR/bin/ngrok" config upgrade --relocate
else
printf "Please register for an ngrok account at: https://dashboard.ngrok.com/signup and install your authtoken.%s\n" "${NC}"
printf "Valet can help you to install the authtoken, use 'valet ngrok-auth {authtoken}' command.\n"
exit 1
fi
fi
}
# If the current source is a symbolic link, we need to resolve it to an
# actual directory name. We'll use PHP to do this easier than we can
# do it in pure Bash. So, we'll call into PHP CLI here to resolve.
if [[ -L $SOURCE ]]
then
DIR=$( dirname "$( readlink -f "$SOURCE" )" )
else
DIR="$( cd "$( dirname "$SOURCE" )" && pwd )"
fi
# If we are in the global Composer "bin" directory, we need to bump our
# current directory up two, so that we will correctly proxy into the
# Valet CLI script which is written in PHP. Will use PHP to do it.
if [[ ! -f "$DIR/cli/valet.php" ]]
then
DIR="$( cd "$DIR/../genesisweb/valet-linux-plus" && pwd )"
fi
# If the command is one of the commands that requires "sudo" privileges
# then we'll proxy the incoming CLI request using sudo, which allows
# us to never require the end users to manually specify each sudo.
if [[ -n $1 && $SUDO_COMMANDS =~ $1 ]]
then
check_dependencies
if [[ "$EUID" -ne 0 ]]
then
sudo env HOME="$HOME_PATH" "$SOURCE" "$@"
exit 0
fi
fi
if [[ -n $2 && "domain port" =~ $1 ]]
then
if [[ "$EUID" -ne 0 ]]
then
sudo env HOME="$HOME_PATH" "$SOURCE" "$@"
exit 0
fi
fi
# If the command is the "share" command we will need to resolve out any
# symbolic links for the site. Before starting Ngrok, we will fire a
# process to retrieve the live Ngrok tunnel URL in the background.
if [[ "$1" = "share" ]]
then
check_dependencies
verify_ngrok_auth
HOST="${PWD##*/}"
DOMAIN=$(cat "$HOME/.config/valet/config.json" | jq -r ".domain")
PORT=$(cat "$HOME/.config/valet/config.json" | jq -r ".port")
for linkname in ~/.config/valet/Sites/*; do
if [[ "$(readlink "${linkname}")" = "$PWD" ]]
then
HOST="${linkname##*/}"
fi
done
# Decide the correct PORT to use according if the site has a secure
# config or not.
if grep --no-messages --quiet 443 ~/.config/valet/Nginx/"$HOST"*
then
PORT=88
fi
# Fetch Ngrok URL In Background
bash "$DIR/cli/scripts/fetch-share-url.sh" &
sudo -u "$USER" "$DIR/bin/ngrok" http "$HOST.$DOMAIN:$PORT" --host-header=rewrite
exit
# Proxy PHP commands to the "php" executable on the isolated site
elif [[ "$1" = "php" ]]
then
if [[ $2 == *"--site="* ]]; then
SITE=${2#*=}
$(/usr/bin/php "$DIR/cli/valet.php" which-php "$SITE") "${@:3}"
else
$(/usr/bin/php "$DIR/cli/valet.php" which-php) "${@:2}"
fi
exit
# Proxy Composer commands with the "php" executable on the isolated site
elif [[ "$1" = "composer" ]]
then
if [[ $2 == *"--site="* ]]; then
SITE=${2#*=}
# shellcheck disable=SC2046
$(/usr/bin/php "$DIR/cli/valet.php" which-php "$SITE") $(which composer) "${@:3}"
else
# shellcheck disable=SC2046
$(/usr/bin/php "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}"
fi
exit
# Finally, for every other command we will just proxy into the PHP tool
# and let it handle the request. These are commands which can be run
# without sudo and don't require taking over terminals like Ngrok.
else
/usr/bin/php "$DIR/cli/valet.php" "$@"
fi