005-set-site-config 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/lua
  2. local uci = require('luci.model.uci').cursor()
  3. local json = require 'luci.json'
  4. local site_code = require('gluon.site_config').site_code
  5. local tools = require 'gluon.site_generate'
  6. function add_var_to_table(table, var)
  7. if type(var) == "table" and type(table) == "table" then
  8. for name, value in pairs(var) do
  9. if table[name] and type(value) == "table" then
  10. table[name] = add_var_to_table(table[name], value)
  11. else
  12. table[name]=value
  13. end
  14. end
  15. end
  16. return table
  17. end
  18. local default = tools.get_config('/lib/gluon/site-select/default.json')
  19. local groups = tools.get_config('/lib/gluon/site-select/groups.json')
  20. local sites = tools.get_config('/lib/gluon/site-select/sites.json')
  21. local currentsite = uci:get("currentsite", "current", "name")
  22. if site_code ~= currentsite then
  23. local configured = false
  24. for _, site in pairs(sites) do
  25. if site.site_code == currentsite then
  26. if site.site_select and site.site_select.group and groups and groups[site.site_select.group] then
  27. default = add_var_to_table(default, groups[site.site_select.group])
  28. end
  29. default = add_var_to_table(default, site)
  30. local file = '/lib/gluon/site.json'
  31. local f = io.open(file, "w")
  32. f:write(json.encode(default))
  33. f:close()
  34. configured = true
  35. break
  36. end
  37. end
  38. if configured == false then
  39. tools.force_site_code(site_code)
  40. end
  41. end