check_site_lib.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. local function loadvar(varname)
  2. local ok, val = pcall(assert(loadstring('return site.' .. varname)))
  3. if ok then
  4. return val
  5. else
  6. return nil
  7. end
  8. end
  9. local function array_to_string(array)
  10. local string = ''
  11. for _, v in ipairs(array) do
  12. if #string >= 1 then
  13. string = string .. ', '
  14. end
  15. string = string .. v
  16. end
  17. return '[' .. string .. ']'
  18. end
  19. local function assert_one_of(var, array, msg)
  20. for _, v in ipairs(array) do
  21. if v == var then
  22. return true
  23. end
  24. end
  25. error(msg)
  26. end
  27. local function assert_type(var, t, msg)
  28. assert(type(var) == t, msg)
  29. end
  30. function assert_uci_name(var)
  31. -- We don't use character classes like %w here to be independent of the locale
  32. assert(var:match('^[0-9a-zA-Z_]+$'), "site.conf error: `" .. var .. "' is not a valid config section name (only alphanumeric characters and the underscore are allowed)")
  33. end
  34. function need_string(varname, required)
  35. local var = loadvar(varname)
  36. if required == false and var == nil then
  37. return nil
  38. end
  39. assert_type(var, 'string', "site.conf error: expected `" .. varname .. "' to be a string")
  40. return var
  41. end
  42. function need_string_match(varname, pat, required)
  43. local var = need_string(varname, required)
  44. if not var then
  45. return nil
  46. end
  47. assert(var:match(pat), "site.conf error: expected `" .. varname .. "' to match pattern `" .. pat .. "'")
  48. return var
  49. end
  50. function need_number(varname, required)
  51. local var = loadvar(varname)
  52. if required == false and var == nil then
  53. return nil
  54. end
  55. assert_type(var, 'number', "site.conf error: expected `" .. varname .. "' to be a number")
  56. return var
  57. end
  58. function need_boolean(varname, required)
  59. local var = loadvar(varname)
  60. if required == false and var == nil then
  61. return nil
  62. end
  63. assert_type(var, 'boolean', "site.conf error: expected `" .. varname .. "' to be a boolean")
  64. return var
  65. end
  66. function need_array(varname, subcheck, required)
  67. local var = loadvar(varname)
  68. if required == false and var == nil then
  69. return nil
  70. end
  71. assert_type(var, 'table', "site.conf error: expected `" .. varname .. "' to be an array")
  72. for _, e in ipairs(var) do
  73. subcheck(e)
  74. end
  75. return var
  76. end
  77. function need_table(varname, subcheck, required)
  78. local var = loadvar(varname)
  79. if required == false and var == nil then
  80. return nil
  81. end
  82. assert_type(var, 'table', "site.conf error: expected `" .. varname .. "' to be a table")
  83. if subcheck then
  84. for k, v in pairs(var) do
  85. subcheck(k, v)
  86. end
  87. end
  88. return var
  89. end
  90. function need_one_of(varname, array, required)
  91. local var = loadvar(varname)
  92. if required == false and var == nil then
  93. return nil
  94. end
  95. assert_one_of(var, array, "site.conf error: expected `" .. varname .. "' to be one of given array: " .. array_to_string(array))
  96. return var
  97. end
  98. function need_string_array(varname, required)
  99. local ok, var = pcall(need_array, varname, function(e) assert_type(e, 'string') end, required)
  100. assert(ok, "site.conf error: expected `" .. varname .. "' to be a string array")
  101. return var
  102. end
  103. function need_string_array_match(varname, pat, required)
  104. local ok, var = pcall(need_array, varname, function(e) assert(e:match(pat)) end, required)
  105. assert(ok, "site.conf error: expected `" .. varname .. "' to be a string array matching pattern `" .. pat .. "'")
  106. return var
  107. end
  108. function need_array_of(varname, array, required)
  109. local ok, var = pcall(need_array, varname, function(e) assert_one_of(e, array) end,required)
  110. assert(ok, "site.conf error: expected `" .. varname .. "' to be a subset of given array: " .. array_to_string(array))
  111. return var
  112. end