Skip to content

Commit

Permalink
optional
Browse files Browse the repository at this point in the history
  • Loading branch information
benjben committed Aug 14, 2023
1 parent a7e0027 commit 87a652c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@ object ConfigParser {
implicit private def logger[F[_]: Sync] = Slf4jLogger.getLogger[F]

def fromPath[F[_]: Sync, SinkConfig: Decoder](
configPath: Path
): EitherT[F, ExitCode, Config[SinkConfig]] =
configFromFile[F, Config[SinkConfig]](configPath)

private def configFromFile[F[_]: Sync, A: Decoder](path: Path): EitherT[F, ExitCode, A] = {
val eitherT = for {
text <- EitherT(readTextFrom[F](path))
hocon <- EitherT.fromEither[F](hoconFromString(text))
result <- EitherT.fromEither[F](resolve(hocon))
} yield result
configPath: Option[Path]
): EitherT[F, ExitCode, Config[SinkConfig]] = {
val eitherT = configPath match {
case Some(path) =>
for {
text <- EitherT(readTextFrom[F](path))
hocon <- EitherT.fromEither[F](hoconFromString(text))
result <- EitherT.fromEither[F](resolve[Config[SinkConfig]](hocon))
} yield result
case None =>
EitherT.fromEither[F](
for {
config <- Either
.catchNonFatal(namespaced(ConfigFactory.load()))
.leftMap(e => s"Error loading the configuration (without config file): ${e.getMessage}")
parsed <- config.as[Config[SinkConfig]].leftMap(_.show)
} yield parsed
)
}

eitherT.leftSemiflatMap { str =>
Logger[F].error(str).as(ExitCode.Error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ object Run {
appInfo: AppInfo,
mkSinks: Config.Streams[SinkConfig] => Resource[F, Sinks[F]]
): Opts[F[ExitCode]] = {
val configPath = Opts.option[Path]("config", "Path to HOCON configuration (optional)", "c", "config.hocon") //.orNone
val configPath = Opts.option[Path]("config", "Path to HOCON configuration (optional)", "c", "config.hocon").orNone
configPath.map(fromPath[F, SinkConfig](appInfo, mkSinks, _))
}

private def fromPath[F[_]: Async, SinkConfig: Decoder](
appInfo: AppInfo,
mkSinks: Config.Streams[SinkConfig] => Resource[F, Sinks[F]],
path: Path
path: Option[Path]
): F[ExitCode] = {
val eitherT = for {
config <- ConfigParser.fromPath[F, SinkConfig](path)
Expand Down

0 comments on commit 87a652c

Please sign in to comment.