Skip to content

Commit

Permalink
Merge pull request #53 from eagletmt/avoid-string-ext
Browse files Browse the repository at this point in the history
Do not overwrite String core methods
  • Loading branch information
winebarrel authored Jul 30, 2023
2 parents d69de77 + 921b980 commit fe5a24b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
8 changes: 4 additions & 4 deletions bin/miam
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/miam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions lib/miam/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down
25 changes: 0 additions & 25 deletions lib/miam/ext/string_ext.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/miam/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions lib/miam/string_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fe5a24b

Please sign in to comment.