From 0e438dc840b20c125378626026f333f8da3a24b3 Mon Sep 17 00:00:00 2001 From: Glenn Franxman Date: Wed, 14 Aug 2024 18:23:55 -0400 Subject: [PATCH] feat: add function to move files to bin folder I've added the move_to_bin_folder function in install.sh. This function moves a file to the specified destination. If the destination is not writable, it uses sudo to move the file. I updated the install_kubectl, install_kind, and install_scaf functions to use the new function. --- install.sh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 92ebfbfa..d7e2f39f 100755 --- a/install.sh +++ b/install.sh @@ -168,6 +168,17 @@ detect_os_and_arch() { echo $os_arch } +move_to_bin_folder() { + DEST=${2:-$PREFERRED_BIN_FOLDER} + echo "Moving $1 to ${DEST}..." + # if the DEST folder is not writable, use sudo to move the file + if [ -w $DEST ]; then + mv $1 $DEST/$1 + else + sudo mv $1 $DEST/$1 + fi +} + install_kubectl() { echo "Installing kubectl..." os_arch=$(detect_os_and_arch) @@ -179,7 +190,7 @@ install_kubectl() { url="https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/${os}/${arch}/kubectl" curl -LO $url chmod +x ./kubectl - sudo mv ./kubectl $PREFERRED_BIN_FOLDER/kubectl + move_to_bin_folder ./kubectl ;; *) echo "Unsupported OS or architecture: $os_arch" @@ -197,7 +208,7 @@ install_kind() { "linux-amd64"|"darwin-amd64"|"darwin-arm64"|"linux-arm64"|"windows-amd64") curl -Lo ./kind "https://kind.sigs.k8s.io/dl/v0.23.0/kind-$os_arch" chmod +x ./kind - sudo mv ./kind $PREFERRED_BIN_FOLDER/kind + move_to_bin_folder ./kind ;; *) echo "Unsupported OS or architecture: $os_arch" @@ -221,7 +232,7 @@ install_scaf() { if [ -f "$TEMP_DOWNLOAD" ]; then chmod +x $TEMP_DOWNLOAD echo "Moving scaf to $DESTINATION..." - sudo mv $TEMP_DOWNLOAD $DESTINATION + move_to_bin_folder $TEMP_DOWNLOAD $DESTINATION if [ -f "$DESTINATION" ]; then echo "scaf installed successfully at $DESTINATION" else