Procházet zdrojové kódy

ffho-site-generate: toolbox for site generation and validation added

Karsten Böddeker před 8 roky
rodič
revize
a05a6cafc2

+ 6 - 17
ffho/ffho-site-generate/files/lib/gluon/upgrade/005-set-site-config

@@ -3,16 +3,7 @@
 local uci = require('luci.model.uci').cursor()
 local json =  require 'luci.json'
 local site_code = require('gluon.site_config').site_code
-
-function get_config(file)
-  local f = io.open(file)
-  if f then
-    local config = json.decode(f:read('*a'))
-    f:close()
-    return config
-  end
-  return nil
-end
+local tools = require 'gluon.site_generate'
 
 function add_var_to_table(table, var)
   if type(var) == "table" and type(table) == "table" then
@@ -27,14 +18,14 @@ function add_var_to_table(table, var)
   return table
 end
 
-local default = get_config('/lib/gluon/site-select/default.json')
-local groups = get_config('/lib/gluon/site-select/groups.json')
-local sites = get_config('/lib/gluon/site-select/sites.json')
+local default = tools.get_config('/lib/gluon/site-select/default.json')
+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")
 
 if site_code ~= currentsite then
   local configured = false
-  for index, site in pairs(sites) do
+  for _, site in pairs(sites) do
     if site.site_code == currentsite then
       if site.site_select and site.site_select.group and groups and groups[site.site_select.group] then
         default = add_var_to_table(default, groups[site.site_select.group])
@@ -52,8 +43,6 @@ if site_code ~= currentsite then
   end
 
   if configured == false then
-    uci:set("currentsite", "current", "name", site_code)
-    uci:save('currentsite')
-    uci:commit('currentsite')
+    tools.force_site_code(site_code)
   end
 end

+ 43 - 0
ffho/ffho-site-generate/files/usr/lib/lua/gluon/site_generate.lua

@@ -0,0 +1,43 @@
+#!/usr/bin/lua
+
+module('gluon.site_generate')
+
+local uci = require('luci.model.uci').cursor()
+local json =  require 'luci.json'
+
+function get_config(file)
+  local f = io.open(file)
+  if f then
+    local config = json.decode(f:read('*a'))
+    f:close()
+    return config
+  end
+  return nil
+end
+
+function force_site_code(site_code)
+  uci:set('currentsite', 'current', 'name', site_code)
+  uci:save('currentsite')
+  uci:commit('currentsite')
+end
+
+function validate_site(site_code)
+  local sites = get_config('/lib/gluon/site-select/sites.json')
+  for _, site in pairs(sites) do
+    if site.site_code == site_code then
+      return true
+    end
+  end
+  return false
+end
+
+function set_site_code(site_code)
+  if site_code and is_site_valid(site_code) then
+    uci:set('currentsite', 'current', 'name', site_code)
+    uci:save('currentsite')
+    uci:commit('currentsite')
+    return true
+  end
+  return false
+end
+