summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db.lua17
-rwxr-xr-xfromdb.lua24
2 files changed, 41 insertions, 0 deletions
diff --git a/db.lua b/db.lua
index 4e7434a..0a9d0f4 100644
--- a/db.lua
+++ b/db.lua
@@ -12,6 +12,7 @@ local pcall = pcall
local io = require "io"
local iostdout = io.stdout
+local iostdin = io.stdin
local common = require "common"
local common_status = common.status
@@ -22,10 +23,25 @@ local errorln = common.errorln
local noiseln = common.noiseln
local debugln = common.debugln
+local json_internalize = function (jsdata)
+ local jsdec = cjson.new ()
+ local parsed = jsdec.decode (jsdata)
+ if parsed == nil then return status_error end
+ return status_ok, parsed
+end
+
-------------------------------------------------------------------------------
--- API
-------------------------------------------------------------------------------
+local read_json_db = function (cal, fh)
+ fh = fh or iostdin
+ local jsdata = fh:read "*all"
+ fh:close ()
+ if jsdata == nil then return status_error end
+ return json_internalize (jsdata)
+end
+
local output_calendar = function (cal, fh)
fh = fh or iostdout
local jsenc = cjson.new ()
@@ -39,5 +55,6 @@ local output_calendar = function (cal, fh)
end
return { output_calendar = output_calendar
+ , read_json_db = read_json_db
}
diff --git a/fromdb.lua b/fromdb.lua
new file mode 100755
index 0000000..cd17406
--- /dev/null
+++ b/fromdb.lua
@@ -0,0 +1,24 @@
+#!/usr/bin/env lua
+local rem = require "rem"
+local db = require "db"
+local common = require "common"
+
+local status = common.status
+local status_ok = status.codes.ok
+local status_error = status.codes.error
+
+common.set_verbosity (1)
+
+local main = function (argv)
+ local st, parsed = db.read_json_db ()
+ if st ~= status_ok then
+ return -1
+ end
+
+ local st = rem.output_calendar (parsed)
+
+ return st == status_ok and 0 or -1
+end
+
+os.exit (main ())
+