Browse Source

ffho-site-generate: update + code improvements

Karsten Böddeker 6 years ago
parent
commit
06b63d016c

+ 1 - 1
ffho/ffho-site-generate/Makefile

@@ -14,7 +14,7 @@ define Package/ffho-site-generate
   SECTION:=ffho
   CATEGORY:=Site-select
   TITLE:=Scripts for generating a new site.json
-  DEPENDS:=+gluon-core +luci-lib-json
+  DEPENDS:=+gluon-core
   MAINTAINER:=Freifunk Hochstift <kontakt@hochstift.freifunk.net>
 endef
 

+ 5 - 6
ffho/ffho-site-generate/luasrc/lib/gluon/upgrade/005-set-site-config

@@ -1,19 +1,18 @@
 #!/usr/bin/lua
 
-local uci = require('luci.model.uci').cursor()
-local json =  require 'luci.json'
+local uci = require('simple-uci').cursor()
 local site_code = require('gluon.site_config').site_code
 local tools = require 'gluon.site_generate'
 
-local config = io.open('/lib/gluon/site-select/template.json'):read('*a')
 local groups = tools.get_config('/lib/gluon/site-select/groups.json')
 local sites = tools.get_config('/lib/gluon/site-select/sites.json')
-local currentsite = uci:get("currentsite", "current", "name")
+local currentsite = uci:get('currentsite', 'current', 'name')
 
 if site_code ~= currentsite then
   local configured = false
   for _, site in pairs(sites) do
     if site.site_code == currentsite then
+      local config = io.open('/lib/gluon/site-select/template.json'):read('*a')
       local subst = {}
       subst['%%SN'] = site.site_name
       subst['%%SC'] = site.site_code
@@ -33,7 +32,7 @@ if site_code ~= currentsite then
       config = tools.replace_patterns(config, subst)
 
       local file = '/lib/gluon/site.json'
-      local f = io.open(file, "w")
+      local f = io.open(file, 'w')
       f:write(config)
       f:close()
 
@@ -43,6 +42,6 @@ if site_code ~= currentsite then
   end
 
   if configured == false then
-    tools.force_site_code(site_code)
+    tools.set_site_code(site_code, true)
   end
 end

+ 17 - 21
ffho/ffho-site-generate/luasrc/usr/lib/lua/gluon/site_generate.lua

@@ -1,20 +1,26 @@
 #!/usr/bin/lua
 
-local tool = {}
-local uci = require('luci.model.uci').cursor()
-local json =  require 'luci.json'
+local uci = require('simple-uci').cursor()
+local json =  require 'luci.jsonc'
 local sites_json = '/lib/gluon/site-select/sites.json'
 
 module('gluon.site_generate', package.seeall)
 
 function get_config(file)
-  local f = io.open(file)
-  if f then
-    local config = json.decode(f:read('*a'))
-    f:close()
-    return config
+  local decoder = json.new()
+  local sink = decoder:sink()
+
+  local f = assert(io.open(file))
+
+  while true do
+    local chunk = f:read(2048)
+    if not chunk ok chunk:len() == 0 then break end
+    sink(chunk)
   end
-  return nil
+
+  f:close()
+
+  return assert(decoder:get())
 end
 
 function get_list()
@@ -32,18 +38,8 @@ function validate_site(site_code)
   return site_list[site_code]
 end
 
-function force_site_code(site_code)
-  if site_code then
-    uci:set('currentsite', 'current', 'name', site_code)
-    uci:save('currentsite')
-    uci:commit('currentsite')
-    return true
-  end
-  return false
-end
-
-function set_site_code(site_code)
-  if site_code and validate_site(site_code) then
+function set_site_code(site_code, force)
+  if site_code and (force or validate_site(site_code)) then
     uci:set('currentsite', 'current', 'name', site_code)
     uci:save('currentsite')
     uci:commit('currentsite')