clear-config icon indicating copy to clipboard operation
clear-config copied to clipboard

Config report doesn't connect name-adjusted keys together

Open Daenyth opened this issue 1 year ago • 0 comments

For example:

  val port = ConfigDef.getOrUse[Port]("server.port", port"8085")
  def configSources[F[_]: Applicative]: ConfigSources[F] = {
    // Allow property names like `foo.bar` to map to environment variables like `FOO_BAR`
    // Taken from https://github.com/japgolly/clear-config/issues/132
    def propertyToEnv(k: ConfigKey): List[ConfigKey] =
      k :: k.map(_.toUpperCase.replace('.', '_')) :: Nil

    ConfigSource.environment[F].mapKeyQueries(propertyToEnv) >
      ConfigSource.propFile[F]("deploy.properties", optional = true)
      ConfigSource.system[F]
  }
  
  def readConfig[F[_]: MonadThrow](log: Logger[F]): F[MainConfig] =
    config.withReport
      .run(configSources[F])
      .flatMap(_.toEither.leftMap(e => new Exception(e) with NoStackTrace {}).liftTo[F])
      .flatMap { case (cfg, report) =>
        // Don't bother reporting unused environment variables, or "system" settings
        val reportMessage = report
          .mapUnused(_.withoutSources(ConfigSourceName.environment, ConfigSourceName.system))
          .full

        log
          .info(reportMessage)
          .as(cfg)
      }

Results in a config report like this:

Used keys (5):
+-----------------+------+-----------------------------------------------------------------+---------+
| Key             | Env  | /Users/gavin/blahblah/blahblahblahblahblah/local-dev.properties | Default |
+-----------------+------+-----------------------------------------------------------------+---------+
| SERVER_PORT     | 8089 |                                                                 |         |
| server.port     |      |                                                                 | 8085    |
+-----------------+------+-----------------------------------------------------------------+---------+

Daenyth avatar Jul 25 '23 15:07 Daenyth