diff --git a/bin/miam b/bin/miam index 6735bb1..27a4a33 100755 --- a/bin/miam +++ b/bin/miam @@ -92,7 +92,7 @@ ARGV.options do |opt| end end -String.colorize = options[:color] +Miam::StringHelper.colorize = options[:color] if options[:debug] Aws.config.update( @@ -181,14 +181,14 @@ begin updated = client.apply(file) - logger.info('No change'.intense_blue) unless updated + logger.info(Miam::StringHelper.intense_blue('No change')) unless updated end rescue => e if options[:debug] raise e else - $stderr.puts("[ERROR] #{e.message}".red) - $stderr.puts(e.backtrace.map(&:yellow)) if options[:show_backtrace] + $stderr.puts(Miam::StringHelper.red("[ERROR] #{e.message}")) + $stderr.puts(e.backtrace.map { |bt| Miam::StringHelper.yellow(bt) }) if options[:show_backtrace] exit 1 end end diff --git a/lib/miam.rb b/lib/miam.rb index 0ecd1e6..f2e0386 100644 --- a/lib/miam.rb +++ b/lib/miam.rb @@ -15,7 +15,7 @@ require 'hashie' module Miam; end -require 'miam/ext/string_ext' +require 'miam/string_helper' require 'miam/ext/hash_ext' require 'miam/logger' require 'miam/template_helper' diff --git a/lib/miam/driver.rb b/lib/miam/driver.rb index 1e18043..a6dbbdc 100644 --- a/lib/miam/driver.rb +++ b/lib/miam/driver.rb @@ -107,7 +107,7 @@ def update_login_profile(user_name, attrs, old_attrs) log_attrs.delete(:password) log(:info, "Update User `#{user_name}`", :color => :green) - log(:info, " login profile:\n".green + Miam::Utils.diff(old_attrs, attrs, :color => @options[:color], :indent => ' '), :color => false) + log(:info, Miam::StringHelper.green(" login profile:\n") + Miam::Utils.diff(old_attrs, attrs, :color => @options[:color], :indent => ' '), :color => false) unless_dry_run do @iam.update_login_profile(attrs.merge(:user_name => user_name)) @@ -287,13 +287,13 @@ def delete_instance_profile(instance_profile_name, attrs, roles_in_instance_prof def update_name(type, user_or_group_name, new_name) log(:info, "Update #{Miam::Utils.camelize(type.to_s)} `#{user_or_group_name}`", :color => :green) - log(:info, " name:\n".green + Miam::Utils.diff(user_or_group_name, new_name, :color => @options[:color], :indent => ' '), :color => false) + log(:info, Miam::StringHelper.green(" name:\n") + Miam::Utils.diff(user_or_group_name, new_name, :color => @options[:color], :indent => ' '), :color => false) update_user_or_group(type, user_or_group_name, "new_#{type}_name".to_sym => new_name) end def update_path(type, user_or_group_name, new_path, old_path) log(:info, "Update #{Miam::Utils.camelize(type.to_s)} `#{user_or_group_name}`", :color => :green) - log(:info, " path:\n".green + Miam::Utils.diff(old_path, new_path, :color => @options[:color], :indent => ' '), :color => false) + log(:info, Miam::StringHelper.green(" path:\n") + Miam::Utils.diff(old_path, new_path, :color => @options[:color], :indent => ' '), :color => false) update_user_or_group(type, user_or_group_name, :new_path => new_path) end diff --git a/lib/miam/ext/string_ext.rb b/lib/miam/ext/string_ext.rb deleted file mode 100644 index 32e0b30..0000000 --- a/lib/miam/ext/string_ext.rb +++ /dev/null @@ -1,25 +0,0 @@ -class String - @@colorize = false - - class << self - def colorize=(value) - @@colorize = value - end - - def colorize - @@colorize - end - end # of class methods - - Term::ANSIColor::Attribute.named_attributes.map do |attribute| - class_eval(<<-EOS, __FILE__, __LINE__ + 1) - def #{attribute.name} - if @@colorize - Term::ANSIColor.send(#{attribute.name.inspect}, self) - else - self - end - end - EOS - end -end diff --git a/lib/miam/logger.rb b/lib/miam/logger.rb index f127aea..129cafb 100644 --- a/lib/miam/logger.rb +++ b/lib/miam/logger.rb @@ -19,7 +19,7 @@ module Helper def log(level, message, log_options = {}) message = "[#{level.to_s.upcase}] #{message}" unless level == :info message << ' (dry-run)' if @options[:dry_run] - message = message.send(log_options[:color]) if log_options[:color] + message = Miam::StringHelper.public_send(log_options[:color], message) if log_options[:color] logger = @options[:logger] || Miam::Logger.instance logger.send(level, message) end diff --git a/lib/miam/string_helper.rb b/lib/miam/string_helper.rb new file mode 100644 index 0000000..4b836f4 --- /dev/null +++ b/lib/miam/string_helper.rb @@ -0,0 +1,25 @@ +module Miam + module StringHelper + @colorize = false + + class << self + def colorize=(value) + @colorize = value + end + + def colorize + @colorize + end + + Term::ANSIColor::Attribute.named_attributes.map do |attribute| + define_method(attribute.name) do |str| + if colorize + Term::ANSIColor.public_send(attribute.name, str) + else + str + end + end + end + end + end +end