Skip to content

Commit

Permalink
Replaced use of main App with def main(), since App doesn't work in s…
Browse files Browse the repository at this point in the history
…cala 3.
  • Loading branch information
abrighton committed Nov 7, 2023
1 parent 5726045 commit 1a3a98c
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ import csw.location.client.scaladsl.HttpLocationServiceFactory

import scala.concurrent.duration.DurationLong

object Example extends App {
object Example {
def main(args: Array[String]): Unit = {
implicit val actorSystem: ActorSystem[SpawnProtocol.Command] = ActorSystem(SpawnProtocol(), "test")
import actorSystem._

private implicit val actorSystem: ActorSystem[SpawnProtocol.Command] = ActorSystem(SpawnProtocol(), "test")
import actorSystem._
val locationService: LocationService = HttpLocationServiceFactory.makeLocalClient

val locationService: LocationService = HttpLocationServiceFactory.makeLocalClient
val installedAuthAdapter = InstalledAppAuthAdapterFactory.make(locationService, new FileAuthStore(Paths.get("/tmp/auth")))

val installedAuthAdapter = InstalledAppAuthAdapterFactory.make(locationService, new FileAuthStore(Paths.get("/tmp/auth")))
println("login initiated")
installedAuthAdapter.login()

println("login initiated")
installedAuthAdapter.login()
val expires: Long = installedAuthAdapter.getAccessToken().get.exp.get
println(s"Expiring on: $expires")
println(System.currentTimeMillis() / 1000)

private val expires: Long = installedAuthAdapter.getAccessToken().get.exp.get
println(s"Expiring on: $expires")
println(System.currentTimeMillis() / 1000)
val timeLeft: Long = expires - System.currentTimeMillis() / 1000
println(s"time left to expire: $timeLeft")

private val timeLeft: Long = expires - System.currentTimeMillis() / 1000
println(s"time left to expire: $timeLeft")
println(installedAuthAdapter.getAccessToken())

println(installedAuthAdapter.getAccessToken())

println(installedAuthAdapter.getAccessToken((timeLeft + 100).seconds))
println(installedAuthAdapter.getAccessToken((timeLeft + 100).seconds))
}
}
34 changes: 18 additions & 16 deletions csw-alarm/csw-alarm-cli/src/main/scala/csw/alarm/cli/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ import csw.alarm.cli.wiring.Wiring
import csw.location.client.utils.LocationServerStatus

// $COVERAGE-OFF$
object Main extends App {
object Main {
private val name: String = BuildInfo.name

new ArgsParser(name).parse(args.toList).foreach { options =>
LocationServerStatus.requireUp(options.locationHost)
run(options)
}

private def run(options: Options): Unit = {
val wiring = Wiring.make(options.locationHost)
import wiring._
import actorRuntime._
def main(args: Array[String]): Unit = {

try {
startLogging(name)
cliApp.execute(options)
new ArgsParser(name).parse(args.toList).foreach { options =>
LocationServerStatus.requireUp(options.locationHost)
run(options)
}
finally {
shutdown()

def run(options: Options): Unit = {
val wiring = Wiring.make(options.locationHost)
import wiring._
import actorRuntime._

try {
startLogging(name)
cliApp.execute(options)
}
finally {
shutdown()
}
}
}

}
// $COVERAGE-ON$
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ class CborSerializationBenchmark {

}

object BigCborTest extends App {
val bytes: Array[Byte] = EventCbor.encode(Data.bigEvent)
val event = EventCbor.decode[SystemEvent](bytes)
assert(event == Data.bigEvent)
println(bytes.length)
object BigCborTest {
def main(args: Array[String]): Unit = {
val bytes: Array[Byte] = EventCbor.encode(Data.bigEvent)
val event = EventCbor.decode[SystemEvent](bytes)
assert(event == Data.bigEvent)
println(bytes.length)
}
}

object CrossLanguageCbor extends App {
val bytes: Array[Byte] = EventCbor.encode(Data.smallEvent)
object CrossLanguageCbor {
def main(args: Array[String]): Unit = {

val bos = new BufferedOutputStream(new FileOutputStream("/tmp/input.cbor"))
bos.write(bytes)
bos.close()
val bytes: Array[Byte] = EventCbor.encode(Data.smallEvent)

val readBytes = Files.readAllBytes(Paths.get("/tmp/input.cbor"))
val event = EventCbor.decode[SystemEvent](readBytes)
println(event)
val bos = new BufferedOutputStream(new FileOutputStream("/tmp/input.cbor"))
bos.write(bytes)
bos.close()

val readBytes = Files.readAllBytes(Paths.get("/tmp/input.cbor"))
val event = EventCbor.decode[SystemEvent](readBytes)
println(event)
}
}
26 changes: 14 additions & 12 deletions csw-config/csw-config-cli/src/main/scala/csw/config/cli/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ import csw.logging.client.scaladsl.LoggingSystemFactory
import csw.network.utils.Networks

// $COVERAGE-OFF$
object Main extends App {
object Main {
private val name = BuildInfo.name

new ArgsParser(name).parse(args.toList).foreach(run)
def main(args: Array[String]): Unit = {
new ArgsParser(name).parse(args.toList).foreach(run)

private def run(options: Options): Unit = {
LocationServerStatus.requireUp(options.locationHost)
def run(options: Options): Unit = {
LocationServerStatus.requireUp(options.locationHost)

val wiring = Wiring.make(options.locationHost)
import wiring._
LoggingSystemFactory.start(name, BuildInfo.version, Networks().hostname, actorSystem)
val wiring = Wiring.make(options.locationHost)
import wiring._
LoggingSystemFactory.start(name, BuildInfo.version, Networks().hostname, actorSystem)

try {
cliApp.start(options)
}
finally {
actorRuntime.shutdown()
try {
cliApp.start(options)
}
finally {
actorRuntime.shutdown()
}
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions csw-event/csw-event-cli/src/main/scala/csw/event/cli/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ import csw.event.cli.wiring.Wiring
import csw.location.client.utils.LocationServerStatus

// $COVERAGE-OFF$
object Main extends App {
object Main {
private val name = BuildInfo.name

new ArgsParser(name).parse(args.toList).foreach { options =>
LocationServerStatus.requireUp(options.locationHost)
run(options)
}
def main(args: Array[String]): Unit = {

new ArgsParser(name).parse(args.toList).foreach { options =>
LocationServerStatus.requireUp(options.locationHost)
run(options)
}

private def run(options: Options): Unit = {
val wiring = Wiring.make(options.locationHost)
import wiring._
import actorRuntime._
startLogging(name)
def run(options: Options): Unit = {
val wiring = Wiring.make(options.locationHost)
import wiring._
import actorRuntime._
startLogging(name)

try cliApp.start(options)
finally actorRuntime.shutdown()
try cliApp.start(options)
finally actorRuntime.shutdown()
}
}
}
// $COVERAGE-ON$
43 changes: 22 additions & 21 deletions examples/src/main/scala/example/auth/SampleHttpApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@ import csw.location.client.scaladsl.HttpLocationServiceFactory
import scala.concurrent.ExecutionContext

// #sample-http-app
object SampleHttpApp extends App {

implicit val actorSystem: ActorSystem[SpawnProtocol.Command] = typed.ActorSystem(SpawnProtocol(), "sample-http-app")
implicit val ec: ExecutionContext = actorSystem.executionContext
private val config = actorSystem.settings.config

val locationService = HttpLocationServiceFactory.makeLocalClient
val directives = SecurityDirectives(config, locationService)
import directives._

def routes: Route =
pathPrefix("api") {
get {
complete("SUCCESS")
} ~
sPost(RealmRolePolicy("admin")) {
complete("SUCCESS")
object SampleHttpApp {
def main(args: Array[String]): Unit = {
implicit val actorSystem: ActorSystem[SpawnProtocol.Command] = typed.ActorSystem(SpawnProtocol(), "sample-http-app")
implicit val ec: ExecutionContext = actorSystem.executionContext
val config = actorSystem.settings.config

val locationService = HttpLocationServiceFactory.makeLocalClient
val directives = SecurityDirectives(config, locationService)
import directives._

def routes: Route =
pathPrefix("api") {
get {
complete("SUCCESS")
} ~
sPost(RealmRolePolicy("admin")) {
complete("SUCCESS")
}
}
}

private val host = "0.0.0.0"
private val port = 9003
val host = "0.0.0.0"
val port = 9003

Http().newServerAt(host, port).bind(routes)
Http().newServerAt(host, port).bind(routes)
}
}
// #sample-http-app
23 changes: 13 additions & 10 deletions examples/src/main/scala/example/auth/installed/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ import csw.location.client.utils.LocationServerStatus
import example.auth.installed.commands.{AppCommand, CommandFactory}

// #main-app
object Main extends App {
object Main {

LocationServerStatus.requireUpLocally()
def main(args: Array[String]): Unit = {

implicit val actorSystem: ActorSystem[_] = ActorSystem(Behaviors.empty, "example-system")
LocationServerStatus.requireUpLocally()

val adapter: InstalledAppAuthAdapter = AdapterFactory.makeAdapter
implicit val actorSystem: ActorSystem[_] = ActorSystem(Behaviors.empty, "example-system")

val command: Option[AppCommand] = CommandFactory.makeCommand(adapter, args)
val adapter: InstalledAppAuthAdapter = AdapterFactory.makeAdapter

try {
command.foreach(_.run())
}
finally {
actorSystem.terminate()
val command: Option[AppCommand] = CommandFactory.makeCommand(adapter, args)

try {
command.foreach(_.run())
}
finally {
actorSystem.terminate()
}
}
}
// #main-app
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import csw.framework.deploy.containercmd.ContainerCmd
import csw.prefix.models.Subsystem.CSW

//#container-app
object ContainerCmdApp extends App {

ContainerCmd.start("ContainerCmdApp", CSW, args)

object ContainerCmdApp {
def main(args: Array[String]): Unit = {
ContainerCmd.start("ContainerCmdApp", CSW, args)
}
}
//#container-app
8 changes: 4 additions & 4 deletions examples/src/main/scala/example/framework/HostConfigApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import csw.framework.deploy.hostconfig.HostConfig
import csw.prefix.models.Subsystem.CSW

//#host-config-app
object HostConfigApp extends App {

HostConfig.start("HostConfigApp", CSW, args)

object HostConfigApp {
def main(args: Array[String]): Unit = {
HostConfig.start("HostConfigApp", CSW, args)
}
}
//#host-config-app
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,30 @@ import scala.concurrent.{Await, Future}
/**
* An example location service client application.
*/
object LocationServiceExampleClientApp extends App {

// http location service client expect that location server is running on local machine
// here we are starting location http server so that httpLocationClient uses can be illustrated
private val wiring = new LocationServerWiring().wiring
Await.result(wiring.locationHttpService.start(), 5.seconds)
val untypedSystem = ActorSystem("untyped-system")
// #create-actor-system
val typedSystem: typed.ActorSystem[SpawnProtocol.Command] =
ActorSystemFactory.remote(SpawnProtocol(), "csw-examples-locationServiceClient")
// #create-actor-system

// #create-location-service
private val locationService = HttpLocationServiceFactory.makeLocalClient(typedSystem)
// #create-location-service

// #create-logging-system
private val host = InetAddress.getLocalHost.getHostName
// Only call this once per application
val loggingSystem: LoggingSystem = LoggingSystemFactory.start("LocationServiceExampleClient", "0.1", host, typedSystem)
// #create-logging-system

untypedSystem.actorOf(LocationServiceExampleClient.props(locationService, loggingSystem)(typedSystem))
object LocationServiceExampleClientApp {
def main(args: Array[String]): Unit = {
// http location service client expect that location server is running on local machine
// here we are starting location http server so that httpLocationClient uses can be illustrated
val wiring = new LocationServerWiring().wiring
Await.result(wiring.locationHttpService.start(), 5.seconds)
val untypedSystem = ActorSystem("untyped-system")
// #create-actor-system
val typedSystem: typed.ActorSystem[SpawnProtocol.Command] =
ActorSystemFactory.remote(SpawnProtocol(), "csw-examples-locationServiceClient")
// #create-actor-system

// #create-location-service
val locationService = HttpLocationServiceFactory.makeLocalClient(typedSystem)
// #create-location-service

// #create-logging-system
val host = InetAddress.getLocalHost.getHostName
// Only call this once per application
val loggingSystem: LoggingSystem = LoggingSystemFactory.start("LocationServiceExampleClient", "0.1", host, typedSystem)
// #create-logging-system

untypedSystem.actorOf(LocationServiceExampleClient.props(locationService, loggingSystem)(typedSystem))
}
}

sealed trait ExampleMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.concurrent.{Await, ExecutionContextExecutor, Future}
/**
* An example that shows how to register a component actor with the location service.
*/
object LocationServiceExampleComponentApp extends App {
object LocationServiceExampleComponentApp {
implicit val system: typed.ActorSystem[SpawnProtocol.Command] = ActorSystemFactory.remote(SpawnProtocol(), "example-system")
implicit val ec: ExecutionContextExecutor = system.executionContext
private val locationService = HttpLocationServiceFactory.makeLocalClient
Expand All @@ -37,7 +37,9 @@ object LocationServiceExampleComponentApp extends App {
LoggingSystemFactory.start("LocationServiceExampleComponent", "0.1", host, system)
// #create-logging-system

system.spawn(LocationServiceExampleComponent.behaviour(locationService), LocationServiceExampleComponent.connection.name)
def main(args: Array[String]): Unit = {
system.spawn(LocationServiceExampleComponent.behaviour(locationService), LocationServiceExampleComponent.connection.name)
}
}

object LocationServiceExampleComponent {
Expand Down
Loading

0 comments on commit 1a3a98c

Please sign in to comment.