From fd99376f5a9239676274c509c6deb497c60db472 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Wed, 29 Nov 2023 14:09:25 +0300 Subject: [PATCH] fix shellcheck errors --- .shellcheckrc | 0 code-server/run.sh | 2 +- coder-login/run.sh | 7 ++++++- filebrowser/run.sh | 14 ++++++++------ git-commit-signing/run.sh | 8 ++++++-- git-config/run.sh | 20 ++++++++++++-------- package.json | 2 +- slackme/slackme.sh | 4 ++-- vault-github/run.sh | 12 +++++++----- vscode-web/run.sh | 15 ++++++++++----- 10 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 .shellcheckrc diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..e69de29 diff --git a/code-server/run.sh b/code-server/run.sh index 8cad538..7a9f4da 100755 --- a/code-server/run.sh +++ b/code-server/run.sh @@ -26,7 +26,7 @@ CODE_SERVER="${INSTALL_PREFIX}/bin/code-server" # Install each extension... IFS=',' read -r -a EXTENSIONLIST <<< "$${EXTENSIONS}" -for extension in "$${EXTENSIONLIST[@]}"; do +for extension in $${EXTENSIONLIST[@]}; do if [ -z "$extension" ]; then continue fi diff --git a/coder-login/run.sh b/coder-login/run.sh index c91eb1e..76659ef 100644 --- a/coder-login/run.sh +++ b/coder-login/run.sh @@ -4,12 +4,17 @@ # logged in to another deployment BOLD='\033[0;1m' +RESET='\033[0m' + +# Convert templated variables to shell variables +CODER_DEPLOYMENT_URL=${CODER_DEPLOYMENT_URL} +CODER_USER_TOKEN=${CODER_USER_TOKEN} printf "$${BOLD}Logging into Coder...\n\n$${RESET}" if ! coder list > /dev/null 2>&1; then set +x - coder login --token="${CODER_USER_TOKEN}" --url="${CODER_DEPLOYMENT_URL}" + coder login --token="$${CODER_USER_TOKEN}" --url="$${CODER_DEPLOYMENT_URL}" else echo "You are already authenticated with coder." fi diff --git a/filebrowser/run.sh b/filebrowser/run.sh index 0cbf8f4..4755f61 100644 --- a/filebrowser/run.sh +++ b/filebrowser/run.sh @@ -9,18 +9,20 @@ printf "🥳 Installation comlete! \n\n" printf "👷 Starting filebrowser in background... \n\n" +# Convert templated variables to shell variables ROOT_DIR=${FOLDER} ROOT_DIR=$${ROOT_DIR/\~/$HOME} +PORT=${PORT} +DB_PATH=${DB_PATH} +LOG_PATH=${LOG_PATH} DB_FLAG="" -if [ "${DB_PATH}" != "filebrowser.db" ]; then - DB_FLAG=" -d ${DB_PATH}" +if [ "$${DB_PATH}" != "filebrowser.db" ]; then + DB_FLAG="-d $${DB_PATH}" fi printf "📂 Serving $${ROOT_DIR} at http://localhost:${PORT} \n\n" -printf "Running 'filebrowser --noauth --root $ROOT_DIR --port ${PORT}$${DB_FLAG}' \n\n" +filebrowser --noauth --root "$${ROOT_DIR}" --port "$${PORT}" "$${DB_FLAG}" > "$${LOG_PATH}" 2>&1 & -filebrowser --noauth --root $ROOT_DIR --port ${PORT}$${DB_FLAG} > ${LOG_PATH} 2>&1 & - -printf "📝 Logs at ${LOG_PATH} \n\n" +printf "📝 Logs at %s \n\n" "$${LOG_PATH}" diff --git a/git-commit-signing/run.sh b/git-commit-signing/run.sh index d757179..0217a57 100755 --- a/git-commit-signing/run.sh +++ b/git-commit-signing/run.sh @@ -1,5 +1,9 @@ #!/usr/bin/env sh +# Convert templated variables to shell variables +CODER_AGENT_URL=${CODER_AGENT_URL} +CODER_AGENT_TOKEN=${CODER_AGENT_TOKEN} + if ! command -v git > /dev/null; then echo "git is not installed" exit 1 @@ -20,8 +24,8 @@ mkdir -p ~/.ssh/git-commit-signing echo "Downloading SSH key" ssh_key=$(curl --request GET \ - --url "${CODER_AGENT_URL}api/v2/workspaceagents/me/gitsshkey" \ - --header "Coder-Session-Token: ${CODER_AGENT_TOKEN}") + --url "$${CODER_AGENT_URL}api/v2/workspaceagents/me/gitsshkey" \ + --header "Coder-Session-Token: $${CODER_AGENT_TOKEN}") jq --raw-output ".public_key" > ~/.ssh/git-commit-signing/coder.pub << EOF $ssh_key diff --git a/git-config/run.sh b/git-config/run.sh index 36dc768..4fedd17 100644 --- a/git-config/run.sh +++ b/git-config/run.sh @@ -1,7 +1,11 @@ #!/usr/bin/env sh +# Convert templated variables to shell variables +GIT_EMAIL=${GIT_EMAIL} +GIT_USERNAME=${GIT_USERNAME} + BOLD='\033[0;1m' -printf "$${BOLD}Checking git-config!\n" +printf "$${BOLD}Checking git-config!\n\n" # Check if git is installed command -v git > /dev/null 2>&1 || { @@ -10,15 +14,15 @@ command -v git > /dev/null 2>&1 || { } # Set git username and email if missing -if [ -z $(git config --get user.email) ]; then - printf "git-config: No user.email found, setting to ${GIT_EMAIL}\n" - git config --global user.email "${GIT_EMAIL}" +if [ -z "$(git config --get user.email)" ]; then + printf "git-config: No user.email found, setting to %s\n" "$${GIT_EMAIL}" + git config --global user.email "$${GIT_EMAIL}" fi -if [ -z $(git config --get user.name) ]; then - printf "git-config: No user.name found, setting to ${GIT_USERNAME}\n" - git config --global user.name "${GIT_USERNAME}" +if [ -z "$(git config --get user.name)" ]; then + printf "git-config: No user.name found, setting to $${GIT_USERNAME}\n\n" + git config --global user.name "$${GIT_USERNAME}" fi -printf "\n$${BOLD}git-config: using email: $(git config --get user.email)\n" +printf "$${BOLD}git-config: using email: $(git config --get user.email)\n" printf "$${BOLD}git-config: using username: $(git config --get user.name)\n\n" diff --git a/package.json b/package.json index a7d1cae..0b41dde 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "test": "bun test", "fmt": "bun x prettier --plugin prettier-plugin-sh -w **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt **/*.tf .sample/main.tf", "fmt:ci": "bun x prettier --plugin prettier-plugin-sh --check **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt -check **/*.tf .sample/main.tf", - "lint": "bun run lint.ts && bun x shellcheck **/*.sh .sample/run.sh new.sh" + "lint": "bun run lint.ts && bun x shellcheck -S error **/*.sh .sample/run.sh new.sh" }, "devDependencies": { "bun-types": "^1.0.3", diff --git a/slackme/slackme.sh b/slackme/slackme.sh index e966dcf..7874d9f 100644 --- a/slackme/slackme.sh +++ b/slackme/slackme.sh @@ -73,13 +73,13 @@ fi START=$(date +%s%N) # Run all arguments as a command -$@ +"$@" END=$(date +%s%N) DURATION_MS=$${DURATION_MS:-$(((END - START) / 1000000))} PRETTY_DURATION=$(pretty_duration $DURATION_MS) set -e -COMMAND=$(echo $@) +COMMAND=$("$@") SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s|\\$COMMAND|$COMMAND|g") SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s|\\$DURATION|$PRETTY_DURATION|g") diff --git a/vault-github/run.sh b/vault-github/run.sh index 383ad9f..d415eab 100644 --- a/vault-github/run.sh +++ b/vault-github/run.sh @@ -36,11 +36,12 @@ unzip() { if [ "$${INSTALL_VERSION}" = "latest" ]; then LATEST_VERSION=$(curl -s https://releases.hashicorp.com/vault/ | grep -oP 'vault/\K[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -n 1) printf "Latest version of Vault is %s.\n\n" "$${LATEST_VERSION}" - if [ -z "$${LATEST_VERSION}" ]; then + # check if the latest version is empty + if [ "$${LATEST_VERSION}" = "" ]; then printf "Failed to determine the latest Vault version.\n" exit 1 fi - VERSION=$${LATEST_VERSION} + VERSION="$${LATEST_VERSION}" fi # Check if the vault CLI is installed and has the correct version @@ -53,9 +54,10 @@ if command -v vault > /dev/null 2>&1; then fi fi -if [ $${installation_needed} -eq 1 ]; then +if [ "$${installation_needed}" = 1 ]; then # Download and install Vault - if [ -z "$${CURRENT_VERSION}" ]; then + # if the current version is not the same as the version we want to install + if [ "$CURRENT_VERSION" = "" ]; then printf "Installing Vault CLI ...\n\n" else printf "Upgrading Vault CLI from version %s to %s ...\n\n" "$${CURRENT_VERSION}" "$${VERSION}" @@ -96,6 +98,6 @@ export VAULT_ADDR="$${VAULT_ADDR}" # Login to vault using the GitHub token printf "🔑 Logging in to Vault ...\n\n" -vault login -no-print -method=github -path=/$${AUTH_PATH} token="$${GITHUB_TOKEN}" +vault login -no-print -method=github -path="/$${AUTH_PATH}" token="$${GITHUB_TOKEN}" printf "🥳 Vault authentication complete!\n\n" printf "You can now use Vault CLI to access secrets.\n" diff --git a/vscode-web/run.sh b/vscode-web/run.sh index d19be62..a565e3b 100644 --- a/vscode-web/run.sh +++ b/vscode-web/run.sh @@ -2,13 +2,19 @@ BOLD='\033[0;1m' +# Convert templated variables to shell variables +PORT=${PORT} +LOG_PATH=${LOG_PATH} +INSTALL_DIR=${INSTALL_DIR} + + # Create install directory if it doesn't exist -mkdir -p ${INSTALL_DIR} +mkdir -p "$${INSTALL_DIR}" printf "$${BOLD}Installing vscode-cli!\n" # Download and extract code-cli tarball -output=$(curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz && tar -xf vscode_cli.tar.gz -C ${INSTALL_DIR} && rm vscode_cli.tar.gz) +output=$(curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz && tar -xf vscode_cli.tar.gz -C "$${INSTALL_DIR}" && rm vscode_cli.tar.gz) if [ $? -ne 0 ]; then echo "Failed to install vscode-cli: $output" @@ -16,6 +22,5 @@ if [ $? -ne 0 ]; then fi printf "🥳 vscode-cli has been installed.\n\n" -echo "👷 Running ${INSTALL_DIR}/bin/code serve-web --port ${PORT} --without-connection-token --accept-server-license-terms in the background..." -echo "Check logs at ${LOG_PATH}!" -${INSTALL_DIR}/code serve-web --port ${PORT} --without-connection-token --accept-server-license-terms > ${LOG_PATH} 2>&1 & +printf "Check logs at %s\n\n" "$${LOG_PATH}" +"$${INSTALL_DIR}/code" serve-web --port "$${PORT}" --without-connection-token --accept-server-license-terms > "$${LOG_PATH}" 2>&1 &