class Terraspace::CLI::Logs

Public Class Methods

new(options={}) click to toggle source
Calls superclass method Terraspace::CLI::Base::new
# File lib/terraspace/cli/logs.rb, line 8
def initialize(options={})
  super
  @action, @stack = options[:action], options[:stack]
  @action ||= '**'
  @stack  ||= '*'
end

Public Instance Methods

all() click to toggle source
# File lib/terraspace/cli/logs.rb, line 89
def all
  if single_log?
    @options[:all].nil? ? true : @options[:all]
  else # multiple
    @options[:all].nil? ? false : @options[:all]
  end
end
all_log_paths() click to toggle source
# File lib/terraspace/cli/logs.rb, line 61
def all_log_paths
  Dir.glob("#{Terraspace.log_root}/#{@action}/#{@stack}.log")
end
apply_limit(lines) click to toggle source
# File lib/terraspace/cli/logs.rb, line 83
def apply_limit(lines)
  return lines if all
  left = limit * -1
  lines[left..-1] || []
end
check_log!() click to toggle source

Only need to check if both action and stack are provided. Otherwise the Dir.globs are used to discover the files

# File lib/terraspace/cli/logs.rb, line 71
def check_log!
  return unless single_log?
  path = "#{Terraspace.log_root}/#{@action}/#{@stack}.log"
  return if File.exist?(path)
  puts "ERROR: Log file was not found: #{pretty(path)}".color(:red)
  exit 1
end
check_logs!() click to toggle source
# File lib/terraspace/cli/logs.rb, line 65
def check_logs!
  return unless all_log_paths.empty?
  puts "WARN: No logs found".color(:yellow)
end
follow_logs() click to toggle source
# File lib/terraspace/cli/logs.rb, line 24
def follow_logs
  glob_path = "#{Terraspace.log_root}/#{@action}/#{@stack}.log"
  Dir.glob(glob_path).each do |path|
    puts "Following #{pretty(path)}".color(:purple)
  end
  EventMachine.run do
    interval = Integer(ENV['TS_LOG_GLOB_INTERNAL'] || 1)
    EventMachine::FileGlobWatchTail.new(glob_path, nil, interval) do |filetail, line|
      puts line # always show timestamp in follow mode
    end
  end
end
format(line) click to toggle source
# File lib/terraspace/cli/logs.rb, line 53
def format(line)
  if timestamps
    line
  else
    line.sub(/.*\]: /,'')
  end
end
limit() click to toggle source
# File lib/terraspace/cli/logs.rb, line 97
def limit
  @options[:limit].nil? ? 10 : @options[:limit]
end
pretty(path) click to toggle source
# File lib/terraspace/cli/logs.rb, line 108
def pretty(path)
  Terraspace::Util.pretty_path(path)
end
report_log(path) click to toggle source
# File lib/terraspace/cli/logs.rb, line 46
def report_log(path)
  pretty_path = pretty(path)
  if File.exist?(path)
    puts "Showing: #{pretty_path}".color(:purple)
  end
end
run() click to toggle source
# File lib/terraspace/cli/logs.rb, line 15
def run
  check_logs!
  if @options[:follow]
    follow_logs
  else
    all_log_paths.each { |path| show_log(path) }
  end
end
show_log(path) click to toggle source
# File lib/terraspace/cli/logs.rb, line 37
def show_log(path)
  report_log(path)
  lines = readlines(path)
  lines = apply_limit(lines)
  lines.each do |line|
    puts format(line)
  end
end
single_log?() click to toggle source
# File lib/terraspace/cli/logs.rb, line 79
def single_log?
  @action != '**' && @stack != '*'
end
timestamps() click to toggle source
# File lib/terraspace/cli/logs.rb, line 101
def timestamps
  if single_log?
    @options[:timestamps].nil? ? false : @options[:timestamps]
  else
    @options[:timestamps].nil? ? true : @options[:timestamps]
  end
end