diff --git a/devbox/cli/devbox b/devbox/cli/devbox index 8387482..3514635 100755 --- a/devbox/cli/devbox +++ b/devbox/cli/devbox @@ -38,21 +38,20 @@ lower() { exit_with_message() { local message="$1" local code="${2:-1}" - echo + echo echo "============================================================================" - echo + echo if [[ $code -eq 0 ]]; then echo "[INFO] $message" else echo "[ERROR] $message" >&2 fi - echo + echo echo "============================================================================" echo exit $code } - detect_os() { if [[ "$OSTYPE" == "darwin"* ]]; then echo "darwin" @@ -65,7 +64,6 @@ detect_os() { fi } - # Add a key-value pair to the args array add_arg() { local key="$1" @@ -91,7 +89,6 @@ get_arg() { return 1 } - devbox_init_guidance() { printf "Welcome to DevBox CLI!\n\n" # if $1 is empty, then ask user select a choice or match user input $1 action to be product_id @@ -101,10 +98,10 @@ devbox_init_guidance() { printf " 1. Use Freeleaps.com repository\n" printf " 2. Use custom repository\n" read -p "Enter your choice (1 or 2): " choice - else + else log_info "Your will start with init product $1 develop environment" product_id=$1 - case "$product_id" in + case "$product_id" in freeleaps) choice=1 ;; @@ -113,78 +110,78 @@ devbox_init_guidance() { ;; esac fi - + case "$choice" in - 1) - freeleaps_username="" - freeleaps_password="" - use_local_component=false - use_custom_repository="" - freeleaps_components="" - git_branch="main" + 1) + freeleaps_username="" + freeleaps_password="" + use_local_component=false + use_custom_repository="" + freeleaps_components="" + git_branch="main" - # Ask user for Freeleaps.com username and password - read -p "Enter your Freeleaps.com username: " freeleaps_username - read -s -p "Enter your Freeleaps.com password: " freeleaps_password - echo - read -p "Use local component dev environment? (y/n): " choose_local_component - if [[ $choose_local_component == "y" ]]; then - use_local_component=true - fi - - # Ask user for git branch - read -p "Enter git branch to checkout (default: main): " input_git_branch - if [[ -n $input_git_branch ]]; then - git_branch=$input_git_branch - fi + # Ask user for Freeleaps.com username and password + read -p "Enter your Freeleaps.com username: " freeleaps_username + read -s -p "Enter your Freeleaps.com password: " freeleaps_password + echo + read -p "Use local component dev environment? (y/n): " choose_local_component + if [[ $choose_local_component == "y" ]]; then + use_local_component=true + fi - add_arg "--freeleaps-username" "$freeleaps_username" - add_arg "--freeleaps-password" "$freeleaps_password" - add_arg "--use-local-component" "$use_local_component" - add_arg "--use-custom-repository" "$use_custom_repository" - add_arg "--git-branch" "$git_branch" - ;; - 2) - # Check if product_id is empty + # Ask user for git branch + read -p "Enter git branch to checkout (default: main): " input_git_branch + if [[ -n $input_git_branch ]]; then + git_branch=$input_git_branch + fi + + add_arg "--freeleaps-username" "$freeleaps_username" + add_arg "--freeleaps-password" "$freeleaps_password" + add_arg "--use-local-component" "$use_local_component" + add_arg "--use-custom-repository" "$use_custom_repository" + add_arg "--git-branch" "$git_branch" + ;; + 2) + # Check if product_id is empty + if [[ -z $product_id ]]; then + # Ask user for product_id + read -p "Enter your product_id: " product_id if [[ -z $product_id ]]; then - # Ask user for product_id - read -p "Enter your product_id: " product_id - if [[ -z $product_id ]]; then - exit_with_message "Product ID is required, please provide a valid product ID." 1 - fi + exit_with_message "Product ID is required, please provide a valid product ID." 1 fi + fi - # Ask user for Freeleaps.com username and password - read -p "Enter your Freeleaps.com username: " freeleaps_username - read -s -p "Enter your Freeleaps.com password: " freeleaps_password - echo - - # Ask user for git branch - read -p "Enter git branch to checkout (default: main): " git_branch - if [[ -z $git_branch ]]; then - git_branch="main" - fi - - add_arg "--freeleaps-username" "$freeleaps_username" - add_arg "--freeleaps-password" "$freeleaps_password" - add_arg "--git-branch" "$git_branch" - - ENCODING_FREELEAPS_USERNAME=$(url_encode "$freeleaps_username") - ENCODEING_FREELEAPS_PASSWORD=$(url_encode "$freeleaps_password") - use_custom_repository="https://$ENCODING_FREELEAPS_USERNAME:$ENCODEING_FREELEAPS_PASSWORD@gitea.freeleaps.mathmast.com/products/$product_id.git" + # Ask user for Freeleaps.com username and password + read -p "Enter your Freeleaps.com username: " freeleaps_username + read -s -p "Enter your Freeleaps.com password: " freeleaps_password + echo - # Test the repository connection - if ! git ls-remote "$use_custom_repository"; then - exit_with_message " Failed to connect to the repository. Please check your username and password." 1 - fi + # Ask user for git branch + read -p "Enter git branch to checkout (default: main): " git_branch + if [[ -z $git_branch ]]; then + git_branch="main" + fi - printf "Repository connection successfully.\n" + add_arg "--freeleaps-username" "$freeleaps_username" + add_arg "--freeleaps-password" "$freeleaps_password" + add_arg "--git-branch" "$git_branch" - add_arg "--use-custom-repository" "$use_custom_repository" - ;; - *) - exit_with_message "Invalid choice. Please enter 1 or 2." 1 - ;; + ENCODING_FREELEAPS_USERNAME=$(url_encode "$freeleaps_username") + ENCODEING_FREELEAPS_PASSWORD=$(url_encode "$freeleaps_password") + use_custom_repository="https://$ENCODING_FREELEAPS_USERNAME:$ENCODEING_FREELEAPS_PASSWORD@gitea.freeleaps.mathmast.com/products/$product_id.git" + + # Test the repository connection + if ! git ls-remote "$use_custom_repository"; then + exit_with_message " Failed to connect to the repository. Please check your username and password." 1 + fi + + printf "Repository connection successfully.\n" + + add_arg "--use-custom-repository" "$use_custom_repository" + ;; + *) + exit_with_message "Invalid choice. Please enter 1 or 2." 1 + ;; esac } @@ -231,7 +228,6 @@ devbox_usage() { echo } - # :command.usage devbox_init_usage() { if [[ -n $long_usage ]]; then @@ -440,13 +436,13 @@ inspect_args() { for key in $sorted_keys; do value="" # Find the value based on the key - for i in `seq 0 $((${#args_keys[@]} - 1))`; do + for i in $(seq 0 $((${#args_keys[@]} - 1))); do if [ "${args_keys[$i]}" = "$key" ]; then value="${args_values[$i]}" break fi done - + done else log_info "args: none" @@ -459,7 +455,7 @@ inspect_args() { log_info "deps:" for key in $sorted_keys; do value="" - for i in `seq 0 $((${#deps_keys[@]} - 1))`; do + for i in $(seq 0 $((${#deps_keys[@]} - 1))); do if [ "${deps_keys[$i]}" = "$key" ]; then value="${deps_values[$i]}" break @@ -481,7 +477,6 @@ inspect_args() { fi } - install_docker() { log_info "Installing Docker..." @@ -573,14 +568,14 @@ check_docker_running() { # if running on WSL, check for Docker socket if grep -qi microsoft /proc/version; then - log_info "Detected WSL environment. Verifying /var/run/docker.sock..." - if [ -S /var/run/docker.sock ]; then - log_info "Docker socket found. Docker should be available via Docker Desktop." - return 0 - else - log_error "Docker socket not found in WSL environment." - return 1 - fi + log_info "Detected WSL environment. Verifying /var/run/docker.sock..." + if [ -S /var/run/docker.sock ]; then + log_info "Docker socket found. Docker should be available via Docker Desktop." + return 0 + else + log_error "Docker socket not found in WSL environment." + return 1 + fi fi log_info "Docker is not running. Attempting to start it..." @@ -612,10 +607,10 @@ check_docker_running() { build_local_image() { local dockerfile_path="$1" log_info "[Build] use Dockerfile: $(grep '^FROM' "$dockerfile_path")" - + # Check if the image already exists docker rmi -f $devbox_full_image 2>/dev/null || true - + # Build the image if ! docker buildx build \ --platform linux/amd64 \ @@ -623,7 +618,7 @@ build_local_image() { --no-cache \ -t $devbox_full_image \ -f "$dockerfile_path" . 2>&1 | tee "$WORKING_HOME/build.log"; then - + exit_with_message " Image build failed, please check the log file: $WORKING_HOME/build.log" 1 fi @@ -633,21 +628,18 @@ build_local_image() { fi } - # used for repository username and password encoding url_encode() { - echo "$1" | sed 's/@/%40/g' + echo "$1" | sed 's/@/%40/g' } - - ############################################### # Initialize the development environment ############################################### init_compile_env() { -# Update for export environments [] -docker exec -i "$DEVBOX_NAME" bash <