site_generate.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/lua
  2. local uci = require('simple-uci').cursor()
  3. local json = require 'luci.jsonc'
  4. local sites_json = '/lib/gluon/site-select/sites.json'
  5. module('gluon.site_generate', package.seeall)
  6. function get_config(file)
  7. local decoder = json.new()
  8. local sink = decoder:sink()
  9. local f = assert(io.open(file))
  10. while true do
  11. local chunk = f:read(2048)
  12. if not chunk or chunk:len() == 0 then break end
  13. sink(chunk)
  14. end
  15. f:close()
  16. return assert(decoder:get())
  17. end
  18. function get_list()
  19. local list = {}
  20. local sites = get_config(sites_json)
  21. for index, site in pairs(sites) do
  22. list[site.site_code]=index
  23. end
  24. return list
  25. end
  26. local site_list=get_list()
  27. function validate_site(site_code)
  28. return site_list[site_code]
  29. end
  30. function set_site_code(site_code, force)
  31. if site_code and (force or validate_site(site_code)) then
  32. uci:set('currentsite', 'current', 'name', site_code)
  33. uci:save('currentsite')
  34. uci:commit('currentsite')
  35. return true
  36. end
  37. return false
  38. end
  39. function replace_patterns(value, subst)
  40. for k, v in pairs(subst) do
  41. value = value:gsub(k, v)
  42. end
  43. return value
  44. end