Skip to content

Commit

Permalink
Reduce complexity of runMain
Browse files Browse the repository at this point in the history
Signed-off-by: reidspencer <reid.spencer@yoppworks.com>
  • Loading branch information
reid-spencer committed Nov 13, 2022
1 parent 6fad5d5 commit ec56c27
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,39 +151,46 @@ object CommandPlugin {
result
}

def runMain(args: Array[String]): Int = {
private def handleCommandRun(
remaining: Array[String],
commonOptions: CommonOptions
): Int = {
var log: Logger = SysLogger()

if (remaining.isEmpty) {
if (!commonOptions.quiet) {
log.error("No command argument was provided")
}
1
} else {
val name = remaining.head
if (commonOptions.dryRun) {
if (!commonOptions.quiet) {
log.info(s"Would have executed: ${remaining.mkString(" ")}")
}
}
if (commonOptions.quiet) { log = StringLogger() }
val result = CommandPlugin
.runCommandWithArgs(name, remaining, log, commonOptions)
result match {
case Right(_) =>
if (commonOptions.quiet) { System.out.println(log.summary) }
0
case Left(messages) =>
if (commonOptions.quiet) { highestSeverity(messages) + 1 }
else { Messages.logMessages(messages, log, commonOptions) + 1 }
}
}
}

def runMain(args: Array[String]): Int = {
val log = SysLogger()
try {
val (common, remaining) = com.reactific.riddl.commands.CommonOptionsHelper
.parseCommonOptions(args)
common match {
case Some(commonOptions) =>
if (remaining.isEmpty) {
if (!commonOptions.quiet) {
log.error("No command argument was provided")
}
1
} else {
val name = remaining.head
if (commonOptions.dryRun) {
if (!commonOptions.quiet) {
log.info(s"Would have executed: ${remaining.mkString(" ")}")
}
}
if (commonOptions.quiet) { log = StringLogger() }
val result = CommandPlugin
.runCommandWithArgs(name, remaining, log, commonOptions)
result match {
case Right(_) =>
if (commonOptions.quiet) { System.out.println(log.summary) }
0
case Left(messages) =>
if (commonOptions.quiet) { highestSeverity(messages) + 1 }
else { Messages.logMessages(messages, log, commonOptions) + 1 }
}
}
case None =>
case Some(commonOptions) => handleCommandRun(remaining, commonOptions)
case None =>
// arguments are bad, error message will have been displayed
log.info("Option parsing failed, terminating.")
1
Expand All @@ -194,7 +201,6 @@ object CommandPlugin {
SevereError.severity + 1
}
}

}

/** The service interface for Riddlc command plugins */
Expand Down

0 comments on commit ec56c27

Please sign in to comment.