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
print_error(msg) click to toggle source
print_help() click to toggle source
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