diff --git a/sce.sh b/sce.sh index c476899..e214a91 100755 --- a/sce.sh +++ b/sce.sh @@ -99,104 +99,138 @@ 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.) + 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 - 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 - if [ $start_only_mongodb_container == 1 ] + + # 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 [ $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 + 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 - docker-compose up --build + 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 + 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