Skip to content

Instantly share code, notes, and snippets.

@kastoestoramadus
Last active April 25, 2025 14:25
Show Gist options
  • Save kastoestoramadus/8d2143bdcb49fbb163fdd0f99a39fbc0 to your computer and use it in GitHub Desktop.
Save kastoestoramadus/8d2143bdcb49fbb163fdd0f99a39fbc0 to your computer and use it in GitHub Desktop.
second
package org.ekrich.config
import java.io.File
import java.nio.file.{Path, Paths}
import java.nio.file.{Files, Paths}
import java.nio.charset.StandardCharsets
object Formatter {
def main(args: Array[String]): Unit = {
println(s"Running HOCON formatter. pwd: ${new File(".").getAbsolutePath}")
assert(args.length > 0)
val targetFilePathStr = args(0)
reformatFile(targetFilePathStr)
}
val parseOptions = ConfigParseOptions.defaults.setAllowMissing(true)
val renderOptions = ConfigRenderOptions.defaults
.setJson(false)
.setOriginComments(false)
.setComments(true)
.setFormatted(true)
def fmtFileToStr(filePathStr: String): String =
ConfigFactory
.parseFile(new File(filePathStr), parseOptions)
.root
.render(renderOptions)
def reformatFile(filePathStr: String): Unit = {
val formattedStr = fmtFileToStr(filePathStr)
Files.write(
Paths.get(filePathStr),
formattedStr.getBytes(StandardCharsets.UTF_8)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment