Skip to content

Commit

Permalink
Merge pull request #1958 from RaasAhsan/resource-app
Browse files Browse the repository at this point in the history
IOApp.ResourceApp
  • Loading branch information
djspiewak authored May 30, 2021
2 parents 4dba25f + 0a6619a commit 161274f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ trait IOApp {
Thread.currentThread().interrupt()
}
}

}

object IOApp {
Expand Down
52 changes: 52 additions & 0 deletions core/shared/src/main/scala/cats/effect/ResourceApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020-2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect

import cats.syntax.all._

trait ResourceApp { self =>
def run(args: List[String]): Resource[IO, ExitCode]

final def main(args: Array[String]): Unit = {
val ioApp = new IOApp {
override def run(args: List[String]): IO[ExitCode] =
self.run(args).use(IO.pure(_))
}

ioApp.main(args)
}
}

object ResourceApp {
trait Simple extends ResourceApp {
def run: Resource[IO, Unit]
final def run(args: List[String]): Resource[IO, ExitCode] = run.as(ExitCode.Success)
}

trait Forever { self =>
def run(args: List[String]): Resource[IO, Unit]

final def main(args: Array[String]): Unit = {
val ioApp = new IOApp {
override def run(args: List[String]): IO[ExitCode] =
self.run(args).useForever
}

ioApp.main(args)
}
}
}

0 comments on commit 161274f

Please sign in to comment.