From 7ff558d37ab0ef6225a8674ebbbd975425dfe558 Mon Sep 17 00:00:00 2001 From: hieptranquoc Date: Tue, 9 Oct 2018 01:40:11 +0700 Subject: [PATCH] @ #493 | should validate plugin params before proceeding --- lib/teracy-dev/plugin.rb | 73 +++++++++++++++++++++++++++++++--------- lib/teracy-dev/util.rb | 14 ++++---- 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/lib/teracy-dev/plugin.rb b/lib/teracy-dev/plugin.rb index fdd9ba8d..2a531de0 100644 --- a/lib/teracy-dev/plugin.rb +++ b/lib/teracy-dev/plugin.rb @@ -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('The plugin state is not set, no action will be taken') end end @@ -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 diff --git a/lib/teracy-dev/util.rb b/lib/teracy-dev/util.rb index 342194b5..d5aa4df1 100644 --- a/lib/teracy-dev/util.rb +++ b/lib/teracy-dev/util.rb @@ -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 @@ -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