From ec56c2705a53b458dec9fbd327feb59135391148 Mon Sep 17 00:00:00 2001 From: reidspencer Date: Sun, 13 Nov 2022 14:17:56 -0500 Subject: [PATCH] Reduce complexity of runMain Signed-off-by: reidspencer --- .../riddl/commands/CommandPlugin.scala | 62 ++++++++++--------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/commands/src/main/scala/com/reactific/riddl/commands/CommandPlugin.scala b/commands/src/main/scala/com/reactific/riddl/commands/CommandPlugin.scala index 040ddd727..fdb772dd0 100644 --- a/commands/src/main/scala/com/reactific/riddl/commands/CommandPlugin.scala +++ b/commands/src/main/scala/com/reactific/riddl/commands/CommandPlugin.scala @@ -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 @@ -194,7 +201,6 @@ object CommandPlugin { SevereError.severity + 1 } } - } /** The service interface for Riddlc command plugins */