Export and namespace Ansible.contains

This commit is contained in:
Simon Schuster 2016-09-02 21:56:19 +02:00
parent 5023d9c4da
commit 6fc8d3cfeb

View file

@ -1,7 +1,7 @@
local Ansible = {} local Ansible = {}
local io = require("io") local io = require("io")
local json = require("dkjson") local json = require("dkjson")
Ansible.__index = Ansible Ansible.__index = Ansible
@ -33,7 +33,7 @@ local function append(t1, t2)
return t1 return t1
end end
local function contains(needle, haystack) function Ansible.contains(needle, haystack)
for _,v in pairs(haystack) do for _,v in pairs(haystack) do
if needle == v then if needle == v then
return true return true
@ -51,7 +51,7 @@ local function findspec(name, spec)
-- check whether an alias exists -- check whether an alias exists
for k,v in pairs(spec) do for k,v in pairs(spec) do
if type(v) == "table" and v['aliases'] then if type(v) == "table" and v['aliases'] then
if contains(name, v['aliases']) then if Ansible.contains(name, v['aliases']) then
return v return v
end end
end end
@ -176,9 +176,9 @@ local function check_transform_type(variable, ansibletype)
local BOOLEANS_TRUE = {'yes', 'on', '1', 'true', 'True'} local BOOLEANS_TRUE = {'yes', 'on', '1', 'true', 'True'}
local BOOLEANS_FALSE = {'no', 'off', '0', 'false', 'False'} local BOOLEANS_FALSE = {'no', 'off', '0', 'false', 'False'}
if contains(variable, BOOLEANS_TRUE) then if Ansible.contains(variable, BOOLEANS_TRUE) then
return true return true
elseif contains(variable, BOOLEANS_FALSE) then elseif Ansible.contains(variable, BOOLEANS_FALSE) then
return false return false
end end
end end
@ -225,13 +225,13 @@ function Ansible:parse(inputfile)
params, err = canonicalize(params, self.spec) params, err = canonicalize(params, self.spec)
if not params then if not params then
self:fail_json({msg=err}) self:fail_json({msg="Err: " .. tostring(err)})
end end
for k,v in pairs(self.spec) do for k,v in pairs(self.spec) do
-- setup defaults -- setup defaults
if v['default'] then if v['default'] then
if not params[k] then if nil == params[k] then
params[k] = v['default'] params[k] = v['default']
end end
end end
@ -249,16 +249,16 @@ function Ansible:parse(inputfile)
local typedesc = self.spec[k]['type'] local typedesc = self.spec[k]['type']
if typedesc then if typedesc then
local val, err = check_transform_type(v, typedesc) local val, err = check_transform_type(v, typedesc)
if val then if nil ~= val then
params[k] = val params[k] = val
else else
self:fail_json({msg=err}) self:fail_json({msg="Err: " .. tostring(err)})
end end
end end
local choices = self.spec[k]['choices'] local choices = self.spec[k]['choices']
if choices then if choices then
if not contains(v, choices) then if not Ansible.contains(v, choices) then
self:fail_json({msg=v .. " not a valid choice for " .. k}) self:fail_json({msg=v .. " not a valid choice for " .. k})
end end
end end