You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.3 KiB

package alfred
import alfred.running.LogFile
import com.fasterxml.jackson.annotation.JsonIgnore
import io.quarkus.runtime.annotations.RegisterForReflection
import org.eclipse.microprofile.config.inject.ConfigProperty
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.lang.RuntimeException
import java.nio.file.Files
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
class Builds(
@ConfigProperty(name = "ALFRED_HOME")
private val home: String
) {
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
init {
logger.info("ALFRED_HOME is $home")
}
fun buildConfig(build: BuildId): BuildConfig {
val path = Paths.get(home, "builds", "$build.properties")
if (!path.toFile().exists()) {
throw UnknownBuild(build)
}
val props = Properties()
props.load(path.toFile().inputStream())
val env = props
.entries
.filter { it.key.toString() == it.key.toString().toUpperCase() }
.associate { Pair(it.key.toString(), it.value.toString()) }
return BuildConfig(
user = props.getProperty("user"),
workspace = props.getProperty("workspace"),
apikey = props.getProperty("apikey") ?: "",
script = props.getProperty("script"),
gitRepo = props.getProperty("gitrepo"),
env = env
)
}
fun createLogFile(build: BuildId): LogFile {
val nowStr = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)
val fileName = "$build.$nowStr.log"
val path = Paths.get(home, "logs", fileName)
path.toFile().parentFile.mkdirs()
return Files.createFile(path).toFile()
}
}
class UnknownBuild(val build: BuildId) : RuntimeException() {
override fun toString() =
"unknown build: $build"
}
@RegisterForReflection
data class BuildConfig(
val user: String,
val workspace: String,
@field:JsonIgnore
val apikey: String,
val script: String?,
val gitRepo: String?,
@field:JsonIgnore
val env: Map<String, String>
)
typealias BuildId = String