Skip to content

Commit

Permalink
Remove CLI opts in favor of config overrides using environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
somebody1234 committed Aug 9, 2023
1 parent e96174e commit dc9f902
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ project-manager {

server {
host = ${project-manager.network.interface}
host = ${?SERVER_HOST}

port = 30535
port = ${?SERVER_PORT}
}

bootloader {
Expand All @@ -51,7 +54,7 @@ project-manager {

storage {
projects-root = ${user.home}/enso
projects-root=${?PROJECTS_ROOT}
projects-root = ${?PROJECTS_ROOT}
temporary-projects-path = ${project-manager.storage.projects-root}/tmp
user-projects-path = ${project-manager.storage.projects-root}/projects
tutorials-path = ${project-manager.storage.projects-root}/tutorials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ object Cli {
val PROFILING_PATH = "profiling-path"
val PROFILING_TIME = "profiling-time"
val PROFILING_EVENTS_LOG_PATH = "profiling-events-log-path"
val SERVER_HOST = "server-host"
val SERVER_PORT = "server-port"

object option {

Expand Down Expand Up @@ -76,22 +74,6 @@ object Cli {
"The path to the runtime events log file. Enables the runtime events logging."
)
.build()

val serverHost: cli.Option = cli.Option.builder
.hasArg(true)
.numberOfArgs(1)
.argName("host")
.longOpt(SERVER_HOST)
.desc("The host name of the server.")
.build()

val serverPort: cli.Option = cli.Option.builder
.hasArg(true)
.numberOfArgs(1)
.argName("port")
.longOpt(SERVER_PORT)
.desc("The port number of the server.")
.build()
}

val options: cli.Options =
Expand All @@ -104,8 +86,6 @@ object Cli {
.addOption(option.profilingPath)
.addOption(option.profilingTime)
.addOption(option.profilingEventsLogPath)
.addOption(option.serverHost)
.addOption(option.serverPort)

/** Parse the command line options. */
def parse(args: Array[String]): Either[String, cli.CommandLine] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ object ProjectManager extends ZIOAppDefault with LazyLogging {
computeExecutionContext
)
for {
binding <- bindServer(mainModule, processConfig)
_ <- logServerStartup(processConfig)
binding <- bindServer(mainModule)
_ <- logServerStartup()
_ <- tryReadLine
_ <- ZIO.succeed { logger.info("Stopping server...") }
_ <- ZIO.succeed { binding.unbind() }
Expand Down Expand Up @@ -205,37 +205,14 @@ object ProjectManager extends ZIOAppDefault with LazyLogging {
ZIO.fail(err)
}

val parseServerHost = ZIO
.attempt {
Option(options.getOptionValue(Cli.SERVER_HOST))
}
.catchAll { err =>
printLineError(s"Invalid ${Cli.SERVER_HOST} argument.") *>
ZIO.fail(err)
}

val parseServerPort = ZIO
.attempt {
Option(options.getOptionValue(Cli.SERVER_PORT))
.map(_.toInt)
}
.catchAll { err =>
printLineError(s"Invalid ${Cli.SERVER_PORT} argument.") *>
ZIO.fail(err)
}

for {
profilingEventsLogPath <- parseProfilingEventsLogPath
profilingPath <- parseProfilingPath
profilingTime <- parseProfilingTime
serverHost <- parseServerHost
serverPort <- parseServerPort
} yield ProjectManagerOptions(
profilingEventsLogPath,
profilingPath,
profilingTime,
serverHost,
serverPort
profilingTime
)
}

Expand All @@ -260,9 +237,7 @@ object ProjectManager extends ZIOAppDefault with LazyLogging {
logLevel,
opts.profilingRuntimeEventsLog,
opts.profilingPath,
opts.profilingTime,
opts.serverHost,
opts.serverPort
opts.profilingTime
)
exitCode <- mainProcess(procConf).fold(
th => {
Expand Down Expand Up @@ -312,25 +287,21 @@ object ProjectManager extends ZIOAppDefault with LazyLogging {
ZIO.succeed(SuccessExitCode)
}

private def logServerStartup(processConfig: MainProcessConfig): UIO[Unit] =
private def logServerStartup(): UIO[Unit] =
ZIO.succeed {
logger.info(
"Started server at {}:{}, press enter to kill server",
processConfig.serverHost.getOrElse(config.server.host),
processConfig.serverPort.getOrElse(config.server.port)
config.server.host,
config.server.port
)
}

private def bindServer(
module: MainModule[ZIO[ZAny, +*, +*]],
processConfig: MainProcessConfig
module: MainModule[ZIO[ZAny, +*, +*]]
): UIO[Http.ServerBinding] =
ZIO.succeed {
Await.result(
module.server.bind(
processConfig.serverHost.getOrElse(config.server.host),
processConfig.serverPort.getOrElse(config.server.port)
),
module.server.bind(config.server.host, config.server.port),
3.seconds
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import scala.concurrent.duration.FiniteDuration
* @param profilingRuntimeEventsLog the path to the runtime events log file
* @param profilingPath the path to the profiling output file
* @param profilingTime the time limiting the profiling duration
* @param serverHost an address that the server listen on
* @param serverPort a port that the server listen on
*/
case class ProjectManagerOptions(
profilingRuntimeEventsLog: Option[Path],
profilingPath: Option[Path],
profilingTime: Option[FiniteDuration],
serverHost: Option[String],
serverPort: Option[Int]
profilingTime: Option[FiniteDuration]
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ object configuration {
* @param profilingEventsLogPath the path to the runtime events log file
* @param profilingPath the path to the profiling out file
* @param profilingTime the time limiting the profiling duration
* @param serverHost an address that the server listen on
* @param serverPort a port that the server listen on
*/
case class MainProcessConfig(
logLevel: LogLevel,
profilingEventsLogPath: Option[Path],
profilingPath: Option[Path],
profilingTime: Option[FiniteDuration],
serverHost: Option[String],
serverPort: Option[Int]
profilingTime: Option[FiniteDuration]
)

/** A configuration object for properties of the Project Manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ class BaseServerSpec extends JsonRpcServerTestKit with BeforeAndAfterAll {
logLevel = if (debugLogs) LogLevel.Trace else LogLevel.Off,
profilingPath = profilingPath,
profilingTime = None,
profilingEventsLogPath = None,
serverHost = None,
serverPort = None
profilingEventsLogPath = None
)

val testClock =
Expand Down

0 comments on commit dc9f902

Please sign in to comment.