From a32780e1cc7f856f9c3886ec6d0ef49fc2a3f140 Mon Sep 17 00:00:00 2001 From: Vatsal Gupta Date: Sat, 23 Nov 2024 09:24:54 +0530 Subject: [PATCH 1/3] Fixes --- .github/workflows/ShellCheck.yaml | 32 -------------------------- .markdownlint.yaml | 2 -- .pre-commit-config.yaml | 5 ++++ CONTRIBUTING.md | 38 ++++++++++++++++++++++--------- README.md | 3 ++- 5 files changed, 34 insertions(+), 46 deletions(-) delete mode 100644 .github/workflows/ShellCheck.yaml delete mode 100644 .markdownlint.yaml diff --git a/.github/workflows/ShellCheck.yaml b/.github/workflows/ShellCheck.yaml deleted file mode 100644 index 6c24bae..0000000 --- a/.github/workflows/ShellCheck.yaml +++ /dev/null @@ -1,32 +0,0 @@ -name: ShellCheck -on: - pull_request: - branches: ["*"] - push: - branches: - - master -jobs: - shellcheck: - name: ShellCheck - runs-on: ubuntu-latest - defaults: - run: - shell: bash - steps: - - name: Checkout Code - uses: actions/checkout@v4 - - name: Install ShellCheck - run: sudo apt update && sudo apt install shellcheck -y - - name: Run ShellCheck - run: | - shellcheck *.sh - if [ $? -ne 0 ]; then - echo "ShellCheck Detected Errors!!!" - exit 1 - fi - - name: Install shfmt - run: | - sudo apt install shfmt -y - - name: Format Shell Scripts - run: | - find . -name "*.sh" -exec shfmt -w -i 2 {} \; diff --git a/.markdownlint.yaml b/.markdownlint.yaml deleted file mode 100644 index 0043103..0000000 --- a/.markdownlint.yaml +++ /dev/null @@ -1,2 +0,0 @@ -MD013: - line_length: 999 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ac9b2b5..4499c8a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,6 +33,11 @@ repos: args: [--autofix] - id: requirements-txt-fixer - id: trailing-whitespace + # Shell formatting with shfmt + - repo: https://github.com/scop/pre-commit-shfmt + rev: v3.10.0-1 + hooks: + - id: shfmt # ShellCheck hook for linting shell scripts - repo: https://github.com/shellcheck-py/shellcheck-py rev: "v0.10.0.1" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a10e6b4..637475b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,26 +16,42 @@ Please follow these guidelines to ensure a smooth process. ### General Guidelines -- **Indentation**: Use 4 spaces for indentation in all code files. Avoid using tabs. +- **Indentation**: Use 4 spaces for indentation in all code files. +Avoid using tabs. - **Line Length**: Limit lines to improve readability. -- **Naming Conventions**: Use meaningful variable and function names. Variables should be written in lowercase with underscores separating words (e.g., `my_variable`), while function names should use lowercase with underscores (e.g., `my_function`). +- **Naming Conventions**: Use meaningful variable and function names. +Variables should be written in lowercase with underscores separating words +(e.g., `my_variable`), while function names should use lowercase with +underscores (e.g., `my_function`). ### Bash Scripts -- **Indentation**: Use 4 spaces for indentation. Ensure consistent indentation throughout the script. -- **POSIX Compliance**: Write scripts to be POSIX-compliant whenever possible to ensure compatibility across different Unix-like systems. Avoid using non-standard features or extensions specific to a particular shell. -- **Quoting**: Always quote variable expansions (e.g., `"${variable}"`) to prevent issues with spaces or special characters. -- **Shebang**: Use `#!/bin/sh` for POSIX-compliant scripts. If specific shell features are needed, document the specific shell requirement in the script header. -- **Function Definitions**: Define functions without `function` keyword (e.g., `my_function() { ... }`). Consistency is key. -- **Comments**: Use comments to explain complex logic or provide context. Keep comments concise and relevant. +- **Indentation**: Use 4 spaces for indentation. +Ensure consistent indentation throughout the script. +- **POSIX Compliance**: Write scripts to be POSIX-compliant +whenever possible to ensure compatibility across different Unix-like systems. +Avoid using non-standard features or extensions specific to a particular shell. +- **Quoting**: Always quote variable expansions (e.g., `"${variable}"`) +to prevent issues with spaces or special characters. +- **Shebang**: Use `#!/bin/sh` for POSIX-compliant scripts. +If specific shellfeatures are needed, document the specific shell +requirement in the script header. +- **Function Definitions**: Define functions without `function` keyword +(e.g., `my_function() { ... }`). Consistency is key. +- **Comments**: Use comments to explain complex logic or provide context. +Keep comments concise and relevant. - **Error Handling**: Include error handling for critical operations. -- **External Commands**: Prefer using built-in commands and utilities when possible. Minimize the use of external commands to improve script portability and performance. +- **External Commands**: Prefer using built-in commands and +utilities when possible. Minimize the use of external commands +to improve script portability and performance. ## Reporting Bugs -Please report bugs using the GitHub issue tracker. Include steps to reproduce the bug and +Please report bugs using the GitHub issue tracker. +Include steps to reproduce the bug and any relevant error messages. ## Contact -If you have questions or need further assistance, feel free to contact us at [Telegram](https://t.me/gvatsal60). +If you have questions or need further assistance, feel free to +contact us at [Telegram](https://t.me/gvatsal60). diff --git a/README.md b/README.md index a3d5d58..79f7b6c 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # Linux-Aliases 🖥️ [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://img.shields.io/github/license/gvatsal60/Linux-Aliases) -[![build status](https://github.com/gvatsal60/Linux-Aliases/actions/workflows/ShellCheck.yaml/badge.svg)](https://github.com/gvatsal60/Linux-Aliases/actions/workflows/ShellCheck.yaml) +[![Markdown Lint](https://github.com/gvatsal60/Linux-Aliases/actions/workflows/readme-checker.yaml/badge.svg)](https://github.com/gvatsal60/Linux-Aliases/actions/workflows/readme-checker.yaml) [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/gvatsal60/Linux-Aliases/master.svg)](https://results.pre-commit.ci/latest/github/gvatsal60/Linux-Aliases/HEAD) +[![CodeFactor](https://www.codefactor.io/repository/github/gvatsal60/linux-aliases/badge)](https://www.codefactor.io/repository/github/gvatsal60/linux-aliases) [![Maintenance](https://img.shields.io/badge/Maintained%3F-Yes-green.svg)](https://GitHub.com/gvatsal60/Linux-Aliases/graphs/commit-activity) ![GitHub pull-requests](https://img.shields.io/github/issues-pr/gvatsal60/Linux-Aliases) ![GitHub Issues](https://img.shields.io/github/issues/gvatsal60/Linux-Aliases) From 4f6d6c43f53709c0ac8be91498283d1e54e87ccf Mon Sep 17 00:00:00 2001 From: Vatsal Gupta Date: Sat, 23 Nov 2024 10:16:30 +0530 Subject: [PATCH 2/3] Fix Mounts --- .devcontainer/devcontainer.json | 2 +- .devcontainer/postCreateScript.sh | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 81c5fb7..fa161a9 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -59,7 +59,7 @@ }, "image": "mcr.microsoft.com/devcontainers/base:noble", "mounts": [ - "source=${localWorkspaceFolder:-.}/snippets,target=${containerWorkspaceFolder}/.vscode,type=bind,consistency=cached,readonly" + "source=${localWorkspaceFolder}/snippets,target=${containerWorkspaceFolder}/.vscode,type=bind,consistency=cached" ], "postCreateCommand": "chmod +x ${containerWorkspaceFolder}/.devcontainer/postCreateScript.sh && ${containerWorkspaceFolder}/.devcontainer/postCreateScript.sh", "runArgs": [ diff --git a/.devcontainer/postCreateScript.sh b/.devcontainer/postCreateScript.sh index e2e6afc..c69592d 100755 --- a/.devcontainer/postCreateScript.sh +++ b/.devcontainer/postCreateScript.sh @@ -27,6 +27,9 @@ ################################################################################################### # Global Variables & Constants ################################################################################################### +readonly SNIPPETS_DIR="snippets" +readonly VSCODE_DIR=".vscode" +readonly SNIPPET_FILES_PATTERN="*.code-snippets" readonly ALIAS_FILE_LINK="https://raw.githubusercontent.com/gvatsal60/Linux-Aliases/HEAD/install.sh" @@ -46,3 +49,22 @@ else echo "Error: curl is not installed. Unable to use Linux aliases" exit 1 fi + +# As bind mounts not supported in GitHub Codespaces +if [ -n "${CODESPACE_NAME}" ]; then + # Generate symlinks for snippet files + # Create the .vscode directory if it doesn't already exist. + mkdir -p "${VSCODE_DIR}" + # Unlink all symbolic links in the '.vscode' directory + find "${VSCODE_DIR}" -type l -name "${SNIPPET_FILES_PATTERN}" | while read -r file; do + unlink "${file}" + done + # Check if the 'snippets' directory exists + if [ -d "${SNIPPETS_DIR}" ]; then + # Find all .code-snippet files in the 'snippets' directory and create symbolic links + find "${SNIPPETS_DIR}" -type f -name "${SNIPPET_FILES_PATTERN}" | while read -r file; do + # Create a symbolic link in '.vscode' with the same base name + ln -s "$(realpath "${file}")" "${VSCODE_DIR}/$(basename "${file}")" + done + fi +fi \ No newline at end of file From a7c3186cb1c9578b0f5260dcdadd80abf69fdb46 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Nov 2024 04:47:03 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .aliases.sh | 4 +- .devcontainer/postCreateScript.sh | 38 +++--- install.sh | 184 +++++++++++++++--------------- 3 files changed, 113 insertions(+), 113 deletions(-) diff --git a/.aliases.sh b/.aliases.sh index f7c909d..5dc3f45 100755 --- a/.aliases.sh +++ b/.aliases.sh @@ -40,7 +40,7 @@ # - Redirects error output (if any) to /dev/null to maintain a clean output. # parse_git_branch() { - git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p' + git branch 2>/dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p' } # Function: gsquash @@ -58,7 +58,7 @@ parse_git_branch() { # Make sure you are on the branch where you want to squash commits before running this command. # gsquash() { - git rebase -i HEAD~"${1}" + git rebase -i HEAD~"${1}" } ################################################################################################### diff --git a/.devcontainer/postCreateScript.sh b/.devcontainer/postCreateScript.sh index c69592d..baecdad 100755 --- a/.devcontainer/postCreateScript.sh +++ b/.devcontainer/postCreateScript.sh @@ -44,27 +44,27 @@ readonly ALIAS_FILE_LINK="https://raw.githubusercontent.com/gvatsal60/Linux-Alia # Install Linux aliases from external script using curl and execute immediately # Note: Make sure to review scripts fetched from external sources for security reasons if command -v curl >/dev/null 2>&1; then - curl -fsSL ${ALIAS_FILE_LINK} | sh + curl -fsSL ${ALIAS_FILE_LINK} | sh else - echo "Error: curl is not installed. Unable to use Linux aliases" - exit 1 + echo "Error: curl is not installed. Unable to use Linux aliases" + exit 1 fi # As bind mounts not supported in GitHub Codespaces if [ -n "${CODESPACE_NAME}" ]; then - # Generate symlinks for snippet files - # Create the .vscode directory if it doesn't already exist. - mkdir -p "${VSCODE_DIR}" - # Unlink all symbolic links in the '.vscode' directory - find "${VSCODE_DIR}" -type l -name "${SNIPPET_FILES_PATTERN}" | while read -r file; do - unlink "${file}" - done - # Check if the 'snippets' directory exists - if [ -d "${SNIPPETS_DIR}" ]; then - # Find all .code-snippet files in the 'snippets' directory and create symbolic links - find "${SNIPPETS_DIR}" -type f -name "${SNIPPET_FILES_PATTERN}" | while read -r file; do - # Create a symbolic link in '.vscode' with the same base name - ln -s "$(realpath "${file}")" "${VSCODE_DIR}/$(basename "${file}")" - done - fi -fi \ No newline at end of file + # Generate symlinks for snippet files + # Create the .vscode directory if it doesn't already exist. + mkdir -p "${VSCODE_DIR}" + # Unlink all symbolic links in the '.vscode' directory + find "${VSCODE_DIR}" -type l -name "${SNIPPET_FILES_PATTERN}" | while read -r file; do + unlink "${file}" + done + # Check if the 'snippets' directory exists + if [ -d "${SNIPPETS_DIR}" ]; then + # Find all .code-snippet files in the 'snippets' directory and create symbolic links + find "${SNIPPETS_DIR}" -type f -name "${SNIPPET_FILES_PATTERN}" | while read -r file; do + # Create a symbolic link in '.vscode' with the same base name + ln -s "$(realpath "${file}")" "${VSCODE_DIR}/$(basename "${file}")" + done + fi +fi diff --git a/install.sh b/install.sh index e4386d4..c180efe 100755 --- a/install.sh +++ b/install.sh @@ -28,7 +28,7 @@ readonly ALIAS_SOURCE_URL="https://raw.githubusercontent.com/gvatsal60/Linux-Ali readonly ALIAS_SEARCH_STR="\. \"${FILE_PATH}\"" ALIAS_SOURCE_STR=$( - cat </dev/null + printf "%s\n" "$*" 2>/dev/null } # Function: print_err # Description: Prints an error message to the console, followed by a newline. # Usage: print_err "Your error message here" print_err() { - printf "%s\n" "$*" >&2 + printf "%s\n" "$*" >&2 } # Function: update_rc # Description: Update shell configuration files update_rc() { - _rc="" - case ${ADJUSTED_ID} in - debian | rhel) - _rc="${HOME}/.bashrc" - ;; - alpine | arch) - _rc="${HOME}/.profile" - ;; - darwin) - _rc="${HOME}/.zshrc" - ;; - *) - print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}" - exit 1 - ;; - esac - - # Check if ".aliases.sh" is already sourced, if not then append it - if [ -f "${_rc}" ]; then - if ! grep -qsE "${ALIAS_SEARCH_STR}" "${_rc}"; then - println "=> Updating ${_rc} for ${ADJUSTED_ID}..." - # Append the sourcing block to the RC file - println "${ALIAS_SOURCE_STR}" >>"${_rc}" - fi - else - # Notify if the rc file does not exist - println "=> Profile not found. ${_rc} does not exist." - println "=> Creating the file ${_rc}... Please note that this may not work as expected." - # Create the rc file - touch "${_rc}" - # Append the sourcing block to the newly created rc file - println "${ALIAS_SOURCE_STR}" >>"${_rc}" - fi - - println "" - println "=> Close and reopen your terminal to start using aliases" - println " OR" - println "=> Run the following to use it now:" - println ">>> source ${_rc} # This loads aliases" + _rc="" + case ${ADJUSTED_ID} in + debian | rhel) + _rc="${HOME}/.bashrc" + ;; + alpine | arch) + _rc="${HOME}/.profile" + ;; + darwin) + _rc="${HOME}/.zshrc" + ;; + *) + print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}" + exit 1 + ;; + esac + + # Check if ".aliases.sh" is already sourced, if not then append it + if [ -f "${_rc}" ]; then + if ! grep -qsE "${ALIAS_SEARCH_STR}" "${_rc}"; then + println "=> Updating ${_rc} for ${ADJUSTED_ID}..." + # Append the sourcing block to the RC file + println "${ALIAS_SOURCE_STR}" >>"${_rc}" + fi + else + # Notify if the rc file does not exist + println "=> Profile not found. ${_rc} does not exist." + println "=> Creating the file ${_rc}... Please note that this may not work as expected." + # Create the rc file + touch "${_rc}" + # Append the sourcing block to the newly created rc file + println "${ALIAS_SOURCE_STR}" >>"${_rc}" + fi + + println "" + println "=> Close and reopen your terminal to start using aliases" + println " OR" + println "=> Run the following to use it now:" + println ">>> source ${_rc} # This loads aliases" } # Function: dw_file # Description: Download file using wget or curl if available dw_file() { - # Check if curl is available - if command -v curl >/dev/null 2>&1; then - curl -fsSL -o "${FILE_PATH}" ${ALIAS_SOURCE_URL} - # Check if wget is available - elif command -v wget >/dev/null 2>&1; then - wget -O "${FILE_PATH}" ${ALIAS_SOURCE_URL} - else - print_err "Error: Either install wget or curl" - exit 1 - fi + # Check if curl is available + if command -v curl >/dev/null 2>&1; then + curl -fsSL -o "${FILE_PATH}" ${ALIAS_SOURCE_URL} + # Check if wget is available + elif command -v wget >/dev/null 2>&1; then + wget -O "${FILE_PATH}" ${ALIAS_SOURCE_URL} + else + print_err "Error: Either install wget or curl" + exit 1 + fi } ################################################################################################### @@ -122,31 +122,31 @@ OS=$(uname) case ${OS} in Darwin) - ADJUSTED_ID="darwin" - ;; + ADJUSTED_ID="darwin" + ;; Linux) - # Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME - # shellcheck source=/dev/null - . /etc/os-release - - # Get an adjusted ID independent of distro variants - if [ "${ID}" = "debian" ] || [ "${ID_LIKE#*debian}" != "${ID_LIKE}" ]; then - ADJUSTED_ID="debian" - elif [ "${ID}" = "arch" ] || [ "${ID_LIKE#*arch}" != "${ID_LIKE}" ]; then - ADJUSTED_ID="arch" - elif [ "${ID}" = "rhel" ] || [ "${ID}" = "fedora" ] || [ "${ID}" = "mariner" ] || [ "${ID_LIKE#*rhel}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*fedora}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*mariner}" != "${ID_LIKE}" ]; then - ADJUSTED_ID="rhel" - elif [ "${ID}" = "alpine" ]; then - ADJUSTED_ID="alpine" - else - print_err "Error: Linux distro ${ID} not supported." - exit 1 - fi - ;; + # Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME + # shellcheck source=/dev/null + . /etc/os-release + + # Get an adjusted ID independent of distro variants + if [ "${ID}" = "debian" ] || [ "${ID_LIKE#*debian}" != "${ID_LIKE}" ]; then + ADJUSTED_ID="debian" + elif [ "${ID}" = "arch" ] || [ "${ID_LIKE#*arch}" != "${ID_LIKE}" ]; then + ADJUSTED_ID="arch" + elif [ "${ID}" = "rhel" ] || [ "${ID}" = "fedora" ] || [ "${ID}" = "mariner" ] || [ "${ID_LIKE#*rhel}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*fedora}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*mariner}" != "${ID_LIKE}" ]; then + ADJUSTED_ID="rhel" + elif [ "${ID}" = "alpine" ]; then + ADJUSTED_ID="alpine" + else + print_err "Error: Linux distro ${ID} not supported." + exit 1 + fi + ;; *) - print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}" - exit 1 - ;; + print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}" + exit 1 + ;; esac # Default behavior @@ -154,26 +154,26 @@ _action="y" # Check if the script is running in interactive mode, for non-interactive mode `_action` defaults to 'y' if [ -t 0 ]; then - # Interactive mode - if [ -f "${FILE_PATH}" ]; then - println "=> File already exists: ${FILE_PATH}" - println "=> Do you want to replace it (default: y)? [y/n]: " - # Read input, use default value if no input is given - read -r _rp_conf - _rp_conf="${_rp_conf:-${_action}}" - _action="${_rp_conf}" - fi + # Interactive mode + if [ -f "${FILE_PATH}" ]; then + println "=> File already exists: ${FILE_PATH}" + println "=> Do you want to replace it (default: y)? [y/n]: " + # Read input, use default value if no input is given + read -r _rp_conf + _rp_conf="${_rp_conf:-${_action}}" + _action="${_rp_conf}" + fi fi if [ "${_action}" = "y" ]; then - println "=> Updating the file: ${FILE_PATH}" - # Download the necessary file from the specified source - dw_file - # Update the configuration file with the latest changes - update_rc + println "=> Updating the file: ${FILE_PATH}" + # Download the necessary file from the specified source + dw_file + # Update the configuration file with the latest changes + update_rc elif [ "${_action}" = "n" ]; then - println "=> Keeping existing file: ${FILE_PATH}" + println "=> Keeping existing file: ${FILE_PATH}" else - print_err "Error: Invalid input. Please check your entry and try again." - exit 1 + print_err "Error: Invalid input. Please check your entry and try again." + exit 1 fi