Do away with automatic offline mode; refactor and simplify offline mechanisms.

This commit is contained in:
Marco Vermeulen
2015-12-29 23:10:46 +00:00
parent 878b1e5560
commit 189d8f5146
12 changed files with 78 additions and 119 deletions

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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 " "

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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."

View File

@@ -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."

View File

@@ -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."

View File

@@ -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,

View File

@@ -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())

View File

@@ -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)