From d791fd713f9941c73a990023f3db80d1514c3a58 Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Tue, 28 Nov 2023 08:31:54 +0100 Subject: [PATCH] update ontoportal script to handle local gems bindq --- bin/ontoportal | 133 +++++++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 53 deletions(-) diff --git a/bin/ontoportal b/bin/ontoportal index 573b49c7..38e508db 100755 --- a/bin/ontoportal +++ b/bin/ontoportal @@ -2,7 +2,7 @@ # Function to display script usage information show_help() { - echo "Usage: $0 {dev|test|run|help} [--reset-cache] [--api-url API_URL] [--api-key API_KEY]" + echo "Usage: $0 {dev|test|run|help} [--reset-cache] [--api-url API_URL] [--api-key API_KEY] [--old-path OLD_PATH] [--goo-path GOO_PATH] [--sparql-client-path SPARQL_CLIENT_PATH]" echo " dev : Start the Ontoportal API development server." echo " Example: $0 dev --api-url http://localhost:9393" echo " Use --reset-cache to remove volumes: $0 dev --reset-cache" @@ -15,13 +15,25 @@ show_help() { echo " application using Docker Compose. It includes options for starting the development server," echo " running tests, and executing commands within the Ontoportal API Docker container." echo + echo "Options:" + echo " --reset-cache : Remove Docker volumes (used with 'dev')." + echo " --api-url API_URL : Specify the API URL." + echo " --api-key API_KEY : Specify the API key." + echo " --old-path OLD_PATH : Specify the path for ontologies_linked_data." + echo " --goo-path GOO_PATH : Specify the path for goo." + echo " --sparql-client-path SPARQL_CLIENT_PATH : Specify the path for sparql-client." + echo echo "Goals:" echo " - Simplify common tasks related to Ontoportal API development using Docker." echo " - Provide a consistent and easy-to-use interface for common actions." } + # Function to update or create the .env file with API_URL and API_KEY update_env_file() { local api_url="$1" + local old_path="$2" + local goo_path="$3" + local sparql_client_path="$4" # Update the .env file with the provided values file_content=$(<.env) @@ -30,6 +42,12 @@ update_env_file() { while IFS= read -r line; do if [[ "$line" == "API_URL="* ]]; then echo "API_URL=$api_url" + elif [[ "$line" == "ONTOLOGIES_LINKED_DATA_PATH="* ]]; then + echo "ONTOLOGIES_LINKED_DATA_PATH=$old_path" + elif [[ "$line" == "GOO_PATH="* ]]; then + echo "ONTOLOGIES_LINKED_DATA_PATH=$goo_path" + elif [[ "$line" == "SPARQL_CLIENT_PATH="* ]]; then + echo "SPARQL_CLIENT_PATH=$sparql_client_path" else echo "$line" fi @@ -49,13 +67,41 @@ create_config_files() { fi } -# Function to handle the "dev" option -dev() { - echo "Starting Ontoportal API development server..." - - create_config_files +# Function to build Docker run command with conditionally added bind mounts +build_docker_run_cmd() { + local custom_command="$1" + local old_path="$2" + local goo_path="$3" + local sparql_client_path="$4" + + local docker_run_cmd="docker run --rm -it" + local bash_cmd="" + + # Conditionally add bind mounts only if the paths are not empty + for path_var in "old_path:ontologies_linked_data" "goo_path:goo" "sparql_client_path:sparql-client"; do + IFS=':' read -r path value <<< "$path_var" + if [ -n "${!path}" ]; then + echo "Run: bundle config local.$value ${!path}" + docker_run_cmd+=" -v ${!path}:/app/$value" + bash_cmd+="(bundle config local.$value /app/$value) &&" + else + bash_cmd+="(bundle config unset local.$value) &&" + fi + done + + bash_cmd+="(bundle check || bundle install) && $custom_command" + docker_run_cmd+=" --service-ports api bash -c \"$bash_cmd\"" + eval "$docker_run_cmd" +} + +# Function to handle the "dev" and "test" options +run_command() { + local custom_command="$1" local reset_cache=false local api_url="" + local old_path="" + local goo_path="" + local sparql_client_path="" # Check for command line arguments @@ -69,6 +115,18 @@ dev() { api_url="$2" shift 2 ;; + --old-path) + old_path="$2" + shift 2 + ;; + --goo-path) + goo_path="$2" + shift 2 + ;; + --sparql-client-path) + sparql_client_path="$2" + shift 2 + ;; *) echo "Unknown option: $1" show_help @@ -77,22 +135,6 @@ dev() { esac done - - - # Check if arguments are provided - if [ -n "$api_url" ] ; then - # If arguments are provided, update the .env file - update_env_file "$api_url" - else - # If no arguments, fetch values from the .env file - source .env - api_url="$API_URL" - fi - - if [ -z "$api_url" ] ; then - echo "Error: Missing required arguments. Please provide both --api-url or update them in your .env" - exit 1 - fi # Check if --reset-cache is present and execute docker compose down --volumes if [ "$reset_cache" = true ]; then @@ -100,43 +142,27 @@ dev() { docker compose down --volumes fi - echo "Run: bundle exec api s -b 0.0.0.0 -p 3000" - docker compose run --rm -it --service-ports api bash -c "(bundle check || bundle install) && bundle exec rackup -o 0.0.0.0 --port 9393" + # Display the updated environment variables + update_env_file "$api_url" "$old_path" "$goo_path" "$sparql_client_path" + + # Build the Docker run command + echo "Run: $custom_command" + build_docker_run_cmd "$custom_command" "$old_path" "$goo_path" "$sparql_client_path" } +# Function to handle the "dev" option +dev() { + echo "Starting OntoPortal API development server..." + + local custom_command="bundle exec api s -b 0.0.0.0 -p 3000" + run_command "$custom_command" +} # Function to handle the "test" option test() { - - - local api_url="" - local test_path="" - local test_options="" - - # Check for command line arguments - while [ "$#" -gt 0 ]; do - case "$1" in - --api-url) - shift - api_url="$1" - ;; - *) - if [ -z "$test_path" ]; then - test_path="$1" - else - test_options="$test_options $1" - fi - ;; - esac - shift - done - - - - script="API_URL=$api_url bundle exec rake test TEST=\"$test_path\" TESTOPTS=\"$test_options\"" echo "Running tests..." - echo "Run: $script" - docker compose run --rm -it api bash -c "(bundle check || bundle install) && $script" + local custom_command="API_URL=$api_url bundle exec rake test TEST=\"$test_path\" TESTOPTS=\"$test_options\"" + run_command "$custom_command" } # Function to handle the "run" option @@ -145,6 +171,7 @@ run() { docker compose run --rm -it api bash -c "$*" } +create_config_files # Main script logic case "$1" in "run")