Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Apr 22, 2024
1 parent f17ab59 commit 0457d81
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
68 changes: 68 additions & 0 deletions src/main/kotlin/Cli.kt
Original file line number Diff line number Diff line change
@@ -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<String, String>): 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<String>(
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<String>(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<String>(
shortName = templateOptShortName,
name = "template",
description = "Template used to create the new CV",
regex = Regex("(regular|full|minimum)"),
value = "regular",
),
Option<String>(
shortName = formatOptShortName,
name = "format",
description = "Data format used to store the generated document",
regex = Regex("(yaml|toml|json)"),
value = "yaml",
),
Parameter<String>(
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),
)
}
74 changes: 3 additions & 71 deletions src/main/kotlin/Cv.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -62,7 +58,7 @@ const val templateOptShortName: Char = 't'
const val formatOptShortName: Char = 'f'

private val browseCommand: List<String> by lazy {
when (Jvm.osKind){
when (Jvm.osKind) {
WINDOWS -> listOf("start")
MACOS -> listOf("open")
LINUX, UNIX -> listOf("xdg-open")
Expand Down Expand Up @@ -105,68 +101,6 @@ private fun exit(exception: Exception) {
exitProcess(code)
}

private fun createProgram(buildProperties: Map<String, String>): 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<String>(
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<String>(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<String>(
shortName = templateOptShortName,
name = "template",
description = "Template used to create the new CV",
regex = Regex("(regular|full|minimum)"),
value = "regular",
),
Option<String>(
shortName = formatOptShortName,
name = "format",
description = "Data format used to store the generated document",
regex = Regex("(yaml|toml|json)"),
value = "yaml",
),
Parameter<String>(
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<String>(templateOptShortName.toString())
val format = command.propertyValueOrNull<String>(formatOptShortName.toString())
Expand Down Expand Up @@ -225,8 +159,6 @@ private fun serve(command: Command) {
get(callback = UrlCallback(urlOf(mainPage)))
}

// ManagementFactory.getRuntimeMXBean().uptime.info("START MS: ")

if (command.propertyValueOrNull<Boolean>("b") == true)
(browseCommand + "http://localhost:${server.runtimePort}/cv").exec()
}
Expand Down

0 comments on commit 0457d81

Please sign in to comment.