class Stax::Base
Public Instance Methods
app_name()
click to toggle source
# File lib/stax/base.rb, line 8 def app_name @_app_name ||= options[:app].empty? ? nil : cfn_safe(options[:app]) end
append(suffix, id)
click to toggle source
# File lib/stax/base.rb, line 85 def append(suffix, id) s = suffix.to_s id.end_with?(s) ? id : id + s end
aws_account_id()
click to toggle source
# File lib/stax/base.rb, line 20 def aws_account_id @_aws_account_id ||= Aws::Sts.id.account end
aws_region()
click to toggle source
# File lib/stax/base.rb, line 24 def aws_region @_aws_region ||= ENV['AWS_REGION'] end
branch_name()
click to toggle source
# File lib/stax/base.rb, line 12 def branch_name @_branch_name ||= cfn_safe(options[:branch]) end
cfn_safe(string)
click to toggle source
make string safe to use in naming CFN stuff
# File lib/stax/base.rb, line 76 def cfn_safe(string) string.gsub(/[\W_]/, '-') end
color(string, hash)
click to toggle source
# File lib/stax/base.rb, line 71 def color(string, hash) set_color(string, hash.fetch(string.to_sym, :yellow)) end
command(id)
click to toggle source
alias for stack to preserve semantics
# File lib/stax/base.rb, line 42 def command(id) stack(id) end
debug(message)
click to toggle source
# File lib/stax/base.rb, line 46 def debug(message) say "[DEBUG] #{message}", :blue end
ensure_env(*vars)
click to toggle source
fail unless given env vars are set
# File lib/stax/base.rb, line 65 def ensure_env(*vars) unless env_set?(*vars) fail_task("Please set env: #{vars.join(' ')}") end end
ensure_stack(*stacks)
click to toggle source
# File lib/stax/base.rb, line 35 def ensure_stack(*stacks) stacks.each do |s| stack(s)&.exists? or fail_task("#{s} stack is required") end end
env_set?(*vars)
click to toggle source
return true only if all given env vars are set
# File lib/stax/base.rb, line 60 def env_set?(*vars) vars.map{ |v| ENV.has_key?(v) }.all? end
fail_task(message, quit = true)
click to toggle source
# File lib/stax/base.rb, line 54 def fail_task(message, quit = true) say "[FAIL] #{message}", :red exit(1) if quit end
human_bytes(bytes, precision = 0)
click to toggle source
convert bytes to nearest unit
# File lib/stax/base.rb, line 128 def human_bytes(bytes, precision = 0) return 0.to_s if bytes < 1 {T: 1000*1000*1000*1000, G: 1000*1000*1000, M: 1000*1000, K: 1000, B: 1}.each do |unit, value| return "#{(bytes.to_f/value).round(precision)}#{unit}" if bytes >= value end end
human_time(timestamp)
click to toggle source
make epoch human-readable
# File lib/stax/base.rb, line 115 def human_time(timestamp) timestamp.nil? ? '-' : Time.at(timestamp.to_i/1000) end
human_time_diff(t, n = 5)
click to toggle source
convert a diff in seconds to d h m s
# File lib/stax/base.rb, line 120 def human_time_diff(t, n = 5) mm, ss = t.divmod(60) hh, mm = mm.divmod(60) dd, hh = hh.divmod(24) {d: dd, h: hh, m: mm, s: ss}.reject{ |_,v| v == 0 }.map{ |k,v| "#{v.round}#{k}" }.first(n).join end
os_open(*args)
click to toggle source
run desktop open command
# File lib/stax/base.rb, line 136 def os_open(*args) cmd = RUBY_PLATFORM.end_with?('linux') ? 'xdg-open' : 'open' system(cmd, *args) end
prepend(prefix, id)
click to toggle source
# File lib/stax/base.rb, line 80 def prepend(prefix, id) p = prefix.to_s id.start_with?(p) ? id : p + id end
stack(id)
click to toggle source
find or create a stack object
# File lib/stax/base.rb, line 29 def stack(id) c = id.to_s.split(/[_-]/).map(&:capitalize).join # convert symbol to class string object = Stax.const_get(c) ObjectSpace.each_object(object).first || object.new([], options) end
stack_prefix()
click to toggle source
# File lib/stax/base.rb, line 16 def stack_prefix @_stack_prefix ||= [app_name, branch_name].compact.join('-') + '-' end
stringify_keys(thing)
click to toggle source
# File lib/stax/base.rb, line 90 def stringify_keys(thing) if thing.is_a?(Hash) Hash[ thing.map { |k,v| [ k.to_s, stringify_keys(v) ] } ] elsif thing.respond_to?(:map) thing.map { |v| stringify_keys(v) } else thing end end
warn(message)
click to toggle source
# File lib/stax/base.rb, line 50 def warn(message) say "[WARNING] #{message}", :red end
y_or_n?(statement, color = nil)
click to toggle source
psycho version of thor yes?() that demands a y or n answer
# File lib/stax/base.rb, line 101 def y_or_n?(statement, color = nil) loop do case ask(statement, color, :add_to_history => false).downcase when 'y' return true when 'n' return false else puts "please respond 'y' or 'n'" end end end