lib/time_block/cli.rb in time_block-0.0.4 vs lib/time_block/cli.rb in time_block-0.1.0

- old
+ new

@@ -1,15 +1,96 @@ +# frozen_string_literal: true + require 'dante' require 'time_block' require 'time_block/agent' +require 'getoptlong' -class TimeBlock::CLI - def self.start(*args) - time = args.shift.strip.to_i - Dante::Runner.new('timeblock').execute(daemonize: true, pid_path: '/tmp/timeblock.pid', log_path: '/tmp/timblock.log') do |opts| - TimeBlock::Agent.new(time).run +module TimeBlock + class CLI + class << self + class UnsupportedCommand < StandardError; end + + def run + parse_args + set_defaults + execute + end + + private + + def execute + send(@command) + end + + def restart + stop + start + end + + def start + Dante::Runner + .new('timeblock') + .execute(daemonize: true, pid_path: pid_path, log_path: log_path) do |_opts| + TimeBlock::Agent.new(@time).run + end + end + + def stop + Dante::Runner + .new('timeblock') + .execute(kill: true, pid_path: pid_path) + end + + def pid_path + '/tmp/timeblock.pid' + end + + def log_path + '/tmp/timeblock.log' + end + + def set_defaults + @time ||= 60 + @verbose ||= false + end + + 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 + when '--verbose' + @verbose = true + end + end + + @command = ARGV.shift + return if supported_commands?(@command) + raise UnsupportedCommand, "Unsupported command detected, #{@command}" + end + + def supported_commands?(cmd) + cmd.nil? || %w[stop start restart].include?(cmd) + end + + def print_help + puts <<~EOF + heartbeat [OPTION] + --help, -h: + show help + --time [time], -i [time]: + interval, e.g: 60 (in seconds) + + EOF + end end - rescue => e - puts e.message - exit(1) end end