0003-scripts-add-function-needed_var_in_array-to-check_si.patch 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. From: Karsten Böddeker <freifunk@kb-light.de>
  2. Date: Thu, 30 Jun 2016 21:40:28 +0200
  3. Subject: [PATCH 1/2] scripts: add function needed_var_in_array to
  4. check_site_lib.lua
  5. The function need_var_in_array(varname, array, required) checks weather a value of a variable (specified by its name) is included in an array.
  6. If the variable is a table or array, the function checks the value of each element against the given array.
  7. ---
  8. scripts/check_site_lib.lua | 23 +++++++++++++++++++++++
  9. 1 file changed, 23 insertions(+)
  10. diff --git a/scripts/check_site_lib.lua b/scripts/check_site_lib.lua
  11. index 766b94a..9d581e9 100644
  12. --- a/scripts/check_site_lib.lua
  13. +++ b/scripts/check_site_lib.lua
  14. @@ -103,3 +103,26 @@ function need_string_array(varname, required)
  15. return assert(pcall(need_array, varname, function(e) assert_type(e, 'string') end, required),
  16. "site.conf error: expected `" .. varname .. "' to be a string array")
  17. end
  18. +
  19. +function need_var_in_array(varname, array, required)
  20. + local var = loadvar(varname)
  21. +
  22. + if required == false and var == nil then
  23. + return nil
  24. + end
  25. +
  26. + function var_in_array(var, array)
  27. + for _, v in ipairs(array) do
  28. + if v == var then
  29. + return true
  30. + end
  31. + end
  32. + return false
  33. + end
  34. +
  35. + for _,v in pairs(var) do
  36. + assert(var_in_array(v, array), "site.conf error: `" .. v .. "' is not a valid value for " .. varname)
  37. + end
  38. +
  39. + return var
  40. +end
  41. --
  42. 2.1.4