Skip to content

Commit

Permalink
Strip out the broadcast id and rename to healthcheck where needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0der committed Mar 4, 2023
1 parent 010736e commit a89a8cc
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 40 deletions.
38 changes: 10 additions & 28 deletions src/main/bash/sdkman-availability.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@
#

function __sdkman_update_service_availability() {
local broadcast_live_id=$(__sdkman_determine_broadcast_id)
__sdkman_set_availability "$broadcast_live_id"
__sdkman_update_broadcast_id "$broadcast_live_id"
local healthcheck_status=$(__sdkman_determine_healthcheck_status)
__sdkman_set_availability "$healthcheck_status"
}

function __sdkman_determine_broadcast_id() {
function __sdkman_determine_healthcheck_status() {
if [[ "$SDKMAN_OFFLINE_MODE" == "true" || "$COMMAND" == "offline" && "$QUALIFIER" == "enable" ]]; then
echo ""
else
echo $(__sdkman_secure_curl_with_timeouts "${SDKMAN_CANDIDATES_API}/broadcast/latest/id")
echo $(__sdkman_secure_curl_with_timeouts "${SDKMAN_CANDIDATES_API}/healthcheck")
fi
}

function __sdkman_set_availability() {
local broadcast_id="$1"
local detect_html="$(echo "$broadcast_id" | tr '[:upper:]' '[:lower:]' | grep 'html')"
if [[ -z "$broadcast_id" ]]; then
local healthcheck_status="$1"
local detect_html="$(echo "$healthcheck_status" | tr '[:upper:]' '[:lower:]' | grep 'html')"
if [[ -z "$healthcheck_status" ]]; then
SDKMAN_AVAILABLE="false"
__sdkman_display_offline_warning "$broadcast_id"
__sdkman_display_offline_warning "$healthcheck_status"
elif [[ -n "$detect_html" ]]; then
SDKMAN_AVAILABLE="false"
__sdkman_display_proxy_warning
Expand All @@ -45,8 +44,8 @@ function __sdkman_set_availability() {
}

function __sdkman_display_offline_warning() {
local broadcast_id="$1"
if [[ -z "$broadcast_id" && "$COMMAND" != "offline" && "$SDKMAN_OFFLINE_MODE" != "true" ]]; then
local healthcheck_status="$1"
if [[ -z "$healthcheck_status" && "$COMMAND" != "offline" && "$SDKMAN_OFFLINE_MODE" != "true" ]]; then
__sdkman_echo_red "==== INTERNET NOT REACHABLE! ==================================================="
__sdkman_echo_red ""
__sdkman_echo_red " Some functionality is disabled or only partially available."
Expand All @@ -65,20 +64,3 @@ function __sdkman_display_proxy_warning() {
__sdkman_echo_red "================================================================================"
echo ""
}

function __sdkman_update_broadcast_id() {
local broadcast_live_id broadcast_id_file broadcast_old_id

broadcast_live_id="$1"
broadcast_id_file="${SDKMAN_DIR}/var/broadcast_id"
broadcast_old_id=""

if [[ -f "$broadcast_id_file" ]]; then
broadcast_old_id=$(< "$broadcast_id_file")
fi

if [[ "$SDKMAN_AVAILABLE" == "true" && "$broadcast_live_id" != "$broadcast_old_id" && "$COMMAND" != "selfupdate" && "$COMMAND" != "flush" ]]; then
mkdir -p "${SDKMAN_DIR}/var"
echo "$broadcast_live_id" | tee "$broadcast_id_file" > /dev/null
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class CandidatesCacheUpdateFailureSpec extends SdkmanEnvSpecification {

static final String CANDIDATES_API = "http://localhost:8080/2"

static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
static final String HEALTHCHECK_ENDPOINT = "$CANDIDATES_API/healthcheck"
static final String CANDIDATES_ALL_ENDPOINT = "$CANDIDATES_API/candidates/all"

File candidatesCache

def setup() {
candidatesCache = new File("${sdkmanDotDirectory}/var", "candidates")
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
curlStub.primeWith(HEALTHCHECK_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
.primeWith(CANDIDATES_ALL_ENDPOINT, "echo html")
sdkmanBashEnvBuilder.withConfiguration("sdkman_debug_mode", "true")
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/groovy/sdkman/specs/CandidatesCacheUpdateSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class CandidatesCacheUpdateSpec extends SdkmanEnvSpecification {

static final String CANDIDATES_API = "http://localhost:8080/2"

static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
static final String HEALTHCHECK_ENDPOINT = "$CANDIDATES_API/healthcheck"
static final String CANDIDATES_ALL_ENDPOINT = "$CANDIDATES_API/candidates/all"

File candidatesCache

def setup() {
candidatesCache = new File("${sdkmanDotDirectory}/var", "candidates")
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
curlStub.primeWith(HEALTHCHECK_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
.primeWith(CANDIDATES_ALL_ENDPOINT, "echo groovy,scala")
sdkmanBashEnvBuilder.withConfiguration("sdkman_debug_mode", "true")
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import static java.nio.file.Files.createSymbolicLink
class CurrentCommandSpec extends SdkmanEnvSpecification {

static final CANDIDATES_API = "http://localhost:8080/2"
static final BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
static final HEALTHCHECK_ENDPOINT = "$CANDIDATES_API/healthcheck"

def setup() {
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
curlStub.primeWith(HEALTHCHECK_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
}

void "should display current version of all candidates installed"() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/groovy/sdkman/specs/SelfupdateSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import static java.time.temporal.ChronoUnit.DAYS

class SelfupdateSpec extends SdkmanEnvSpecification {
static final String CANDIDATES_API = "http://localhost:8080/2"
static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
static final String HEALTHCHECK_ENDPOINT = "$CANDIDATES_API/healthcheck"
static final String VERSION_ENDPOINT = "$CANDIDATES_API/broker/download/sdkman/version/stable"

def setup() {
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
curlStub.primeWith(HEALTHCHECK_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
curlStub.primeWith(VERSION_ENDPOINT, "echo 5.0.0")
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/sdkman/steps/env.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ etcDir = "${sdkmanDirEnv}/etc" as File
extDir = "${sdkmanDirEnv}/ext" as File
tmpDir = "${sdkmanDir}/tmp" as File

broadcastIdFile = new File(varDir, "broadcast_id")
healthcheckFile = new File(varDir, "healthcheck")
candidatesFile = new File(varDir, "candidates")
versionFile = new File(varDir, "version")
initScript = new File(binDir, "sdkman-init.sh")
Expand Down
6 changes: 3 additions & 3 deletions src/test/groovy/sdkman/steps/initialisation_steps.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ And(~'^the archive for candidate "([^"]*)" version "([^"]*)" is removed$') { Str
}

And(~'^the internet is reachable$') { ->
primeEndpointWithString("/broadcast/latest/id", "12345")
primeEndpointWithString("/healthcheck", "12345")
primeEndpointWithString("/app/stable", sdkmanVersion)
primeSelfupdate()

Expand All @@ -49,7 +49,7 @@ And(~'^the internet is not reachable$') { ->
}

And(~'^offline mode is disabled with reachable internet$') { ->
primeEndpointWithString("/broadcast/latest/id", "12345")
primeEndpointWithString("/healthcheck", "12345")
primeEndpointWithString("/app/stable", sdkmanVersion)

offlineMode = false
Expand All @@ -58,7 +58,7 @@ And(~'^offline mode is disabled with reachable internet$') { ->
}

And(~'^offline mode is enabled with reachable internet$') { ->
primeEndpointWithString("/broadcast/latest/id", "12345")
primeEndpointWithString("/healthcheck", "12345")
primeEndpointWithString("/app/stable", sdkmanVersion)

offlineMode = true
Expand Down

0 comments on commit a89a8cc

Please sign in to comment.