From 189d8f51467d3d778eedea8b47e22fb71ff63f4e Mon Sep 17 00:00:00 2001 From: Marco Vermeulen Date: Tue, 29 Dec 2015 23:10:46 +0000 Subject: [PATCH] Do away with automatic offline mode; refactor and simplify offline mechanisms. --- src/main/bash/sdkman-broadcast.sh | 53 +++++++++++++------ src/main/bash/sdkman-common.sh | 22 +------- src/main/bash/sdkman-init.sh | 19 +------ src/main/bash/sdkman-list.sh | 2 +- src/main/bash/sdkman-main.sh | 11 ++-- src/main/bash/sdkman-offline.sh | 32 ++--------- src/main/bash/sdkman-selfupdate.sh | 2 +- src/test/cucumber/forced_offline_mode.feature | 15 +++--- ...de.feature => service_unavailable.feature} | 20 +++---- .../sdkman/env/SdkmanBashEnvBuilder.groovy | 7 --- .../sdkman/specs/CurrentCommandSpec.groovy | 1 - .../sdkman/steps/initialisation_steps.groovy | 13 ++--- 12 files changed, 78 insertions(+), 119 deletions(-) rename src/test/cucumber/{offline_mode.feature => service_unavailable.feature} (90%) diff --git a/src/main/bash/sdkman-broadcast.sh b/src/main/bash/sdkman-broadcast.sh index 53858023..a3ed6a4a 100644 --- a/src/main/bash/sdkman-broadcast.sh +++ b/src/main/bash/sdkman-broadcast.sh @@ -24,16 +24,10 @@ function __sdkman_broadcast { fi } -function sdkman_update_broadcast_or_force_offline { +function sdkman_update_broadcast_and_service_availability { BROADCAST_LIVE_ID=$(sdkman_determine_broadcast_id) - - sdkman_force_offline_on_proxy "$BROADCAST_LIVE_ID" - if [[ "$SDKMAN_FORCE_OFFLINE" == 'true' ]]; then BROADCAST_LIVE_ID=""; fi - - sdkman_display_online_availability - sdkman_determine_offline "$BROADCAST_LIVE_ID" - - sdkman_update_broadcast "$COMMAND" "$BROADCAST_LIVE_ID" + sdkman_set_availability "$BROADCAST_LIVE_ID" + sdkman_update_broadcast "$BROADCAST_LIVE_ID" } function sdkman_determine_broadcast_id { @@ -44,15 +38,44 @@ function sdkman_determine_broadcast_id { fi } -function sdkman_display_online_availability { - if [[ -z "$BROADCAST_LIVE_ID" && "$SDKMAN_ONLINE" == "true" && "$COMMAND" != "offline" ]]; then - echo "$OFFLINE_WARNING" +function sdkman_set_availability { + local broadcast_id="$1" + local detect_html="$(echo "$broadcast_id" | tr '[:upper:]' '[:lower:]' | grep 'html')" + if [[ -z "$broadcast_id" ]]; then + SDKMAN_AVAILABLE="false" + sdkman_display_offline_warning "$broadcast_id" + elif [[ -n "$detect_html" ]]; then + SDKMAN_AVAILABLE="false" + sdkman_display_proxy_warning + else + SDKMAN_AVAILABLE="true" fi } +function sdkman_display_offline_warning { + local broadcast_id="$1" + if [[ -z "$broadcast_id" && "$COMMAND" != "offline" && "$SDKMAN_FORCE_OFFLINE" != "true" ]]; then + echo "==== INTERNET NOT REACHABLE! ===============================" + echo "" + echo " Some functionality is disabled or only partially available." + echo " If this persists, please enable the offline mode:" + echo "" + echo " $ sdk offline enable" + echo "" + echo "============================================================" + echo "" + fi +} + +function sdkman_display_proxy_warning { + echo "==== PROXY DETECTED! =======================================" + echo "Please ensure you have open internet access to continue." + echo "============================================================" + echo "" +} + function sdkman_update_broadcast { - local command="$1" - local broadcast_live_id="$2" + local broadcast_live_id="$1" local broadcast_id_file="${SDKMAN_DIR}/var/broadcast_id" local broadcast_text_file="${SDKMAN_DIR}/var/broadcast" @@ -67,7 +90,7 @@ function sdkman_update_broadcast { BROADCAST_OLD_TEXT=$(cat "$broadcast_text_file"); fi - if [[ "${SDKMAN_AVAILABLE}" == "true" && "$broadcast_live_id" != "${broadcast_old_id}" && "$command" != "selfupdate" && "$command" != "flush" ]]; then + if [[ "${SDKMAN_AVAILABLE}" == "true" && "$broadcast_live_id" != "${broadcast_old_id}" && "$COMMAND" != "selfupdate" && "$COMMAND" != "flush" ]]; then mkdir -p "${SDKMAN_DIR}/var" echo "${broadcast_live_id}" > "$broadcast_id_file" diff --git a/src/main/bash/sdkman-common.sh b/src/main/bash/sdkman-common.sh index ec5b24c5..b5991a74 100644 --- a/src/main/bash/sdkman-common.sh +++ b/src/main/bash/sdkman-common.sh @@ -43,15 +43,14 @@ function sdkman_determine_version { VERSION="$version" elif [[ "${SDKMAN_AVAILABLE}" == "false" && -z "$version" && -L "${SDKMAN_CANDIDATES_DIR}/${CANDIDATE}/current" ]]; then - VERSION=$(readlink "${SDKMAN_CANDIDATES_DIR}/${CANDIDATE}/current" | sed "s!${SDKMAN_CANDIDATES_DIR}/${CANDIDATE}/!!g") elif [[ "${SDKMAN_AVAILABLE}" == "false" && -n "$version" ]]; then - echo "Stop! ${CANDIDATE} ${1} is not available in offline mode." + echo "Stop! ${CANDIDATE} ${1} is not available while offline." return 1 elif [[ "${SDKMAN_AVAILABLE}" == "false" && -z "$version" ]]; then - echo "${OFFLINE_MESSAGE}" + echo "This command is not available while offline." return 1 elif [[ "${SDKMAN_AVAILABLE}" == "true" && -z "$version" ]]; then @@ -77,23 +76,6 @@ function sdkman_determine_version { fi } -function __sdkman_default_environment_variables { - - if [ ! "$SDKMAN_FORCE_OFFLINE" ]; then - SDKMAN_FORCE_OFFLINE="false" - fi - - if [ ! "$SDKMAN_ONLINE" ]; then - SDKMAN_ONLINE="true" - fi - - if [[ "${SDKMAN_ONLINE}" == "false" || "${SDKMAN_FORCE_OFFLINE}" == "true" ]]; then - SDKMAN_AVAILABLE="false" - else - SDKMAN_AVAILABLE="true" - fi -} - function __sdkman_link_candidate_version { CANDIDATE="$1" VERSION="$2" diff --git a/src/main/bash/sdkman-init.sh b/src/main/bash/sdkman-init.sh index 02a36f99..dfb648f4 100644 --- a/src/main/bash/sdkman-init.sh +++ b/src/main/bash/sdkman-init.sh @@ -68,23 +68,6 @@ if ${cygwin} ; then [ -n "${CP}" ] && CP=$(cygpath --path --unix "${CP}") fi - -OFFLINE_WARNING=$( cat << EOF -==== WARNING =============================================== - -INTERNET NOT REACHABLE! - -Some functionality is disabled. If this persists, please -enable the offline mode: - - $ sdk offline enable - -============================================================ -EOF -) - -OFFLINE_MESSAGE="This command is not available in offline mode." - # fabricate list of candidates if [[ -f "${SDKMAN_DIR}/var/candidates" ]]; then SDKMAN_CANDIDATES_CSV=$(cat "${SDKMAN_DIR}/var/candidates") @@ -154,7 +137,7 @@ if [[ -f "$SDKMAN_VERSION_TOKEN" && -z "$(find "$SDKMAN_VERSION_TOKEN" -mmin +$( else SDKMAN_REMOTE_VERSION=$(curl -s "${SDKMAN_SERVICE}/app/version" --connect-timeout 1 --max-time 1) sdkman_force_offline_on_proxy "$SDKMAN_REMOTE_VERSION" - if [[ -z "$SDKMAN_REMOTE_VERSION" || "$SDKMAN_FORCE_OFFLINE" == 'true' ]]; then + if [[ -z "$SDKMAN_REMOTE_VERSION" ]]; then SDKMAN_REMOTE_VERSION="$SDKMAN_VERSION" else echo ${SDKMAN_REMOTE_VERSION} > "$SDKMAN_VERSION_TOKEN" diff --git a/src/main/bash/sdkman-list.sh b/src/main/bash/sdkman-list.sh index 54c102d3..53e91c43 100644 --- a/src/main/bash/sdkman-list.sh +++ b/src/main/bash/sdkman-list.sh @@ -31,7 +31,7 @@ function __sdkman_build_version_csv { function __sdkman_offline_list { echo "------------------------------------------------------------" - echo "Offline Mode: only showing installed ${CANDIDATE} versions" + echo "Offline: only showing installed ${CANDIDATE} versions" echo "------------------------------------------------------------" echo " " diff --git a/src/main/bash/sdkman-main.sh b/src/main/bash/sdkman-main.sh index ca23b54d..8953810a 100644 --- a/src/main/bash/sdkman-main.sh +++ b/src/main/bash/sdkman-main.sh @@ -49,11 +49,16 @@ function sdk { # # Various sanity checks and default settings # - __sdkman_default_environment_variables - mkdir -p "$SDKMAN_DIR" - sdkman_update_broadcast_or_force_offline + # Always presume internet availability + SDKMAN_AVAILABLE="true" + if [ -z "$SDKMAN_FORCE_OFFLINE" ]; then + SDKMAN_FORCE_OFFLINE="false" + fi + + # ...unless proven otherwise + sdkman_update_broadcast_and_service_availability # Load the sdkman config if it exists. if [ -f "${SDKMAN_DIR}/etc/config" ]; then diff --git a/src/main/bash/sdkman-offline.sh b/src/main/bash/sdkman-offline.sh index 4b2aad02..2b0a289c 100644 --- a/src/main/bash/sdkman-offline.sh +++ b/src/main/bash/sdkman-offline.sh @@ -17,37 +17,13 @@ # function __sdkman_offline { - if [[ "$1" == "enable" ]]; then + local mode="$1" + if [[ "$mode" == "enable" ]]; then SDKMAN_FORCE_OFFLINE="true" echo "Forced offline mode enabled." fi - if [[ "$1" == "disable" ]]; then + if [[ "$mode" == "disable" ]]; then SDKMAN_FORCE_OFFLINE="false" - SDKMAN_ONLINE="true" echo "Online mode re-enabled!" fi -} - -function sdkman_determine_offline { - local input="$1" - if [[ -z "$input" ]]; then - SDKMAN_ONLINE="false" - SDKMAN_AVAILABLE="false" - else - SDKMAN_ONLINE="true" - fi -} - -function sdkman_force_offline_on_proxy { - local response="$1" - local detect_html="$(echo "$response" | tr '[:upper:]' '[:lower:]' | grep 'html')" - if [[ -n "$detect_html" ]]; then - echo "SDKMAN can't reach the internet so going offline. Re-enable online with:" - echo "" - echo " $ sdk offline disable" - echo "" - SDKMAN_FORCE_OFFLINE="true" - else - SDKMAN_FORCE_OFFLINE="false" - fi -} +} \ No newline at end of file diff --git a/src/main/bash/sdkman-selfupdate.sh b/src/main/bash/sdkman-selfupdate.sh index 24ab889c..2452b96a 100644 --- a/src/main/bash/sdkman-selfupdate.sh +++ b/src/main/bash/sdkman-selfupdate.sh @@ -19,7 +19,7 @@ function __sdkman_selfupdate { SDKMAN_FORCE_SELFUPDATE="$1" if [[ "$SDKMAN_AVAILABLE" == "false" ]]; then - echo "$OFFLINE_MESSAGE" + echo "This command is not available while offline." elif [[ "$SDKMAN_REMOTE_VERSION" == "$SDKMAN_VERSION" && "$SDKMAN_FORCE_SELFUPDATE" != "force" ]]; then echo "No update available at this time." diff --git a/src/test/cucumber/forced_offline_mode.feature b/src/test/cucumber/forced_offline_mode.feature index a1d72fbc..98ace0a2 100644 --- a/src/test/cucumber/forced_offline_mode.feature +++ b/src/test/cucumber/forced_offline_mode.feature @@ -24,7 +24,8 @@ Feature: Forced Offline Mode Then I see "Forced offline mode enabled." And I do not see "INTERNET NOT REACHABLE!" When I enter "sdk install grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I do not see "INTERNET NOT REACHABLE!" + And I see "Stop! grails 2.1.0 is not available while offline." Scenario: Disable Offline Mode with internet reachable Given offline mode is enabled with reachable internet @@ -45,7 +46,7 @@ Feature: Forced Offline Mode Then I see "Online mode re-enabled!" When I enter "sdk install grails 2.1.0" Then I see "INTERNET NOT REACHABLE!" - And I see "Stop! grails 2.1.0 is not available in offline mode." + And I see "Stop! grails 2.1.0 is not available while offline." #broadcast Scenario: Recall a broadcast while Forced Offline @@ -70,7 +71,7 @@ Feature: Forced Offline Mode And an initialised environment And the system is bootstrapped When I enter "sdk list grails" - Then I see "Offline Mode: only showing installed grails versions" + Then I see "Offline: only showing installed grails versions" #use version Scenario: Use an uninstalled candidate version while Forced Offline @@ -80,7 +81,7 @@ Feature: Forced Offline Mode And an initialised environment And the system is bootstrapped When I enter "sdk use grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I see "Stop! grails 2.1.0 is not available while offline." #default version Scenario: Set the default to an uninstalled candidate version while Forced Offline @@ -89,7 +90,7 @@ Feature: Forced Offline Mode And an initialised environment And the system is bootstrapped When I enter "sdk default grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I see "Stop! grails 2.1.0 is not available while offline." #install command Scenario: Install a candidate version that is not installed while Forced Offline @@ -98,7 +99,7 @@ Feature: Forced Offline Mode And an initialised environment And the system is bootstrapped When I enter "sdk install grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I see "Stop! grails 2.1.0 is not available while offline." #uninstall command Scenario: Uninstall a candidate version while Forced Offline @@ -132,5 +133,5 @@ Feature: Forced Offline Mode And an initialised environment And the system is bootstrapped When I enter "sdk selfupdate" - Then I see "This command is not available in offline mode." + Then I see "This command is not available while offline." diff --git a/src/test/cucumber/offline_mode.feature b/src/test/cucumber/service_unavailable.feature similarity index 90% rename from src/test/cucumber/offline_mode.feature rename to src/test/cucumber/service_unavailable.feature index 2052aca4..ed22e269 100644 --- a/src/test/cucumber/offline_mode.feature +++ b/src/test/cucumber/service_unavailable.feature @@ -1,4 +1,4 @@ -Feature: Offline Mode +Feature: Service Unavailable Background: Given the internet is not reachable @@ -11,14 +11,14 @@ Feature: Offline Mode And the candidate "grails" version "1.3.9" is already installed but not default And the system is bootstrapped When I enter "sdk list grails" - Then I see "Offline Mode: only showing installed grails versions" + Then I see "Offline: only showing installed grails versions" And I see "> 2.1.0" And I see "* 1.3.9" Scenario: List candidate versions not found while Offline Given the system is bootstrapped When I enter "sdk list grails" - Then I see "Offline Mode: only showing installed grails versions" + Then I see "Offline: only showing installed grails versions" And I see "None installed!" # use command @@ -35,20 +35,20 @@ Feature: Offline Mode And the candidate "grails" version "2.1.0" is already installed but not default And the system is bootstrapped When I enter "sdk use grails" - Then I see "This command is not available in offline mode." + Then I see "This command is not available while offline." Scenario: Use an uninstalled candidate version while Offline Given the candidate "grails" version "1.3.9" is already installed and default And the candidate "grails" version "2.1.0" is not installed And the system is bootstrapped When I enter "sdk use grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I see "Stop! grails 2.1.0 is not available while offline." Scenario: Use an invalid candidate version while Offline Given the candidate "grails" version "1.3.9" is already installed and default And the system is bootstrapped When I enter "sdk use grails 9.9.9" - Then I see "Stop! grails 9.9.9 is not available in offline mode." + Then I see "Stop! grails 9.9.9 is not available while offline." Scenario: Use an installed candidate version while Offline Given the candidate "grails" version "2.1.0" is already installed and default @@ -63,13 +63,13 @@ Feature: Offline Mode Given the candidate "grails" version "1.3.9" is already installed and default And the system is bootstrapped When I enter "sdk default grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I see "Stop! grails 2.1.0 is not available while offline." Scenario: Set the default to an invalid candidate version while Offline Given the candidate "grails" version "1.3.9" is already installed and default And the system is bootstrapped When I enter "sdk default grails 999" - Then I see "Stop! grails 999 is not available in offline mode." + Then I see "Stop! grails 999 is not available while offline." Scenario: Set the default to an installed candidate version while Offline Given the candidate "grails" version "2.1.0" is already installed and default @@ -83,7 +83,7 @@ Feature: Offline Mode Given the candidate "grails" version "2.1.0" is not installed And the system is bootstrapped When I enter "sdk install grails 2.1.0" - Then I see "Stop! grails 2.1.0 is not available in offline mode." + Then I see "Stop! grails 2.1.0 is not available while offline." Scenario: Install a candidate version that is already installed while Offline Given the candidate "grails" version "2.1.0" is already installed and default @@ -146,5 +146,5 @@ Feature: Offline Mode Scenario: Attempt self-update while Offline Given the system is bootstrapped When I enter "sdk selfupdate" - Then I see "This command is not available in offline mode." + Then I see "This command is not available while offline." diff --git a/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy b/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy index 957d0566..8b3f526e 100644 --- a/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy +++ b/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy @@ -13,7 +13,6 @@ class SdkmanBashEnvBuilder { CurlStub curlStub List candidates = ['groovy', 'grails'] List availableCandidates = candidates - boolean onlineMode = true boolean forcedOfflineMode = false String broadcast = "This is a LIVE broadcast!" String service = "http://localhost:8080" @@ -62,11 +61,6 @@ class SdkmanBashEnvBuilder { this } - SdkmanBashEnvBuilder withOnlineMode(boolean onlineMode){ - this.onlineMode = onlineMode - this - } - SdkmanBashEnvBuilder withForcedOfflineMode(boolean forcedOfflineMode){ this.forcedOfflineMode = forcedOfflineMode this @@ -125,7 +119,6 @@ class SdkmanBashEnvBuilder { def env = [ SDKMAN_DIR: sdkmanDir.absolutePath, SDKMAN_CANDIDATES_DIR: sdkmanCandidatesDir.absolutePath, - SDKMAN_ONLINE: "$onlineMode", SDKMAN_FORCE_OFFLINE: "$forcedOfflineMode", SDKMAN_SERVICE: service, SDKMAN_BROADCAST_SERVICE: broadcastService, diff --git a/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy b/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy index b0fe8c4c..0e3ac3ee 100644 --- a/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy +++ b/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy @@ -35,7 +35,6 @@ class CurrentCommandSpec extends SdkmanEnvSpecification { curlStub.primeWith("http://localhost:8080/app/version", "echo x.y.z").build() bash = sdkmanBashEnvBuilder .withCurlStub(curlStub) - .withOnlineMode(true) .withForcedOfflineMode(false) .withAvailableCandidates(allCandidates) .withCandidates(installedCandidates.keySet().toList()) diff --git a/src/test/groovy/sdkman/steps/initialisation_steps.groovy b/src/test/groovy/sdkman/steps/initialisation_steps.groovy index 8e3e6d8c..e9f60f8b 100644 --- a/src/test/groovy/sdkman/steps/initialisation_steps.groovy +++ b/src/test/groovy/sdkman/steps/initialisation_steps.groovy @@ -39,46 +39,44 @@ And(~'^the internet is reachable$') {-> primeSelfupdate() forcedOffline = false - online = true serviceUrlEnv = SERVICE_UP_URL javaHome = FAKE_JDK_PATH } And(~'^the internet is not reachable$') {-> forcedOffline = false - online = false serviceUrlEnv = SERVICE_DOWN_URL javaHome = FAKE_JDK_PATH } And(~'^offline mode is disabled with reachable internet$') {-> - primeEndpointWithString("/broadcast/latest", "This is a LIVE Broadcast!") + primeEndpointWithString("/broadcast/latest/id", "12345") + primeEndpointWithString("/broadcast/latest", "broadcast message") + primeEndpointWithString("/app/version", sdkmanVersion) forcedOffline = false - online = true serviceUrlEnv = SERVICE_UP_URL javaHome = FAKE_JDK_PATH } And(~'^offline mode is enabled with reachable internet$') {-> primeEndpointWithString("/broadcast/latest/id", "12345") + primeEndpointWithString("/broadcast/latest", "broadcast message") + primeEndpointWithString("/app/version", sdkmanVersion) forcedOffline = true - online = true serviceUrlEnv = SERVICE_UP_URL javaHome = FAKE_JDK_PATH } And(~'^offline mode is enabled with unreachable internet$') {-> forcedOffline = true - online = false serviceUrlEnv = SERVICE_DOWN_URL javaHome = FAKE_JDK_PATH } And(~'^an initialised environment$') {-> bash = SdkmanBashEnvBuilder.create(sdkmanBaseDir) - .withOnlineMode(online) .withForcedOfflineMode(forcedOffline) .withService(serviceUrlEnv) .withBroadcastService(serviceUrlEnv) @@ -90,7 +88,6 @@ And(~'^an initialised environment$') {-> And(~'^an outdated initialised environment$') {-> bash = SdkmanBashEnvBuilder.create(sdkmanBaseDir) - .withOnlineMode(online) .withForcedOfflineMode(forcedOffline) .withService(serviceUrlEnv) .withBroadcastService(serviceUrlEnv)