application.ToOpenAPICommand.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-executable Show documentation
Show all versions of specmatic-executable Show documentation
Command-line standalone executable jar for Specmatic
@file:JvmName("BundleCommand_Jvm")
package application
import io.specmatic.core.YAML
import io.specmatic.core.log.HeadingLog
import io.specmatic.core.log.Verbose
import io.specmatic.core.log.logger
import io.specmatic.core.parseGherkinStringToFeature
import io.specmatic.core.utilities.exitWithMessage
import io.swagger.v3.core.util.Yaml
import org.springframework.beans.factory.annotation.Autowired
import picocli.CommandLine
import java.io.File
import java.util.concurrent.Callable
import kotlin.system.exitProcess
@CommandLine.Command(name = "to-openapi",
mixinStandardHelpOptions = true,
description = ["Converts a Gherkin file to OpenAPI"])
class ToOpenAPICommand : Callable {
@CommandLine.Parameters(description = ["Path in which to create the bundle"])
lateinit var contractPath: String
@CommandLine.Option(names = ["--debug"])
var verbose: Boolean = false
@Autowired
lateinit var fileOperations: FileOperations
override fun call() {
if(verbose) {
logger = Verbose()
}
logger.keepReady(HeadingLog(contractPath))
if(!fileOperations.isFile(contractPath)) {
exitWithMessage("$contractPath is not a file")
}
try {
val gherkinContract = parseGherkinStringToFeature(fileOperations.read(contractPath))
val openAPI = gherkinContract.toOpenApi()
val openAPIYaml = Yaml.pretty(openAPI)
val contractFile = File(contractPath)
val openAPIFile: File =
contractFile.canonicalFile.parentFile.resolve("${contractFile.nameWithoutExtension}.$YAML")
openAPIFile.writeText(openAPIYaml)
} catch(e: Throwable) {
logger.log(e)
exitProcess(1)
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy