Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CommandResourceApp #518

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.monovore.decline.effect

import cats.effect.ExitCode
import cats.effect.IO
import cats.effect.unsafe.IORuntime
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class CommandResourceAppSpec extends AnyFlatSpec with Matchers {

"CommandResourceApp" should "return a success exit code when passing an argument" in {
runApp("me") shouldBe ExitCode.Success
}

it should "return a success exit code when passing a version option" in {
runApp("--version") shouldBe ExitCode.Success
}

it should "return a success exit code when passing a help option" in {
runApp("--help") shouldBe ExitCode.Success
}

it should "return an error exit code when passing no arguments" in {
runApp() shouldBe ExitCode.Error
}

private[this] def runApp(args: String*): ExitCode =
PureResourceHelloWorld.run(args.toList).use(IO.pure).unsafeRunSync()(IORuntime.global)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.monovore.decline.effect

import cats.effect.ExitCode
import cats.effect.IO
import cats.effect.Resource
import com.monovore.decline._

object PureResourceHelloWorld
extends CommandResourceApp(
name = "pure-hello",
header = "Pure Hello World with Decline",
version = "0.0.1"
) {

def main: Opts[Resource[IO, ExitCode]] = {
val toGreetOpt = Opts.argument[String]("to-greet")
toGreetOpt.map { toGreet => Resource.eval(IO.println(s"Hello $toGreet").as(ExitCode.Success)) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ object CommandIOApp {
exitCode <- parseResult.fold(printHelp[F], identity)
} yield exitCode

private[CommandIOApp] def printHelp[F[_]: Console: Functor](help: Help): F[ExitCode] =
private[effect] def printHelp[F[_]: Console: Functor](help: Help): F[ExitCode] =
Console[F].errorln(help).as {
if (help.errors.nonEmpty) ExitCode.Error
else ExitCode.Success
}

private[CommandIOApp] def addVersionFlag[F[_]: Console: Functor](
private[effect] def addVersionFlag[F[_]: Console: Functor](
opts: Opts[F[ExitCode]]
)(version: String): Opts[F[ExitCode]] = {
val flag = Opts.flag(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.monovore.decline.effect

import cats.Functor
import cats.effect.ExitCode
import cats.effect.IO
import cats.effect.Resource
import cats.effect.ResourceApp
import cats.effect.Sync
import cats.effect.std.Console
import cats.syntax.all._
import com.monovore.decline._

import scala.language.higherKinds

abstract class CommandResourceApp(
name: String,
header: String,
helpFlag: Boolean = true,
version: String = ""
) extends ResourceApp {

def main: Opts[Resource[IO, ExitCode]]

override final def run(args: List[String]): Resource[IO, ExitCode] =
CommandResourceApp
.run(name, header, helpFlag, Option(version).filter(_.nonEmpty))(main, args)

}

object CommandResourceApp {

def run[F[_]: Sync: Console](
name: String,
header: String,
helpFlag: Boolean = true,
version: Option[String] = None
)(opts: Opts[Resource[F, ExitCode]], args: List[String]): Resource[F, ExitCode] = {
run[F](Command(name, header, helpFlag)(version.map(addVersionFlag(opts)).getOrElse(opts)), args)
}

def run[F[_]: Sync: Console](
command: Command[Resource[F, ExitCode]],
args: List[String]
): Resource[F, ExitCode] =
for {
parseResult <- Resource.eval(
Sync[F].delay(
command.parse(
PlatformApp.ambientArgs getOrElse args,
PlatformApp.ambientEnvs getOrElse sys.env
)
)
)
exitCode <- parseResult.fold(printHelp[F], identity)
} yield exitCode

private[CommandResourceApp] def printHelp[F[_]: Console: Functor](
help: Help
): Resource[F, ExitCode] =
Resource.eval(CommandIOApp.printHelp(help))

private[CommandResourceApp] def addVersionFlag[F[_]: Console: Functor](
opts: Opts[Resource[F, ExitCode]]
)(version: String): Opts[Resource[F, ExitCode]] = {
val flag = Opts.flag(
long = "version",
short = "v",
help = "Print the version number and exit.",
visibility = Visibility.Partial
)

flag.as(Resource.eval(Console[F].println(version).as(ExitCode.Success))) orElse opts
}

}