Skip to content

Commit 28b750d

Browse files
committed
rework autoexec to be more itemized
1 parent 5b59b0b commit 28b750d

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

setup-lando.sh

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -798,18 +798,67 @@ wait_for_user() {
798798
fi
799799
}
800800

801-
# determine the exec we need for sudo protected things
802-
# we add /tmp in here because their are high security environments where /tmp is not universally writable
803-
if needs_sudo; then
804-
debug "auto_exec elevating to sudo"
805-
auto_exec() {
806-
execute_sudo "$@"
807-
}
808-
else
809-
auto_exec() {
810-
execute "$@"
811-
}
812-
fi
801+
# helpers for more itemized sudoing
802+
auto_link() {
803+
local source="$1"
804+
local dest="$2"
805+
local perm_source="$(find_first_existing_parent "$source")"
806+
local perm_dest="$(find_first_existing_parent "$dest")"
807+
808+
if have_sudo_access && [[ ! -w "$perm_source" || ! -w "$perm_dest" ]]; then
809+
execute_sudo ln -sf "$source" "$dest"
810+
else
811+
execute ln -sf "$source" "$dest"
812+
fi
813+
}
814+
815+
auto_mkdirp() {
816+
local dir="$1"
817+
local perm_dir="$(find_first_existing_parent "$dir")"
818+
819+
if have_sudo_access && [[ ! -w "$perm_dir" ]]; then
820+
execute_sudo mkdir -p "$dir"
821+
else
822+
execute mkdir -p "$dir"
823+
fi
824+
}
825+
826+
auto_mv() {
827+
local source="$1"
828+
local dest="$2"
829+
local perm_source="$(find_first_existing_parent "$source")"
830+
local perm_dest="$(find_first_existing_parent "$dest")"
831+
832+
if have_sudo_access && [[ ! -w "$perm_source" || ! -w "$perm_dest" ]]; then
833+
execute_sudo mv -f "$source" "$dest"
834+
else
835+
execute mv -f "$source" "$dest"
836+
fi
837+
}
838+
839+
auto_curl_n_x() {
840+
local dest="$1"
841+
local url="$2"
842+
local perm_dir="$(find_first_existing_parent "$dest")"
843+
844+
if have_sudo_access && [[ ! -w "$perm_dir" ]]; then
845+
execute_sudo curl \
846+
--fail \
847+
--location \
848+
--progress-bar \
849+
--output "$dest" \
850+
"$url"
851+
execute_sudo chmod +x "$dest"
852+
else
853+
execute curl \
854+
--fail \
855+
--location \
856+
--progress-bar \
857+
--output "$dest" \
858+
"$url"
859+
execute chmod +x "$dest"
860+
fi
861+
}
813862

814863
# Invalidate sudo timestamp before exiting (if it wasn't active before).
815864
if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null; then
@@ -846,37 +895,31 @@ if needs_sudo; then
846895
fi
847896

848897
# Create directories if we need to
849-
if [[ ! -d "$DEST" ]]; then auto_exec mkdir -p "$DEST"; fi
850-
if [[ ! -d "$LANDO_TMPDIR" ]]; then auto_exec mkdir -p "$LANDO_TMPDIR"; fi
851-
if [[ ! -d "$LANDO_BINDIR" ]]; then execute mkdir -p "$LANDO_BINDIR"; fi
898+
if [[ ! -d "$DEST" ]]; then auto_mkdirp "$DEST"; fi
899+
if [[ ! -d "$LANDO_TMPDIR" ]]; then auto_mkdirp "$LANDO_TMPDIR"; fi
900+
if [[ ! -d "$LANDO_BINDIR" ]]; then auto_mkdirp "$LANDO_BINDIR"; fi
852901

853902
# download lando
854903
log "${tty_magenta}downloading${tty_reset} ${tty_bold}${URL}${tty_reset} to ${tty_bold}${LANDO}${tty_reset}"
855-
auto_exec curl \
856-
--fail \
857-
--location \
858-
--progress-bar \
859-
--output "$LANDO_TMPFILE" \
860-
"$URL"
861-
862-
# make executable and weak "it works" test
863-
auto_exec chmod +x "${LANDO_TMPFILE}"
904+
auto_curl_n_x "$LANDO_TMPFILE" "$URL"
905+
906+
# weak "it works" test
864907
execute "${LANDO_TMPFILE}" version >/dev/null
865908

866909
# if dest = symlinker then we need to actually mv 2 LANDO_DATADIR
867910
# NOTE: we use mv here instead of cp because of https://developer.apple.com/forums/thread/130313
868911
if [[ "$LANDO" == "$SYMLINKER" ]]; then
869-
execute mkdir -p "${LANDO_DATADIR}/${VERSION}"
870-
execute mv -f "$LANDO_TMPFILE" "$HIDDEN_LANDO"
871-
execute ln -sf "$HIDDEN_LANDO" "$SYMLINKER"
912+
auto_mkdirp "${LANDO_DATADIR}/${VERSION}"
913+
auto_mv "$LANDO_TMPFILE" "$HIDDEN_LANDO"
914+
auto_link "$HIDDEN_LANDO" "$SYMLINKER"
872915
else
873-
auto_exec mv -f "$LANDO_TMPFILE" "$LANDO"
874-
auto_exec ln -sf "$LANDO" "$SYMLINKER"
916+
auto_mv "$LANDO_TMPFILE" "$LANDO"
917+
auto_link "$LANDO" "$SYMLINKER"
875918
fi
876919

877920
# hook up the syslink here
878921
if [[ "$SYSLINK" == "1" ]]; then
879-
auto_exec ln -sf "$SYMLINKER" "$SYSLINKER"
922+
auto_link "$SYMLINKER" "$SYSLINKER"
880923
fi
881924

882925
# if lando 3 then we need to do some other cleanup things

0 commit comments

Comments
 (0)