Repository: lfaoro/ssm
Pull request #4: Bash script changes
Author: login: george-cindea
Labels: name: enhancement
description: New feature or request
name: good first issue
description: Good for newcomers
Created: 2025-04-25T09:15:50Z | Merged: 2025-04-26T14:44:11Z

In this PR:

- change simple if statements to inline for ease of read
- removed -n flag in test, it is the default behavior
- declare local variables in separate statement
- change variable name from dir to directory (dir is a linux command, possibly not in all distros)
- remove usage of $? - this is finicky and prone to error
- LATEST_RELEASE_URL var is not used anywhere so it was commented
- remove usage of pipe and change to here-strings
- introduce regex test instead of comparison


Diff:
diff --git a/scripts/dev.sh b/scripts/dev.sh
index d71ea89..d36364f 100755
--- a/scripts/dev.sh
+++ b/scripts/dev.sh
@@ -22,7 +22,7 @@ cleanup() {
 }
 trap cleanup SIGINT SIGTERM SIGQUIT
 
-start_app(){
+start_app() {
     reset
     export TERM=xterm-256color
     echo "starting ${APP}"
diff --git a/scripts/get.sh b/scripts/get.sh
index 4a6eb76..741d522 100755
--- a/scripts/get.sh
+++ b/scripts/get.sh
@@ -8,12 +8,8 @@
 set -euo pipefail
 
 cleanup() {
-    if [ -n "${TEMP_FILE:-}" ]; then
-        rm -f "$TEMP_FILE"
-    fi
-    if [ -n "${TEMP_DIR:-}" ]; then
-        rm -rf "$TEMP_DIR"
-    fi
+    if [[ "${TEMP_FILE:-}" ]]; then rm -f "$TEMP_FILE"; fi
+    if [[ "${TEMP_DIR:-}" ]]; then rm -rf "$TEMP_DIR"; fi
 }
 trap cleanup EXIT
 
@@ -23,38 +19,39 @@ error() {
 }
 
 is_writable() {
-    local dir="$1"
-    if [ ! -d "$dir" ]; then
-        return 1
-    fi
-    local temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
-    if ! mv "$temp_check" "$dir/" 2>/dev/null; then
+    local path temp_check
+    path="$1"
+    if [[ ! -d "$path" ]]; then return 1; fi
+    temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
+    if ! mv "$temp_check" "$path/" 2>/dev/null; then
         rm -f "$temp_check"
         return 1
     fi
-    rm -f "$dir/$(basename "$temp_check")"
+    rm -f "$path/$(basename "$temp_check")"
     return 0
 }
 
 check_permissions() {
-    local dir="$1"
+    local path
+    path="$1"
     TEMP_FILE=$(mktemp -t install_XXXXXX) || error "failed to create temp file"
-    if ! mv "$TEMP_FILE" "$dir/" 2>/dev/null; then
-        echo "warning: no write permission in $dir"
+    if ! mv "$TEMP_FILE" "$path/" 2>/dev/null; then
+        echo "warning: no write permission in $path"
         INSTALL_DIR="$HOME/.local/bin"
         mkdir -p "$INSTALL_DIR" || error "failed to create $INSTALL_DIR"
     fi
-    rm -f "$dir/$(basename "$TEMP_FILE")" 2>/dev/null
+    rm -f "$path/$(basename "$TEMP_FILE")" 2>/dev/null
 }
 
 check_path() {
-    local dir="$1"
-    if [[ ":$PATH:" != *":$dir:"* ]]; then
-        echo "Warning: $dir is not in your PATH"
+    local path
+    path="$1"
+    if [[ ":$PATH:" != *":$path:"* ]]; then
+        echo "Warning: $path is not in your PATH"
         case "$SHELL" in
-            *bash) echo "Run: echo 'export PATH=\$PATH:$dir' >> ~/.bashrc" ;;
-            *zsh)  echo "Run: echo 'export PATH=\$PATH:$dir' >> ~/.zshrc" ;;
-            *)     echo "Add $dir to your PATH" ;;
+            *bash) echo "Run: echo 'export PATH=\$PATH:$path' >> ~/.bashrc" ;;
+            *zsh)  echo "Run: echo 'export PATH=\$PATH:$path' >> ~/.zshrc" ;;
+            *)     echo "Add $path to your PATH" ;;
         esac
     fi
 }
@@ -62,29 +59,28 @@ check_path() {
 # Configuration
 APP_NAME=ssm
 REPO="lfaoro/ssm"
-LATEST_RELEASE_URL="https://github.com/${REPO}/releases/latest"
 DOWNLOAD_URL="https://github.com/${REPO}/releases/download"
 
 # get latest version
 echo "Fetching latest version..."
 echo "Making API request to: https://api.github.com/repos/${REPO}/releases/latest"
-API_RESPONSE=$(curl -sSL "https://api.github.com/repos/${REPO}/releases/latest" 2>&1)
-if [ $? -ne 0 ]; then
+
+if ! API_RESPONSE=$(curl -sSL "https://api.github.com/repos/${REPO}/releases/latest" 2>&1); then
     echo "Error: Failed to fetch from GitHub API"
     echo "Debug: Curl response:"
     echo "$API_RESPONSE"
     error "GitHub API request failed"
 fi
 
-VERSION=$(echo "$API_RESPONSE" | grep -o '"tag_name": "[^"]*"' | sed 's/"tag_name": "//;s/"//')
-if [ -z "$VERSION" ]; then
+VERSION=$(sed 's/"tag_name": "//;s/"//' <<< "$(grep -o '"tag_name": "[^"]*"' <<< "$API_RESPONSE")")
+if [[ -z "$VERSION" ]]; then
     echo "Debug: Raw API response:"
     echo "$API_RESPONSE"
     error "failed to determine latest version"
 fi
 echo "Found version: ${VERSION}"
 
-OS=$(uname -s | tr '[:upper:]' '[:lower:]')
+OS=$(tr '[:upper:]' '[:lower:]' <<< "$(uname -s)")
 ARCH=$(uname -m)
 case "${ARCH}" in
     x86_64|amd64) ARCH="x86_64" ;;
@@ -116,11 +112,6 @@ case "${OS}" in
             INSTALL_DIR="$HOME/.local/bin"
         fi
         ;;
-    msys*|mingw*)
-        OS="windows"
-        ARCHIVE_NAME="${APP_NAME}_windows_${ARCH}.zip"
-        INSTALL_DIR="$HOME/bin"
-        ;;
     *) error "Unsupported operating system: ${OS}" ;;
 esac
 
@@ -140,10 +131,10 @@ echo "Attempting to download from: ${DOWNLOAD_ARCHIVE_URL}"
 # verify the download url exists before attempting to download
 echo "Verifying download URL..."
 HTTP_STATUS=$(curl -L -s -o /dev/null -w "%{http_code}" "${DOWNLOAD_ARCHIVE_URL}")
-if [ "$HTTP_STATUS" != "200" ]; then
+if [[ "$HTTP_STATUS" != "200" ]]; then
     echo "Error: HTTP status code: ${HTTP_STATUS}"
     echo "Debug: Attempting to list available assets..."
-    curl -sSL "https://api.github.com/repos/${REPO}/releases/latest" | grep -o '"browser_download_url": "[^"]*"' | sed 's/"browser_download_url": "//;s/"//'
+    sed 's/"browser_download_url": "//;s/"//' <<< "$(grep -o '"browser_download_url": "[^"]*"' <<< "$API_RESPONSE")"
     error "download URL not accessible: ${DOWNLOAD_ARCHIVE_URL}"
 fi
 
@@ -151,49 +142,26 @@ fi
 TEMP_DIR=$(mktemp -d) || error "failed to create temporary directory"
 
 # Download and extract the archive
-if [ "${OS}" = "windows" ]; then
-    echo "Downloading Windows binary..."
-    if ! /usr/bin/curl -fsSL "${DOWNLOAD_ARCHIVE_URL}" -o "${TEMP_DIR}/${ARCHIVE_NAME}" --progress-bar; then
-        echo "Error: Failed to download Windows binary"
-        error "Download failed"
-    fi
-    echo "Extracting Windows binary..."
-    if ! unzip -q "${TEMP_DIR}/${ARCHIVE_NAME}" -d "${TEMP_DIR}"; then
-        echo "Error: Failed to extract Windows binary"
-        error "Failed to extract archive"
-    fi
-    echo "Installing Windows binary..."
-    if ! mv "${TEMP_DIR}/ssm" "${INSTALL_DIR}/${APP_NAME}.exe"; then
-        echo "Error: Failed to move Windows binary"
-        error "Failed to move binary"
-    fi
-    if ! chmod +x "${INSTALL_DIR}/${APP_NAME}.exe"; then
-        echo "Error: Failed to set executable permissions"
-        error "failed to set executable permissions"
-    fi
-    BINARY_PATH="${INSTALL_DIR}/${APP_NAME}.exe"
-else
-    echo "Downloading binary..."
-    if ! /usr/bin/curl -fsSL "${DOWNLOAD_ARCHIVE_URL}" -o "${TEMP_DIR}/${ARCHIVE_NAME}" --progress-bar; then
-        echo "Error: Failed to download binary"
-        error "Download failed"
-    fi
-    echo "Extracting binary..."
-    if ! tar -xzf "${TEMP_DIR}/${ARCHIVE_NAME}" -C "${TEMP_DIR}"; then
-        echo "Error: Failed to extract binary"
-        error "Failed to extract archive"
-    fi
-    echo "Installing binary..."
-    if ! mv "${TEMP_DIR}/ssm" "${INSTALL_DIR}/${APP_NAME}"; then
-        echo "Error: Failed to move binary"
-        error "Failed to move binary"
-    fi
-    if ! chmod +x "${INSTALL_DIR}/${APP_NAME}"; then
-        echo "Error: Failed to set executable permissions"
-        error "failed to set executable permissions"
-    fi
-    BINARY_PATH="${INSTALL_DIR}/${APP_NAME}"
+echo "Downloading binary..."
+if ! /usr/bin/curl -fsSL "${DOWNLOAD_ARCHIVE_URL}" -o "${TEMP_DIR}/${ARCHIVE_NAME}" --progress-bar; then
+    echo "Error: Failed to download binary"
+    error "Download failed"
+fi
+echo "Extracting binary..."
+if ! tar -xzf "${TEMP_DIR}/${ARCHIVE_NAME}" -C "${TEMP_DIR}"; then
+    echo "Error: Failed to extract binary"
+    error "Failed to extract archive"
+fi
+echo "Installing binary..."
+if ! mv "${TEMP_DIR}/ssm" "${INSTALL_DIR}/${APP_NAME}"; then
+    echo "Error: Failed to move binary"
+    error "Failed to move binary"
+fi
+if ! chmod +x "${INSTALL_DIR}/${APP_NAME}"; then
+    echo "Error: Failed to set executable permissions"
+    error "failed to set executable permissions"
 fi
+BINARY_PATH="${INSTALL_DIR}/${APP_NAME}"
 
 echo "Successfully installed ${APP_NAME} to: ${BINARY_PATH}"
 check_path "${INSTALL_DIR}"


Reviews:
user: login: lfaoro
state: CHANGES_REQUESTED
author_association: OWNER
submitted_at: 2025-04-26T12:39:55Z
user: login: lfaoro
state: COMMENTED
author_association: OWNER
submitted_at: 2025-04-26T12:41:16Z

Review comments:
diff_hunk: @@ -23,68 +19,70 @@ error() {
 }
 
 is_writable() {
-    local dir="$1"
-    if [ ! -d "$dir" ]; then
-        return 1
-    fi
-    local temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
-    if ! mv "$temp_check" "$dir/" 2>/dev/null; then
+    local directory temp_check
+    directory="$1"
+    if [[ ! -d "$directory" ]]; then return 1; fi
+    temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
+    if ! mv "$temp_check" "$directory/" 2>/dev/null; then
         rm -f "$temp_check"
         return 1
     fi
-    rm -f "$dir/$(basename "$temp_check")"
+    rm -f "$directory/$(basename "$temp_check")"
path: scripts/get.sh
user: login: lfaoro
body: `dir` is short and clear, don't see the need to use `directory`
created_at: 2025-04-25T15:33:23Z
author_association: OWNER
diff_hunk: @@ -23,68 +19,70 @@ error() {
 }
 
 is_writable() {
-    local dir="$1"
-    if [ ! -d "$dir" ]; then
-        return 1
-    fi
-    local temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
-    if ! mv "$temp_check" "$dir/" 2>/dev/null; then
+    local directory temp_check
+    directory="$1"
+    if [[ ! -d "$directory" ]]; then return 1; fi
+    temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
+    if ! mv "$temp_check" "$directory/" 2>/dev/null; then
         rm -f "$temp_check"
         return 1
     fi
-    rm -f "$dir/$(basename "$temp_check")"
+    rm -f "$directory/$(basename "$temp_check")"
     return 0
 }
 
 check_permissions() {
-    local dir="$1"
+    local directory
+    directory="$1"
     TEMP_FILE=$(mktemp -t install_XXXXXX) || error "failed to create temp file"
-    if ! mv "$TEMP_FILE" "$dir/" 2>/dev/null; then
-        echo "warning: no write permission in $dir"
+    if ! mv "$TEMP_FILE" "$directory/" 2>/dev/null; then
+        echo "warning: no write permission in $directory"
         INSTALL_DIR="$HOME/.local/bin"
         mkdir -p "$INSTALL_DIR" || error "failed to create $INSTALL_DIR"
     fi
-    rm -f "$dir/$(basename "$TEMP_FILE")" 2>/dev/null
+    rm -f "$directory/$(basename "$TEMP_FILE")" 2>/dev/null
 }
 
 check_path() {
-    local dir="$1"
-    if [[ ":$PATH:" != *":$dir:"* ]]; then
-        echo "Warning: $dir is not in your PATH"
+    local directory
+    directory="$1"
+    if [[ ":$PATH:" != *":$directory:"* ]]; then
+        echo "Warning: $directory is not in your PATH"
         case "$SHELL" in
-            *bash) echo "Run: echo 'export PATH=\$PATH:$dir' >> ~/.bashrc" ;;
-            *zsh)  echo "Run: echo 'export PATH=\$PATH:$dir' >> ~/.zshrc" ;;
-            *)     echo "Add $dir to your PATH" ;;
+            *bash) echo "Run: echo 'export PATH=\$PATH:$directory' >> ~/.bashrc" ;;
+            *zsh)  echo "Run: echo 'export PATH=\$PATH:$directory' >> ~/.zshrc" ;;
+            *)     echo "Add $directory to your PATH" ;;
         esac
     fi
 }
 
 # Configuration
 APP_NAME=ssm
 REPO="lfaoro/ssm"
-LATEST_RELEASE_URL="https://github.com/${REPO}/releases/latest"
+#Not used anywhere, if used externally, export it
path: scripts/get.sh
user: login: lfaoro
body: if unused, remove it
created_at: 2025-04-25T15:34:01Z
author_association: OWNER
diff_hunk: @@ -140,18 +138,18 @@ echo "Attempting to download from: ${DOWNLOAD_ARCHIVE_URL}"
 # verify the download url exists before attempting to download
 echo "Verifying download URL..."
 HTTP_STATUS=$(curl -L -s -o /dev/null -w "%{http_code}" "${DOWNLOAD_ARCHIVE_URL}")
-if [ "$HTTP_STATUS" != "200" ]; then
+if [[ "$HTTP_STATUS" != "200" ]]; then
     echo "Error: HTTP status code: ${HTTP_STATUS}"
     echo "Debug: Attempting to list available assets..."
-    curl -sSL "https://api.github.com/repos/${REPO}/releases/latest" | grep -o '"browser_download_url": "[^"]*"' | sed 's/"browser_download_url": "//;s/"//'
+    sed 's/"browser_download_url": "//;s/"//' <<< "$(grep -o '"browser_download_url": "[^"]*"' <<< "$API_RESPONSE")"
     error "download URL not accessible: ${DOWNLOAD_ARCHIVE_URL}"
 fi
 
 # Create temporary directory for extraction
 TEMP_DIR=$(mktemp -d) || error "failed to create temporary directory"
 
 # Download and extract the archive
-if [ "${OS}" = "windows" ]; then
+if [[ "${OS}" =~ "windows" ]]; then
path: scripts/get.sh
user: login: lfaoro
body: you can remove windows support, as win users use ssm via WSL2
we don't release ssm.exe since 0.2.1
created_at: 2025-04-25T15:35:26Z
author_association: OWNER
diff_hunk: @@ -23,68 +19,70 @@ error() {
 }
 
 is_writable() {
-    local dir="$1"
-    if [ ! -d "$dir" ]; then
-        return 1
-    fi
-    local temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
-    if ! mv "$temp_check" "$dir/" 2>/dev/null; then
+    local directory temp_check
+    directory="$1"
+    if [[ ! -d "$directory" ]]; then return 1; fi
+    temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
+    if ! mv "$temp_check" "$directory/" 2>/dev/null; then
         rm -f "$temp_check"
         return 1
     fi
-    rm -f "$dir/$(basename "$temp_check")"
+    rm -f "$directory/$(basename "$temp_check")"
path: scripts/get.sh
user: login: lfaoro
body: test
created_at: 2025-04-26T12:38:56Z
author_association: OWNER
diff_hunk: @@ -22,7 +22,7 @@ cleanup() {
 }
 trap cleanup SIGINT SIGTERM SIGQUIT
 
-start_app(){
+start_app() {
path: scripts/dev.sh
user: login: lfaoro
body: test
created_at: 2025-04-26T12:39:23Z
author_association: OWNER
diff_hunk: @@ -23,68 +19,70 @@ error() {
 }
 
 is_writable() {
-    local dir="$1"
-    if [ ! -d "$dir" ]; then
-        return 1
-    fi
-    local temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
-    if ! mv "$temp_check" "$dir/" 2>/dev/null; then
+    local directory temp_check
+    directory="$1"
+    if [[ ! -d "$directory" ]]; then return 1; fi
+    temp_check=$(mktemp -t install_check_XXXXXX) || error "failed to create temp file"
+    if ! mv "$temp_check" "$directory/" 2>/dev/null; then
         rm -f "$temp_check"
         return 1
     fi
-    rm -f "$dir/$(basename "$temp_check")"
+    rm -f "$directory/$(basename "$temp_check")"
path: scripts/get.sh
user: login: lfaoro
body: use path instead of dir
created_at: 2025-04-26T12:41:16Z
author_association: OWNER

PR comments:
user: login: lfaoro
created_at: 2025-04-25T15:36:37Z
body: thank you @george-cindea for the script improvements, I left some code notes, resolve them and it will be merged -- truly appreciate your support!
author_association: OWNER
user: login: lfaoro
created_at: 2025-04-25T16:41:57Z
body: @all-contributors please add @george-cindea for code
author_association: OWNER
user: login: allcontributors[bot]
created_at: 2025-04-25T16:42:00Z
body: @lfaoro 

We had trouble processing your request. Please try again later.
author_association: NONE
user: login: lfaoro
created_at: 2025-04-26T12:40:26Z
body: test again
author_association: OWNER
user: login: george-cindea
created_at: 2025-04-26T12:56:00Z
body: All done. don't forget to squash commits at/before merge :)
author_association: CONTRIBUTOR
user: login: lfaoro
created_at: 2025-04-26T14:45:19Z
body: too late. i merged as is :))
author_association: OWNER
user: login: lfaoro
created_at: 2025-04-26T14:45:45Z
body: @all-contributors please add @george-cindea for code
author_association: OWNER
user: login: allcontributors[bot]
created_at: 2025-04-26T14:45:48Z
body: @lfaoro 

We had trouble processing your request. Please try again later.
author_association: NONE