From 0d673084f65163cb8d99fdf06ef797f7168b50c4 Mon Sep 17 00:00:00 2001 From: hoethan231 Date: Wed, 7 Aug 2024 06:41:52 +0000 Subject: [PATCH 1/3] check for source before executing --- sce.sh | 186 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 96 insertions(+), 90 deletions(-) diff --git a/sce.sh b/sce.sh index c476899..d8d5bec 100755 --- a/sce.sh +++ b/sce.sh @@ -99,104 +99,110 @@ function contains_config { return 0 } -# Checks for valid command -if ! [[ -n "${VALID_COMMANDS[$1]}" ]]; then - print_usage -fi - -if [ $1 == "completion" ] -then - if [ -n "$FISH_VERSION" ]; then - # Fish shell detected - echo "function sce; bash $(pwd)/sce.sh \$argv; end" - exit 0 - fi - # For other shells (Bash, Zsh, etc.) - echo "# for the sce dev tool" - echo "alias sce=\"$(pwd)/sce.sh\"" - echo "" - exit 0 -fi - -if [ $1 == "create" ] -then - cat $SCE_COMMAND_DIRECTORY"create_user.txt" | docker exec -i sce-mongodb-dev mongosh --shell --norc --quiet - exit 0 -fi - -name="" -configPaths=() -missingPaths=() -start_only_mongodb_container=1 - -# Check for second parameter before proceeding -if [ -z "$2" ]; then - print_usage -fi - -# Every key must have a value as -n checks if value is non-empty string -if [[ -n "${VALID_REPOS[$2]}" ]]; then - name=${VALID_REPOS[$2]} - - if [ $name == "Quasar" ]; then - configPaths+=("config/config.json") - - elif [ $name == "Clark" ]; then - configPaths+=("src/config/config.json") - configPaths+=("api/config/config.json") - - elif [ $name == "Mongo" ]; then - start_only_mongodb_container=1 - name="Clark" - - elif [ $name == "SCE-discord-bot" ]; then - configPaths+=("config.json") +# executes script when called instead of sourced +if [ "${BASH_SOURCE[0]}" = "$0" ]; then + + # Checks for valid command + if ! [[ -n "${VALID_COMMANDS[$1]}" ]]; then + print_usage fi -else - print_usage -fi - -if [ $1 == "clone" ] -then - # clone with the SSH URL if the user wanted to - # if the third argument is absent or anything else - # just default to the HTTPS url - if [[ ! -z "$3" ]] && [[ $3 == "--ssh" ]] + + if [ $1 == "completion" ] then - git clone "$GITHUB_BASE_SSH_URL$name.git" - else - git clone "$GITHUB_BASE_HTTP_URL$name.git" + if [ -n "$FISH_VERSION" ]; then + # Fish shell detected + echo "function sce; bash $(pwd)/sce.sh \$argv; end" + exit 0 + fi + # For other shells (Bash, Zsh, etc.) + echo "# for the sce dev tool" + echo "alias sce=\"$(pwd)/sce.sh\"" + exit 0 fi - exit 0 -elif [ $1 == "link" ] -then - sce_run_location=$(pwd) - # remove sim link if it exists, ignore any stderr/stdout - rm "$SCE_COMMAND_DIRECTORY$name" &> /dev/null - ln -s "$sce_run_location" "$SCE_COMMAND_DIRECTORY$name" -elif [ $1 == "run" ] -then - REPO_LOCATION="$SCE_COMMAND_DIRECTORY$name" - if [ ! -d "$REPO_LOCATION" ] + + if [ $1 == "create" ] then - print_repo_not_found $name + cat $SCE_COMMAND_DIRECTORY"create_user.txt" | docker exec -i sce-mongodb-dev mongosh --shell --norc --quiet + exit 0 fi - cd $REPO_LOCATION - contains_config $configPaths - if [ $? -eq 1 ] - then - print_missing_config $REPO_LOCATION $missingPaths + + name="" + configPaths=() + missingPaths=() + start_only_mongodb_container=1 + + # Check for second parameter before proceeding + if [ -z "$2" ]; then + print_usage + fi + + # Every key must have a value as -n checks if value is non-empty string + if [[ -n "${VALID_REPOS[$2]}" ]]; then + name=${VALID_REPOS[$2]} + + if [ $name == "Quasar" ]; then + configPaths+=("config/config.json") + + elif [ $name == "Clark" ]; then + configPaths+=("src/config/config.json") + configPaths+=("api/config/config.json") + + elif [ $name == "Mongo" ]; then + start_only_mongodb_container=1 + name="Clark" + + elif [ $name == "SCE-discord-bot" ]; then + configPaths+=("config.json") + fi + else + print_usage fi - if [ $start_only_mongodb_container == 1 ] + + if [ $1 == "clone" ] then - docker-compose -f docker-compose.dev.yml up mongodb -d + # clone with the SSH URL if the user wanted to + # if the third argument is absent or anything else + # just default to the HTTPS url + if [[ ! -z "$3" ]] && [[ $3 == "--ssh" ]] + then + git clone "$GITHUB_BASE_SSH_URL$name.git" + else + git clone "$GITHUB_BASE_HTTP_URL$name.git" + fi exit 0 - fi - if [ $name == "SCE-discord-bot" ] + elif [ $1 == "link" ] then - docker-compose up --build + sce_run_location=$(pwd) + # remove sim link if it exists, ignore any stderr/stdout + rm "$SCE_COMMAND_DIRECTORY$name" &> /dev/null + ln -s "$sce_run_location" "$SCE_COMMAND_DIRECTORY$name" + elif [ $1 == "run" ] + then + REPO_LOCATION="$SCE_COMMAND_DIRECTORY$name" + if [ ! -d "$REPO_LOCATION" ] + then + print_repo_not_found $name + fi + cd $REPO_LOCATION + contains_config $configPaths + if [ $? -eq 1 ] + then + print_missing_config $REPO_LOCATION $missingPaths + fi + if [ $start_only_mongodb_container == 1 ] + then + docker-compose -f docker-compose.dev.yml up mongodb -d + exit 0 + fi + if [ $name == "SCE-discord-bot" ] + then + docker-compose up --build + exit 0 + fi + docker-compose -f docker-compose.dev.yml up --build exit 0 fi - docker-compose -f docker-compose.dev.yml up --build - exit 0 -fi + +else + echo "sourced" +fi \ No newline at end of file From 24b44282ac60cbad02a639c07b7210082817ead2 Mon Sep 17 00:00:00 2001 From: hoethan231 Date: Wed, 7 Aug 2024 07:05:34 +0000 Subject: [PATCH 2/3] update completion function to include sourcing --- sce.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sce.sh b/sce.sh index d8d5bec..0e159fc 100755 --- a/sce.sh +++ b/sce.sh @@ -115,8 +115,20 @@ if [ "${BASH_SOURCE[0]}" = "$0" ]; then exit 0 fi # For other shells (Bash, Zsh, etc.) - echo "# for the sce dev tool" - echo "alias sce=\"$(pwd)/sce.sh\"" + BASHRC_PATH="$HOME/.bashrc" + ALIAS_LINE="alias sce=\"$(pwd)/sce.sh\"" + SOURCE_LINE="source $(pwd)/sce.sh" + + # Check if alias line exists + if ! grep -Fxq "$ALIAS_LINE" "$BASHRC_PATH"; then + echo "# for the sce dev tool" >> "$BASHRC_PATH" + echo "$ALIAS_LINE" >> "$BASHRC_PATH" + fi + + # Check if source line exists + if ! grep -Fxq "$SOURCE_LINE" "$BASHRC_PATH"; then + echo "$SOURCE_LINE" >> "$BASHRC_PATH" + fi exit 0 fi From db69428a9f130ea00921f372573158f3595e7066 Mon Sep 17 00:00:00 2001 From: hoethan231 Date: Wed, 7 Aug 2024 21:48:58 +0000 Subject: [PATCH 3/3] implemenet autocomplete --- sce.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/sce.sh b/sce.sh index 0e159fc..e214a91 100755 --- a/sce.sh +++ b/sce.sh @@ -216,5 +216,21 @@ if [ "${BASH_SOURCE[0]}" = "$0" ]; then fi else - echo "sourced" + function _sce { + local CURRENT_WORD=${COMP_WORDS[COMP_CWORD]} + local KEYSTRING + #builds a string with the keys from the arrays and sorts it + case $COMP_CWORD in + 1) + KEYSTRING=$(echo "${!VALID_COMMANDS[@]}" | tr ' ' '\n' | sort -u) + ;; + 2) + KEYSTRING=$(echo "${!VALID_REPOS[@]}" | tr ' ' '\n' | sort -u) + ;; + esac + #filters keys with current word/char for autocomplete + COMPREPLY=( $(compgen -W "$KEYSTRING" -- $CURRENT_WORD) ) + } + + complete -F _sce sce fi \ No newline at end of file