Class: Terraspace::CLI::Logs
- Inherits:
-
Base
- Object
- Base
- Terraspace::CLI::Logs
show all
- Includes:
- Concern
- Defined in:
- lib/terraspace/cli/logs.rb,
lib/terraspace/cli/logs/concern.rb
Defined Under Namespace
Modules: Concern
Instance Method Summary
collapse
Methods included from Concern
#pid, #readlines
#pretty_path, #pretty_time
Methods included from Util::Sure
#sure?
#logger
Constructor Details
#initialize(options = {}) ⇒ Logs
Returns a new instance of Logs.
8
9
10
11
12
13
|
# File 'lib/terraspace/cli/logs.rb', line 8
def initialize(options={})
super
@action, @stack = options[:action], options[:stack]
@action ||= '**'
@stack ||= '*'
end
|
Instance Method Details
#all ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/terraspace/cli/logs.rb', line 89
def all
if single_log?
@options[:all].nil? ? true : @options[:all]
else @options[:all].nil? ? false : @options[:all]
end
end
|
#all_log_paths ⇒ Object
61
62
63
|
# File 'lib/terraspace/cli/logs.rb', line 61
def all_log_paths
Dir.glob("#{Terraspace.log_root}/#{@action}/#{@stack}.log")
end
|
#apply_limit(lines) ⇒ Object
83
84
85
86
87
|
# 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! ⇒ Object
Only need to check if both action and stack are provided. Otherwise the Dir.globs are used to discover the files
71
72
73
74
75
76
77
|
# 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! ⇒ Object
65
66
67
68
|
# 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 ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
|
# 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 end
end
end
|
53
54
55
56
57
58
59
|
# File 'lib/terraspace/cli/logs.rb', line 53
def format(line)
if timestamps
line
else
line.sub(/.*\]: /,'')
end
end
|
#limit ⇒ Object
97
98
99
|
# File 'lib/terraspace/cli/logs.rb', line 97
def limit
@options[:limit].nil? ? 10 : @options[:limit]
end
|
#report_log(path) ⇒ Object
46
47
48
49
50
51
|
# 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 ⇒ Object
15
16
17
18
19
20
21
22
|
# 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) ⇒ Object
37
38
39
40
41
42
43
44
|
# 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? ⇒ Boolean
79
80
81
|
# File 'lib/terraspace/cli/logs.rb', line 79
def single_log?
@action != '**' && @stack != '*'
end
|
#timestamps ⇒ Object
101
102
103
104
105
106
107
|
# 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
|