All Downloads are FREE. Search and download functionalities are using the official Maven repository.

application.ToOpenAPICommand.kt Maven / Gradle / Ivy

There is a newer version: 2.0.37
Show newest version
@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