site_generate.lua 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/lua
  2. local tool = {}
  3. local uci = require('luci.model.uci').cursor()
  4. local json = require 'luci.json'
  5. local sites_json = '/lib/gluon/site-select/sites.json'
  6. module('gluon.site_generate', package.seeall)
  7. function get_config(file)
  8. local f = io.open(file)
  9. if f then
  10. local config = json.decode(f:read('*a'))
  11. f:close()
  12. return config
  13. end
  14. return nil
  15. end
  16. function validate_site(site_code)
  17. local sites = get_config(sites_json)
  18. for _, site in pairs(sites) do
  19. if site.site_code == site_code then
  20. return true
  21. end
  22. end
  23. return false
  24. end
  25. function force_site_code(site_code)
  26. if site_code then
  27. uci:set('currentsite', 'current', 'name', site_code)
  28. uci:save('currentsite')
  29. uci:commit('currentsite')
  30. return true
  31. end
  32. return false
  33. end
  34. function set_site_code(site_code)
  35. if site_code and validate_site(site_code) then
  36. uci:set('currentsite', 'current', 'name', site_code)
  37. uci:save('currentsite')
  38. uci:commit('currentsite')
  39. return true
  40. end
  41. return false
  42. end