small refactor

This commit is contained in:
2025-01-20 23:31:00 +08:00
parent 1e6ece37a5
commit 9ce3e7fa0a
6 changed files with 18 additions and 18 deletions

View File

@@ -4,7 +4,7 @@ import net.woggioni.gbcs.common.contextLogger
import net.woggioni.gbcs.common.error import net.woggioni.gbcs.common.error
import net.woggioni.gbcs.common.info import net.woggioni.gbcs.common.info
import net.woggioni.gbcs.cli.impl.GbcsCommand import net.woggioni.gbcs.cli.impl.GbcsCommand
import net.woggioni.gbcs.client.GbcsClient import net.woggioni.gbcs.client.GradleBuildCacheClient
import picocli.CommandLine import picocli.CommandLine
import java.security.SecureRandom import java.security.SecureRandom
import java.time.Duration import java.time.Duration
@@ -40,7 +40,7 @@ class BenchmarkCommand : GbcsCommand() {
clientCommand.configuration.profiles[profileName] clientCommand.configuration.profiles[profileName]
?: throw IllegalArgumentException("Profile $profileName does not exist in configuration") ?: throw IllegalArgumentException("Profile $profileName does not exist in configuration")
} }
val client = GbcsClient(profile) val client = GradleBuildCacheClient(profile)
val entryGenerator = sequence { val entryGenerator = sequence {
val random = Random(SecureRandom.getInstance("NativePRNGNonBlocking").nextLong()) val random = Random(SecureRandom.getInstance("NativePRNGNonBlocking").nextLong())

View File

@@ -1,7 +1,7 @@
package net.woggioni.gbcs.cli.impl.commands package net.woggioni.gbcs.cli.impl.commands
import net.woggioni.gbcs.cli.impl.GbcsCommand import net.woggioni.gbcs.cli.impl.GbcsCommand
import net.woggioni.gbcs.client.GbcsClient import net.woggioni.gbcs.client.GradleBuildCacheClient
import net.woggioni.jwo.Application import net.woggioni.jwo.Application
import picocli.CommandLine import picocli.CommandLine
import java.nio.file.Path import java.nio.file.Path
@@ -28,8 +28,8 @@ class ClientCommand(app : Application) : GbcsCommand() {
) )
var profileName : String? = null var profileName : String? = null
val configuration : GbcsClient.Configuration by lazy { val configuration : GradleBuildCacheClient.Configuration by lazy {
GbcsClient.Configuration.parse(configurationFile) GradleBuildCacheClient.Configuration.parse(configurationFile)
} }
override fun run() { override fun run() {

View File

@@ -2,7 +2,7 @@ package net.woggioni.gbcs.cli.impl.commands
import net.woggioni.gbcs.common.contextLogger import net.woggioni.gbcs.common.contextLogger
import net.woggioni.gbcs.cli.impl.GbcsCommand import net.woggioni.gbcs.cli.impl.GbcsCommand
import net.woggioni.gbcs.client.GbcsClient import net.woggioni.gbcs.client.GradleBuildCacheClient
import picocli.CommandLine import picocli.CommandLine
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
@@ -38,7 +38,7 @@ class GetCommand : GbcsCommand() {
clientCommand.configuration.profiles[profileName] clientCommand.configuration.profiles[profileName]
?: throw IllegalArgumentException("Profile $profileName does not exist in configuration") ?: throw IllegalArgumentException("Profile $profileName does not exist in configuration")
} }
GbcsClient(profile).use { client -> GradleBuildCacheClient(profile).use { client ->
client.get(key).thenApply { value -> client.get(key).thenApply { value ->
value?.let { value?.let {
(output?.let(Files::newOutputStream) ?: System.out).use { (output?.let(Files::newOutputStream) ?: System.out).use {

View File

@@ -3,7 +3,7 @@ package net.woggioni.gbcs.cli.impl.commands
import net.woggioni.gbcs.common.contextLogger import net.woggioni.gbcs.common.contextLogger
import net.woggioni.gbcs.cli.impl.GbcsCommand import net.woggioni.gbcs.cli.impl.GbcsCommand
import net.woggioni.gbcs.cli.impl.converters.InputStreamConverter import net.woggioni.gbcs.cli.impl.converters.InputStreamConverter
import net.woggioni.gbcs.client.GbcsClient import net.woggioni.gbcs.client.GradleBuildCacheClient
import picocli.CommandLine import picocli.CommandLine
import java.io.InputStream import java.io.InputStream
@@ -39,7 +39,7 @@ class PutCommand : GbcsCommand() {
clientCommand.configuration.profiles[profileName] clientCommand.configuration.profiles[profileName]
?: throw IllegalArgumentException("Profile $profileName does not exist in configuration") ?: throw IllegalArgumentException("Profile $profileName does not exist in configuration")
} }
GbcsClient(profile).use { client -> GradleBuildCacheClient(profile).use { client ->
value.use { value.use {
client.put(key, it.readAllBytes()) client.put(key, it.readAllBytes())
}.get() }.get()

View File

@@ -48,7 +48,7 @@ import java.util.concurrent.atomic.AtomicInteger
import io.netty.util.concurrent.Future as NettyFuture import io.netty.util.concurrent.Future as NettyFuture
class GbcsClient(private val profile: Configuration.Profile) : AutoCloseable { class GradleBuildCacheClient(private val profile: Configuration.Profile) : AutoCloseable {
private val group: NioEventLoopGroup private val group: NioEventLoopGroup
private var sslContext: SslContext private var sslContext: SslContext
private val log = contextLogger() private val log = contextLogger()

View File

@@ -3,7 +3,7 @@ package net.woggioni.gbcs.client.impl
import net.woggioni.gbcs.api.exception.ConfigurationException import net.woggioni.gbcs.api.exception.ConfigurationException
import net.woggioni.gbcs.common.Xml.Companion.asIterable import net.woggioni.gbcs.common.Xml.Companion.asIterable
import net.woggioni.gbcs.common.Xml.Companion.renderAttribute import net.woggioni.gbcs.common.Xml.Companion.renderAttribute
import net.woggioni.gbcs.client.GbcsClient import net.woggioni.gbcs.client.GradleBuildCacheClient
import org.w3c.dom.Document import org.w3c.dom.Document
import java.net.URI import java.net.URI
import java.nio.file.Files import java.nio.file.Files
@@ -15,10 +15,10 @@ import java.time.Duration
object Parser { object Parser {
fun parse(document: Document): GbcsClient.Configuration { fun parse(document: Document): GradleBuildCacheClient.Configuration {
val root = document.documentElement val root = document.documentElement
val profiles = mutableMapOf<String, GbcsClient.Configuration.Profile>() val profiles = mutableMapOf<String, GradleBuildCacheClient.Configuration.Profile>()
for (child in root.asIterable()) { for (child in root.asIterable()) {
val tagName = child.localName val tagName = child.localName
@@ -26,7 +26,7 @@ object Parser {
"profile" -> { "profile" -> {
val name = child.renderAttribute("name") ?: throw ConfigurationException("name attribute is required") val name = child.renderAttribute("name") ?: throw ConfigurationException("name attribute is required")
val uri = child.renderAttribute("base-url")?.let(::URI) ?: throw ConfigurationException("base-url attribute is required") val uri = child.renderAttribute("base-url")?.let(::URI) ?: throw ConfigurationException("base-url attribute is required")
var authentication: GbcsClient.Configuration.Authentication? = null var authentication: GradleBuildCacheClient.Configuration.Authentication? = null
for (gchild in child.asIterable()) { for (gchild in child.asIterable()) {
when (gchild.localName) { when (gchild.localName) {
"tls-client-auth" -> { "tls-client-auth" -> {
@@ -47,14 +47,14 @@ object Parser {
.toList() .toList()
.toTypedArray() .toTypedArray()
authentication = authentication =
GbcsClient.Configuration.Authentication.TlsClientAuthenticationCredentials(key, certChain) GradleBuildCacheClient.Configuration.Authentication.TlsClientAuthenticationCredentials(key, certChain)
} }
"basic-auth" -> { "basic-auth" -> {
val username = gchild.renderAttribute("user") ?: throw ConfigurationException("username attribute is required") val username = gchild.renderAttribute("user") ?: throw ConfigurationException("username attribute is required")
val password = gchild.renderAttribute("password") ?: throw ConfigurationException("password attribute is required") val password = gchild.renderAttribute("password") ?: throw ConfigurationException("password attribute is required")
authentication = authentication =
GbcsClient.Configuration.Authentication.BasicAuthenticationCredentials(username, password) GradleBuildCacheClient.Configuration.Authentication.BasicAuthenticationCredentials(username, password)
} }
} }
} }
@@ -63,10 +63,10 @@ object Parser {
?: 50 ?: 50
val connectionTimeout = child.renderAttribute("connection-timeout") val connectionTimeout = child.renderAttribute("connection-timeout")
?.let(Duration::parse) ?.let(Duration::parse)
profiles[name] = GbcsClient.Configuration.Profile(uri, authentication, connectionTimeout, maxConnections) profiles[name] = GradleBuildCacheClient.Configuration.Profile(uri, authentication, connectionTimeout, maxConnections)
} }
} }
} }
return GbcsClient.Configuration(profiles) return GradleBuildCacheClient.Configuration(profiles)
} }
} }