diff src/lib/Config.luan @ 3:43814e9f5802

add config
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 21 Oct 2023 22:52:41 -0600
parents
children 80105b716a62
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Config.luan	Sat Oct 21 22:52:41 2023 -0600
@@ -0,0 +1,35 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local parse = Luan.parse or error()
+local Db = require "site:/lib/Db.luan"
+
+
+local Config = {}
+
+local function get()
+	local doc = Db.get_document("type:config")
+	return doc and parse(doc.config)
+end
+
+function Config.get()
+	return get() or error "config not set"
+end
+
+function Config.get_for_config()
+	local config = get() or {}
+	config.discord = config.discord or {}
+	config.discord.client_id = config.discord.client_id or "replace"
+	config.discord.client_secret = config.discord.client_secret or "replace"
+	return config
+end
+
+function Config.set(text)
+	parse(text)  -- validate
+	Db.run_in_transaction( function()
+		local doc = Db.get_document("type:config") or {type="config"}
+		doc.config = text
+		Db.save(doc)
+	end )
+end
+
+return Config