From fc1d021c227fb6fabae700a633a46d9293db1144 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 | 43 +++++++++++++++++++++++++--------------- lib/teracy-dev/util.rb | 14 ++++++------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/lib/teracy-dev/plugin.rb b/lib/teracy-dev/plugin.rb index fdd9ba8d..8458249d 100644 --- a/lib/teracy-dev/plugin.rb +++ b/lib/teracy-dev/plugin.rb @@ -15,24 +15,35 @@ 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 + unless Util.exist? plugin['name'] + 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? + unless Util.exist? plugin['sources'] + 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']) + 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 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