Skip to content

Commit

Permalink
Generate INI-type configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
he-sk committed Sep 28, 2020
1 parent b554da8 commit fd29d53
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions peel-core/src/main/scala/org/peelframework/core/config/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ object Model {
}
}

class INI(val c: Config, val prefix: String) extends Model {

val sections = {
val sectionBuffer = ListBuffer[Section]()

def sanitize(s: String) =
s.stripPrefix(s"$prefix.") // remove prefix

def fixRoot(s: String) = if (s == "_root_") null else s

def collectPairs(c: Config, name: String): Unit = {
val buffer = ListBuffer[Pair]()

for (e <- c.entrySet().asScala) {
val key = sanitize(e.getKey)
.replace("\"_root_\"", "_root_")
.stripPrefix(s"$name.")
buffer += Pair(key, c.getString(e.getKey))
}

sectionBuffer += Section(fixRoot(name), buffer.toList.asJava)
}

for (e <- c.getObject(prefix).entrySet().asScala) {
val name = sanitize(e.getKey)
collectPairs(c.withOnlyPath(s"$prefix.$name"), name)
}

sectionBuffer.toList.asJava
}

}

/** A model for environment files (e.g., etc/hadoop/hadoop-env.sh).
*
* The children of the specified `prefix` path in the given `c` config are converted as (key, value) pairs in a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#sections}}{{#name}}[{{name}}]
{{/name}}
{{#entries}}
{{name}}={{value}}
{{/entries}}

{{/sections}}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<commons-io.version>1.3.2</commons-io.version>
<commons-lang.version>3.4</commons-lang.version>
<!-- Templating -->
<jmustache.version>1.9</jmustache.version>
<jmustache.version>1.15</jmustache.version>
<!-- Arguments parsing -->
<argparse4j.version>0.6.0</argparse4j.version>
</properties>
Expand Down

0 comments on commit fd29d53

Please sign in to comment.