fs2json: directory structure -> json object converter. default tip
authorTomas Zeman <tomas@functionals.cz>
Tue, 15 Dec 2020 09:22:21 +0100
changeset 60 4267602e8494
parent 59 a3e588dcdcb3
fs2json: directory structure -> json object converter. E.g. to be used instead of erica / py-Couchapp (interaction with couchdb is left to the user).
scala/fs2json/amm
scala/fs2json/fs2json.sc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scala/fs2json/amm	Tue Dec 15 09:22:21 2020 +0100
@@ -0,0 +1,31 @@
+#!/usr/bin/env sh
+
+# This is a wrapper script, that automatically download ammonite from GitHub release pages
+# You can give the required mill version with AMM_VERSION env variable
+# If no version is given, it falls back to the value of DEFAULT_AMM_VERSION
+DEFAULT_AMM_VERSION=2.3.8
+
+set -e
+
+if [ -z "$AMM_VERSION" ] ; then
+  AMM_VERSION=$DEFAULT_AMM_VERSION
+fi
+
+AMM_DOWNLOAD_PATH="$HOME/.ammonite/download"
+AMM_EXEC_PATH="${AMM_DOWNLOAD_PATH}/$AMM_VERSION"
+
+if [ ! -x "$AMM_EXEC_PATH" ] ; then
+  mkdir -p $AMM_DOWNLOAD_PATH
+  DOWNLOAD_FILE=$AMM_EXEC_PATH-tmp-download
+  AMM_DOWNLOAD_URL="https://github.com/lihaoyi/ammonite/releases/download/${AMM_VERSION%%-*}/2.13-$AMM_VERSION"
+  curl --fail -L -o "$DOWNLOAD_FILE" "$AMM_DOWNLOAD_URL"
+  chmod +x "$DOWNLOAD_FILE"
+  mv "$DOWNLOAD_FILE" "$AMM_EXEC_PATH"
+  unset DOWNLOAD_FILE
+  unset AMM_DOWNLOAD_URL
+fi
+
+unset AMM_DOWNLOAD_PATH
+unset AMM_VERSION
+
+exec $AMM_EXEC_PATH "$@"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scala/fs2json/fs2json.sc	Tue Dec 15 09:22:21 2020 +0100
@@ -0,0 +1,23 @@
+import ammonite.ops._
+import os.FileType.{Dir, File}
+import ujson.{Obj, Str, Value}
+
+import scala.collection.mutable
+
+def fs2json(p: Path): Obj = {
+  val l: Seq[(String, Value)] = ls(p) flatMap { f => f.fileType match {
+    case Dir => Some(f.last -> fs2json(f))
+    case File if f.last endsWith ".json" =>
+      Some(f.last.dropRight(5) -> ujson.read(os.read(f)))
+    case File =>
+      val n = f.last.split("\\.").toList match {
+        case v :: Nil => v
+        case v => v dropRight 1 mkString "."
+      }
+      Some(n -> Str(os.read(f).stripLineEnd))
+    case _ => None
+  }}
+  Obj(mutable.LinkedHashMap.from(l))
+}
+
+// vim: et sw=2 ts=2 syn=scala