diff --git a/src/main/kotlin/Cli.kt b/src/main/kotlin/Cli.kt new file mode 100644 index 0000000..6d3f44b --- /dev/null +++ b/src/main/kotlin/Cli.kt @@ -0,0 +1,68 @@ +package co.codecv + +import com.hexagonkt.args.* +import com.hexagonkt.args.Property.Companion.HELP +import com.hexagonkt.args.Property.Companion.VERSION +import com.hexagonkt.core.require + +internal fun createProgram(buildProperties: Map): Program { + val urlParamDescription = "URL for the CV file to use. If no schema, 'file' is assumed" + val browseFlag = Flag('b', "browse", "Open browser with served CV") + val addressParam = Option( + shortName = 'a', + name = addressParamName, + description = "Address to bind the server to", + Regex("^(?:(?:25[0-5]|2[0-4]\\d|1?\\d{1,2})(?:\\.(?!\$)|\$)){4}\$"), + value = "127.0.0.1" + ) + val urlParam = Parameter(urlParamName, urlParamDescription, optional = false) + + val serveCommand = Command( + name = serveCommandName, + title = "Serve a CV document", + description = "Serve the CV document supplied, allowing it to be rendered on a browser", + properties = setOf(HELP, browseFlag, addressParam, urlParam), + ) + + val createCommand = Command( + name = createCommandName, + title = "Create a CV document", + description = "Creates a new CV document based on a template", + properties = setOf( + HELP, + Option( + shortName = templateOptShortName, + name = "template", + description = "Template used to create the new CV", + regex = Regex("(regular|full|minimum)"), + value = "regular", + ), + Option( + shortName = formatOptShortName, + name = "format", + description = "Data format used to store the generated document", + regex = Regex("(yaml|toml|json)"), + value = "yaml", + ), + Parameter( + name = fileParamName, + description = "File to store the CV document. Document printed on stdout if missed", + ) + ), + ) + + val validateCommand = Command( + name = validateCommandName, + title = "Validate an existing CV", + description = "Returns a list of errors and a 400 code if the CV document is not valid", + properties = setOf(HELP, urlParam), + ) + + return Program( + name = buildProperties.require("project"), + version = buildProperties.require("version"), + description = buildProperties.require("description"), + properties = setOf(VERSION) + serveCommand.properties, + commands = setOf(serveCommand, createCommand, validateCommand), + ) +} diff --git a/src/main/kotlin/Cv.kt b/src/main/kotlin/Cv.kt index 81bd5c2..5d858d6 100644 --- a/src/main/kotlin/Cv.kt +++ b/src/main/kotlin/Cv.kt @@ -1,14 +1,10 @@ package co.codecv -import com.hexagonkt.args.* -import com.hexagonkt.args.Property.Companion.HELP -import com.hexagonkt.args.Property.Companion.VERSION +import com.hexagonkt.args.Command import com.hexagonkt.core.* +import com.hexagonkt.core.OsKind.* import com.hexagonkt.core.logging.logger import com.hexagonkt.core.media.mediaTypeOfOrNull -import com.hexagonkt.core.CodedException -import com.hexagonkt.core.OsKind.* -import com.hexagonkt.core.properties import com.hexagonkt.core.text.wordsToCamel import com.hexagonkt.http.handlers.HttpContext import com.hexagonkt.http.model.ContentType @@ -62,7 +58,7 @@ const val templateOptShortName: Char = 't' const val formatOptShortName: Char = 'f' private val browseCommand: List by lazy { - when (Jvm.osKind){ + when (Jvm.osKind) { WINDOWS -> listOf("start") MACOS -> listOf("open") LINUX, UNIX -> listOf("xdg-open") @@ -105,68 +101,6 @@ private fun exit(exception: Exception) { exitProcess(code) } -private fun createProgram(buildProperties: Map): Program { - val urlParamDescription = "URL for the CV file to use. If no schema, 'file' is assumed" - val browseFlag = Flag('b', "browse", "Open browser with served CV") - val addressParam = Option( - shortName = 'a', - name = addressParamName, - description ="Address to bind the server to", - Regex("^(?:(?:25[0-5]|2[0-4]\\d|1?\\d{1,2})(?:\\.(?!\$)|\$)){4}\$"), - value = "127.0.0.1" - ) - val urlParam = Parameter(urlParamName, urlParamDescription, optional = false) - - val serveCommand = Command( - name = serveCommandName, - title = "Serve a CV document", - description = "Serve the CV document supplied, allowing it to be rendered on a browser", - properties = setOf(HELP, browseFlag, addressParam, urlParam), - ) - - val createCommand = Command( - name = createCommandName, - title = "Create a CV document", - description = "Creates a new CV document based on a template", - properties = setOf( - HELP, - Option( - shortName = templateOptShortName, - name = "template", - description = "Template used to create the new CV", - regex = Regex("(regular|full|minimum)"), - value = "regular", - ), - Option( - shortName = formatOptShortName, - name = "format", - description = "Data format used to store the generated document", - regex = Regex("(yaml|toml|json)"), - value = "yaml", - ), - Parameter( - name = fileParamName, - description = "File to store the CV document. Document printed on stdout if missed", - ) - ), - ) - - val validateCommand = Command( - name = validateCommandName, - title = "Validate an existing CV", - description = "Returns a list of errors and a 400 code if the CV document is not valid", - properties = setOf(HELP, urlParam), - ) - - return Program( - name = buildProperties.require("project"), - version = buildProperties.require("version"), - description = buildProperties.require("description"), - properties = setOf(VERSION) + serveCommand.properties, - commands = setOf(serveCommand, createCommand, validateCommand), - ) -} - private fun create(command: Command) { val template = command.propertyValueOrNull(templateOptShortName.toString()) val format = command.propertyValueOrNull(formatOptShortName.toString()) @@ -225,8 +159,6 @@ private fun serve(command: Command) { get(callback = UrlCallback(urlOf(mainPage))) } -// ManagementFactory.getRuntimeMXBean().uptime.info("START MS: ") - if (command.propertyValueOrNull("b") == true) (browseCommand + "http://localhost:${server.runtimePort}/cv").exec() }