Skip to content

Bypass download if CLI version matches #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,33 @@ class CoderCLIManager @JvmOverloads constructor(
)

/**
* Return the binary version or null if it could not be determined.
* Return the binary version.
*
* Throws if it could not be determined.
*/
fun version(): String? {
fun version(): CoderSemVer {
val raw = exec("version", "--output", "json")
val json = Gson().fromJson(raw, Version::class.java)
if (json?.version == null) {
throw InvalidVersionException("No version found in output")
}
return CoderSemVer.parse(json.version)
}

/**
* Returns true if the CLI has the same major/minor/patch version as the
* provided version and false if it does not match or the CLI version could
* not be determined or the provided version is invalid.
*/
fun matchesVersion(buildVersion: String): Boolean {
return try {
val raw = exec("version", "--output", "json")
val json = Gson().fromJson(raw, Version::class.java)
json.version
val cliVersion = version()
val matches = cliVersion == CoderSemVer.parse(buildVersion)
logger.info("$localBinaryPath version $cliVersion matches $buildVersion: $matches")
matches
} catch (e: Exception) {
logger.warn("Unable to determine CLI version: ${e.message}")
null
logger.info("Unable to determine $localBinaryPath version: ${e.message}")
false
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/coder/gateway/sdk/CoderSemVer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CoderSemVer(private val major: Long = 0, private val minor: Long = 0, priv


override fun toString(): String {
return "CoderSemVer(major=$major, minor=$minor)"
return "CoderSemVer(major=$major, minor=$minor, patch=$patch)"
}

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,9 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
// supported if the binary is downloaded from alternate sources.
// For CLIs without the JSON output flag we will fall back to
// the 304 method.
if (cliManager.version() != clientService.buildVersion) {
if (!cliManager.matchesVersion(clientService.buildVersion)) {
this.indicator.text = "Downloading Coder CLI..."
cliManager.downloadCLI()
} else {
logger.info("Found existing binary at ${cliManager.localBinaryPath} with the expected version ${clientService.buildVersion}")
}

this.indicator.text = "Authenticating Coder CLI..."
Expand Down
72 changes: 65 additions & 7 deletions src/test/groovy/CoderCLIManagerTest.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coder.gateway.sdk

import com.google.gson.JsonSyntaxException
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
Expand Down Expand Up @@ -131,10 +132,11 @@ class CoderCLIManagerTest extends Specification {

when:
def downloaded = ccm.downloadCLI()
ccm.version()

then:
downloaded
CoderSemVer.isValidVersion(ccm.version())
noExceptionThrown()

// Make sure login failures propagate correctly.
when:
Expand Down Expand Up @@ -443,11 +445,67 @@ class CoderCLIManagerTest extends Specification {

where:
contents | expected
"""echo '{"version": "1.0.0"}'""" | "1.0.0"
"""echo '{"version": "1.0.0", "foo": true, "baz": 1}'""" | "1.0.0"
"""echo '{"foo": true, "baz": 1}'""" | null
"""echo '{"version: "1.0.0", "foo": true, "baz": 1}'""" | null
"exit 0" | null
"exit 1" | null
"""echo '{"version": "1.0.0"}'""" | CoderSemVer.parse("1.0.0")
"""echo '{"version": "1.0.0", "foo": true, "baz": 1}'""" | CoderSemVer.parse("1.0.0")
}

@IgnoreIf({ os.windows })
def "fails to parse version"() {
given:
def ccm = new CoderCLIManager(new URL("https://test.coder.parse-fail.invalid"), tmpdir)
Files.createDirectories(ccm.localBinaryPath.parent)

when:
if (contents != null) {
ccm.localBinaryPath.toFile().text = "#!/bin/sh\n$contents"
ccm.localBinaryPath.toFile().setExecutable(true)
}
ccm.version()

then:
thrown(expected)

where:
contents | expected
null | ProcessInitException
"""echo '{"foo": true, "baz": 1}'""" | InvalidVersionException
"""echo '{"version: '""" | JsonSyntaxException
"exit 0" | InvalidVersionException
"exit 1" | InvalidExitValueException
Comment on lines +470 to +474
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

}

@IgnoreIf({ os.windows })
def "checks if version matches"() {
given:
def ccm = new CoderCLIManager(new URL("https://test.coder.version-matches.invalid"), tmpdir)
Files.createDirectories(ccm.localBinaryPath.parent)

when:
if (contents != null) {
ccm.localBinaryPath.toFile().text = "#!/bin/sh\n$contents"
ccm.localBinaryPath.toFile().setExecutable(true)
}

then:
ccm.matchesVersion(build) == matches

where:
contents | build | matches
null | "v1.0.0" | false
"""echo '{"version": "v1.0.0"}'""" | "v1.0.0" | true
"""echo '{"version": "v1.0.0"}'""" | "v1.0.0-devel+b5b5b5b5" | true
"""echo '{"version": "v1.0.0-devel+b5b5b5b5"}'""" | "v1.0.0-devel+b5b5b5b5" | true
"""echo '{"version": "v1.0.0-devel+b5b5b5b5"}'""" | "v1.0.0" | true
"""echo '{"version": "v1.0.0-devel+b5b5b5b5"}'""" | "v1.0.0-devel+c6c6c6c6" | true
"""echo '{"version": "v1.0.0-prod+b5b5b5b5"}'""" | "v1.0.0-devel+b5b5b5b5" | true
"""echo '{"version": "v1.0.0"}'""" | "v1.0.1" | false
"""echo '{"version": "v1.0.0"}'""" | "v1.1.0" | false
"""echo '{"version": "v1.0.0"}'""" | "v2.0.0" | false
"""echo '{"version": "v1.0.0"}'""" | "v0.0.0" | false
"""echo '{"version": ""}'""" | "v1.0.0" | false
"""echo '{"version": "v1.0.0"}'""" | "" | false
"""echo '{"version'""" | "v1.0.0" | false
"""exit 0""" | "v1.0.0" | false
"""exit 1""" | "v1.0.0" | false
}
}