site_generate.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 get_list()
  17. local list = {}
  18. local sites = get_config(sites_json)
  19. for index, site in pairs(sites) do
  20. list[site.site_code]=index
  21. end
  22. return list
  23. end
  24. local site_list=get_list()
  25. function validate_site(site_code)
  26. return site_list[site_code]
  27. end
  28. function force_site_code(site_code)
  29. if site_code then
  30. uci:set('currentsite', 'current', 'name', site_code)
  31. uci:save('currentsite')
  32. uci:commit('currentsite')
  33. return true
  34. end
  35. return false
  36. end
  37. function set_site_code(site_code)
  38. if site_code and validate_site(site_code) then
  39. uci:set('currentsite', 'current', 'name', site_code)
  40. uci:save('currentsite')
  41. uci:commit('currentsite')
  42. return true
  43. end
  44. return false
  45. end
  46. function replace_patterns(value, subst)
  47. for k, v in pairs(subst) do
  48. value = value:gsub(k, v)
  49. end
  50. return value
  51. end