From 6dff639969a1b7c456c04c87b0145efa5c7e6f26 Mon Sep 17 00:00:00 2001 From: Mike Privette <51706217+mikeprivette@users.noreply.github.com> Date: Tue, 20 Feb 2024 10:41:34 +0000 Subject: [PATCH] Updates - README.md - added instructions to make sure the setup.sh script was executable as this was not explicitly stated - setup.sh - updated sed to use `sed -i` to be compatible with Linux, MacOSX and other OS versions and added a check in the local directory taht setup.sh executes in for a pyproject.toml file because the script was looking for the .toml file in the user's home directory and throwing an error --- README.md | 16 +++++++++++----- setup.sh | 50 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a157797..77103c3 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,13 @@ git clone https://github.com/danielmiessler/fabric.git cd fabric ``` -4. Install poetry +4. Ensure the `setup.sh` script is executable. If you're not sure, you can make it executable by running the following command: + +```bash +chmod +x setup.sh +``` + +5. Install poetry ref.: https://python-poetry.org/docs/#installing-with-the-official-installer @@ -154,7 +160,7 @@ ref.: https://python-poetry.org/docs/#installing-with-the-official-installer curl -sSL https://install.python-poetry.org | python3 - ``` -5. Run the `setup.sh`, which will do the following: +6. Run the `setup.sh`, which will do the following: - Installs python dependencies. - Creates aliases in your OS. It should update `~/.bashrc`, `/.zshrc`, and `~/.bash_profile` if they are present in your file system. @@ -162,9 +168,9 @@ curl -sSL https://install.python-poetry.org | python3 - ./setup.sh ``` -6. Restart your shell to reload everything. +7. Restart your shell to reload everything. -7. Set your `OPENAI_API_KEY`. +8. Set your `OPENAI_API_KEY`. ```bash fabric --setup @@ -172,7 +178,7 @@ fabric --setup You'll be asked to enter your OpenAI API key, which will be written to `~/.config/fabric/.env`. Patterns will then be downloaded from Github, which will take a few moments. -8. Now you are up and running! You can test by pulling the help. +9. Now you are up and running! You can test by pulling the help. ```bash # Making sure the paths are set up correctly diff --git a/setup.sh b/setup.sh index acfb5c3..999caca 100755 --- a/setup.sh +++ b/setup.sh @@ -1,5 +1,12 @@ #!/bin/bash +# Check if pyproject.toml exists in the current directory +if [ ! -f "pyproject.toml" ]; then + echo "Poetry could not find a pyproject.toml file in the current directory or its parents." + echo "Please navigate to the project directory where pyproject.toml is located and rerun this script." + exit 1 +fi + # Installs poetry-based python dependencies echo "Installing python dependencies" poetry install @@ -8,10 +15,10 @@ poetry install commands=("fabric" "fabric-api" "fabric-webui") # List of shell configuration files to update -config_files=(~/.bashrc ~/.zshrc ~/.bash_profile) +config_files=("$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile") -# Initialize an empty string to hold the path of the sourced file -source_command="" +# Initialize an array to hold the paths of the sourced files +source_commands=() for config_file in "${config_files[@]}"; do # Check if the configuration file exists @@ -19,30 +26,45 @@ for config_file in "${config_files[@]}"; do echo "Updating $config_file" for cmd in "${commands[@]}"; do # Get the path of the command - CMD_PATH=$(poetry run which $cmd) + CMD_PATH=$(poetry run which $cmd 2>/dev/null) + + # Check if CMD_PATH is empty + if [ -z "$CMD_PATH" ]; then + echo "Command $cmd not found in the current Poetry environment." + continue + fi # Check if the config file contains an alias for the command - if grep -q "alias $cmd=" "$config_file"; then - # Replace the existing alias with the new one - sed -i "/alias $cmd=/c\alias $cmd='$CMD_PATH'" "$config_file" + if grep -qE "alias $cmd=|alias $cmd =" "$config_file"; then + # Compatibility with GNU and BSD sed: Check for operating system and apply appropriate sed syntax + if [[ "$OSTYPE" == "darwin"* ]]; then + # BSD sed (macOS) + sed -i '' "/alias $cmd=/c\\ +alias $cmd='$CMD_PATH'" "$config_file" + else + # GNU sed (Linux and others) + sed -i "/alias $cmd=/c\alias $cmd='$CMD_PATH'" "$config_file" + fi echo "Updated alias for $cmd in $config_file." else # If not, add the alias to the config file - echo -e "\nalias $cmd='$CMD_PATH'" >> "$config_file" + echo -e "\nalias $cmd='$CMD_PATH'" >>"$config_file" echo "Added alias for $cmd to $config_file." fi done - # Set source_command to source the updated file - source_command="source $config_file" + # Add to source_commands array + source_commands+=("$config_file") else echo "$config_file does not exist." fi done -# Provide instruction to source the updated file -if [ ! -z "$source_command" ]; then - echo "To apply the changes, please run the following command in your terminal:" - echo "$source_command" +# Provide instruction to source the updated files +if [ ${#source_commands[@]} -ne 0 ]; then + echo "To apply the changes, please run the following command(s) in your terminal:" + for file in "${source_commands[@]}"; do + echo "source $file" + done else echo "No configuration files were updated. No need to source." fi