From 998e4c32dfb4b032dc21597fec4be568e06a9823 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Fri, 6 Dec 2019 18:49:49 -0500 Subject: [PATCH 1/2] Allow valet share to support multiple domains from same app This PR allows passing a specific domain name to `valet share` in order to have the app be served for that domain. Now how it works is this: - (NEW) if a domain name is passed, it checks whether it matches a link (which would be required for multiple domains served by same app project), and uses that - if a domain name is NOT passed, it looks up any links for current directory, and uses the first found link - else falls back to current project foldername It also still allows passing through custom ngrok parameters if desired (ref: #112), as either the 2nd parameter (no domain name passed) or (NEW) 3rd parameter Fixes #537 --- cli/scripts/fetch-share-url.sh | 2 +- valet | 40 +++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/cli/scripts/fetch-share-url.sh b/cli/scripts/fetch-share-url.sh index ccb8ae146..febc26a69 100644 --- a/cli/scripts/fetch-share-url.sh +++ b/cli/scripts/fetch-share-url.sh @@ -2,4 +2,4 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -php $DIR/../valet.php fetch-share-url | pbcopy +php $DIR/../valet.php fetch-share-url $1 | pbcopy diff --git a/valet b/valet index 79c093f63..54ca674b0 100755 --- a/valet +++ b/valet @@ -31,18 +31,38 @@ fi # process to retrieve the live Ngrok tunnel URL in the background. if [[ "$1" = "share" ]] then + # check for parameters to passthru to ngrok (they start with '-' or '--') + PARAMS=${3:-$2} + if [[ ${PARAMS:0:1} != '-' ]]; then + PARAMS='' + fi + HOST="${PWD##*/}" - TLD=$(php "$DIR/cli/valet.php" tld) - for linkname in ~/.config/valet/Sites/*; do - if [[ "$(readlink $linkname)" = "$PWD" ]] - then - HOST="${linkname##*/}" + # check for custom domain passed through to the share command ($2 w/o '-' prefix) + if [[ ${2:0:1} != '-' ]]; then + # if not blank and is a link or is the cwd use it + if [[ ! -z $2 && (-L ~/.config/valet/Sites/$2 || $2 == $HOST) ]]; then + HOST=$2 + CLIHOST=$2 fi - done + fi + + # if no custom domain passed then check if there's a linked site for cwd + if [[ -z $CLIHOST ]]; then + # find the first linked site for the current dir, if one exists + for linkname in ~/.config/valet/Sites/*; do + if [[ "$(readlink $linkname)" = "$PWD" ]] + then + HOST="${linkname##*/}" + break + fi + done + fi + + TLD=$(php "$DIR/cli/valet.php" tld) - # Decide the correct PORT to use according if the site has a secure - # config or not. + # Decide the correct PORT: uses 60 for secure, else 80 if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST* then PORT=60 @@ -51,8 +71,8 @@ then fi # Fetch Ngrok URL In Background... - bash "$DIR/cli/scripts/fetch-share-url.sh" & - sudo -u "$(logname)" "$DIR/bin/ngrok" http "$HOST.$TLD:$PORT" -host-header=rewrite ${*:2} + bash "$DIR/cli/scripts/fetch-share-url.sh" "$HOST" & + sudo -u "$(logname)" "$DIR/bin/ngrok" http "$HOST.$TLD:$PORT" -host-header=rewrite $PARAMS exit # Finally, for every other command we will just proxy into the PHP tool From 99f0bd19c5fe43b27aaa10adfad7409b09bc61ab Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Tue, 10 Dec 2019 23:17:49 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-Authored-By: Matt Stauffer --- valet | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/valet b/valet index 54ca674b0..ced31ec24 100755 --- a/valet +++ b/valet @@ -31,7 +31,7 @@ fi # process to retrieve the live Ngrok tunnel URL in the background. if [[ "$1" = "share" ]] then - # check for parameters to passthru to ngrok (they start with '-' or '--') + # Check for parameters to pass through to ngrok (these will start with '-' or '--') PARAMS=${3:-$2} if [[ ${PARAMS:0:1} != '-' ]]; then PARAMS='' @@ -39,18 +39,18 @@ then HOST="${PWD##*/}" - # check for custom domain passed through to the share command ($2 w/o '-' prefix) + # Check for custom domain passed through to the share command ($2 w/o '-' prefix) if [[ ${2:0:1} != '-' ]]; then - # if not blank and is a link or is the cwd use it + # If not blank and is a link, or is the cwd, use it if [[ ! -z $2 && (-L ~/.config/valet/Sites/$2 || $2 == $HOST) ]]; then HOST=$2 CLIHOST=$2 fi fi - # if no custom domain passed then check if there's a linked site for cwd + # If no custom domain passed, then check if there's a linked site for cwd if [[ -z $CLIHOST ]]; then - # find the first linked site for the current dir, if one exists + # Find the first linked site for the current dir, if one exists for linkname in ~/.config/valet/Sites/*; do if [[ "$(readlink $linkname)" = "$PWD" ]] then