class HeartTop::CLI
Attributes
interval[RW]
verbose[RW]
Public Class Methods
run()
click to toggle source
# File lib/heart_top/cli.rb, line 16 def run begin parse_args! rescue UnsupportedCommand => e print_error(e.message) exit end set_defaults print_details execute end
Private Class Methods
execute()
click to toggle source
# File lib/heart_top/cli.rb, line 30 def execute puts Rainbow("Executing #{@command} ...").green send("#{@command}") end
log_path()
click to toggle source
# File lib/heart_top/cli.rb, line 39 def log_path '/tmp/hearttop.log' end
parse_args!()
click to toggle source
# File lib/heart_top/cli.rb, line 71 def parse_args! opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT], ['--interval', '-i', GetoptLong::REQUIRED_ARGUMENT], ['--verbose', '-v', GetoptLong::NO_ARGUMENT]) @interval = nil @verboes = nil opts.each do |opt, arg| case opt when '--help' print_help exit(0) when '--interval' @interval = arg.to_i when '--verbose' @verbose = true end end @command = ARGV.shift return if supported_commands?(@command) raise UnsupportedCommand, Rainbow('Unsupported command detected, ').red + Rainbow(@command).green end
pid_path()
click to toggle source
# File lib/heart_top/cli.rb, line 35 def pid_path '/tmp/heartop.pid' end
print_details()
click to toggle source
# File lib/heart_top/cli.rb, line 97 def print_details return unless @verbose table = Terminal::Table.new do |t| t << [Rainbow('verbose').green, Rainbow(@verbose).green] t.add_row [Rainbow('interval').green, Rainbow(@interval).green] t.add_row [Rainbow('command').green, Rainbow(@command).green] end puts table end
print_error(msg)
click to toggle source
# File lib/heart_top/cli.rb, line 107 def print_error(msg) puts Rainbow(msg).red end
print_help()
click to toggle source
# File lib/heart_top/cli.rb, line 111 def print_help puts <<~EOF heartbeat [OPTION] --help, -h: show help --interval [interval], -i [interval]: interval, e.g: 10 (in seconds) EOF end
restart()
click to toggle source
# File lib/heart_top/cli.rb, line 43 def restart stop start end
set_defaults()
click to toggle source
# File lib/heart_top/cli.rb, line 66 def set_defaults @interval ||= 10 @verbose ||= false end
start()
click to toggle source
# File lib/heart_top/cli.rb, line 48 def start options = { interval: @interval, verbose: @verbose } Dante::Runner .new('hearttop') .execute(daemonize: true, pid_path: pid_path, log_path: log_path) do |_opts| HeartTop::Agent.new(options).run end end
stop()
click to toggle source
# File lib/heart_top/cli.rb, line 60 def stop Dante::Runner .new('hearttop') .execute(kill: true, pid_path: pid_path) end
supported_commands?(cmd)
click to toggle source
# File lib/heart_top/cli.rb, line 93 def supported_commands?(cmd) cmd.nil? || %w[stop start restart].include?(cmd) end