Skip to content

Commit

Permalink
@ teracyhq#493 | should validate plugin params before proceeding
Browse files Browse the repository at this point in the history
  • Loading branch information
hieptranquoc committed Oct 9, 2018
1 parent 065f6ad commit c556f84
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
73 changes: 57 additions & 16 deletions lib/teracy-dev/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,37 @@ def self.sync(plugins)

plugins.each do |plugin|

if !installed_plugins.has_key?(plugin['name']) and plugin['state'] == 'installed'
logger.info("installing plugin: #{plugin}")

if plugin['sources'].nil? or plugin['sources'].empty?
plugin['sources'] = [
"https://rubygems.org/",
"https://gems.hashicorp.com/"
]
end

plugin_manager.install_plugin(plugin['name'], Util.symbolize(plugin))
reload_required = true
name_validate = validate(plugin, 'name' => plugin['name'])
if !name_validate
logger.warn('Plugin name must be configured')
next
end

if installed_plugins.has_key?(plugin['name']) and plugin['state'] == 'uninstalled'
logger.info("uninstalling plugin: #{plugin['name']}")
plugin_manager.uninstall_plugin(plugin['name'])
reload_required = true
case plugin['state']
when 'installed'
if installed_plugins.empty?
source_validate = validate(plugin, 'sources' => plugin['sources'])
if !source_validate
plugin['sources'] = [
"https://rubygems.org/",
"https://gems.hashicorp.com/"
]
end
end

if !installed_plugins.has_key?(plugin['name'])
logger.info("installing plugin: #{plugin}")
plugin_manager.install_plugin(plugin['name'], Util.symbolize(plugin))
reload_required = true
end
when 'uninstalled'
if installed_plugins.has_key?(plugin['name']) and plugin['state'] == 'uninstalled'
logger.info("uninstalling plugin: #{plugin['name']}")
plugin_manager.uninstall_plugin(plugin['name'])
reload_required = true
end
else
logger.debug('Plugin state not be set, do nothing')
end
end

Expand All @@ -45,5 +58,33 @@ def self.sync(plugins)
def self.installed?(plugin_name)
return Vagrant.has_plugin?(plugin_name)
end

def self.validate(plugin, config)
result = true
if config.is_a? Hash
config.each do |key, val|
val = val.to_s
if !plugin.has_key? key
result = false
end

if val.empty? || val.nil?
result = false
end
end
elsif config.is_a? Array
config.each do |key|
if !plugin.has_key? key
result = false
end
end
else
if !plugin.has_key? config
result = false
end
end

result
end
end
end
14 changes: 7 additions & 7 deletions lib/teracy-dev/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def self.symbolize(obj)
return obj.reduce({}) do |memo, (k, v)|
memo.tap { |m| m[k.to_sym] = symbolize(v) }
end if obj.is_a? Hash
return obj.reduce([]) do |memo, v|

return obj.reduce([]) do |memo, v|
memo << symbolize(v); memo
end if obj.is_a? Array

obj
end

Expand Down Expand Up @@ -134,10 +134,10 @@ def self.override(originHash, sourceHash)
replaced_key = key.to_s.sub(/_u?[ra]_/, '')

if !originHash.has_key?(replaced_key)
if value.class.name == 'Hash'
originHash[key] = {}
elsif value.class.name == 'Array'
originHash[replaced_key] = []
if value.class.name == 'Hash'
originHash[key] = {}
elsif value.class.name == 'Array'
originHash[replaced_key] = []
end
end

Expand Down

0 comments on commit c556f84

Please sign in to comment.