class TimeBlock::CLI

Public Class Methods

run() click to toggle source
# File lib/time_block/cli.rb, line 13
def run
  parse_args!
  set_defaults
  execute
rescue UnsupportedCommand => e
  puts e.message
end

Private Class Methods

execute() click to toggle source
# File lib/time_block/cli.rb, line 23
def execute
  send(@command)
end
log_path() click to toggle source
# File lib/time_block/cli.rb, line 51
def log_path
  '/tmp/timeblock.log'
end
parse_args!() click to toggle source
# File lib/time_block/cli.rb, line 60
def parse_args!
  opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT],
                        ['--time', '-t', GetoptLong::REQUIRED_ARGUMENT],
                        ['--verbose', '-v', GetoptLong::NO_ARGUMENT])
  @time = nil
  @verboes = nil
  opts.each do |opt, arg|
    case opt
    when '--help'
      print_help
      exit(0)
    when '--time'
      @time = arg.to_i
      if arg.chars.last == 'm'
        @time *= 60
      elsif arg.chars.last == 'h'
        @time *= 60 * 24
      end
    when '--verbose'
      @verbose = true
    end
  end

  @command = ARGV.shift
  return if supported_commands?(@command)
  raise UnsupportedCommand, "Unsupported command detected, #{@command}"
end
pid_path() click to toggle source
# File lib/time_block/cli.rb, line 47
def pid_path
  '/tmp/timeblock.pid'
end
print_help() click to toggle source
restart() click to toggle source
# File lib/time_block/cli.rb, line 27
def restart
  stop
  start
end
set_defaults() click to toggle source
# File lib/time_block/cli.rb, line 55
def set_defaults
  @time ||= 60
  @verbose ||= false
end
start() click to toggle source
# File lib/time_block/cli.rb, line 32
def start
  puts "Time #{@time}s ..."
  Dante::Runner
    .new('timeblock')
    .execute(daemonize: true, pid_path: pid_path, log_path: log_path) do |_opts|
    TimeBlock::Agent.new(@time).run
  end
end
stop() click to toggle source
# File lib/time_block/cli.rb, line 41
def stop
  Dante::Runner
    .new('timeblock')
    .execute(kill: true, pid_path: pid_path)
end
supported_commands?(cmd) click to toggle source
# File lib/time_block/cli.rb, line 88
def supported_commands?(cmd)
  %w[stop start restart].include?(cmd)
end