Code Coverage
 
Lines
Covered
27.85% covered (danger)
27.85%
22 / 79
1
require 'pathname'
1
module TheFox
1
module Timr
1
module Command
# Basic Class
1
class BasicCommand
1
include TheFox::Timr::Helper
1
include TheFox::Timr::Error
# Current Working Directory
1
attr_accessor :cwd
1
def initialize(argv = Array.new)
102
@cwd = nil
102
@timr = nil
end
# This is the actual execution of the Command.
1
def run
0
raise NotImplementedError
end
# Should be executed after `run` to gently save everything.
1
def shutdown
14
if @timr
0
@timr.shutdown
end
end
# All methods in this block are static.
1
class << self
1
include TheFox::Timr::Error
# Creates a new Command instance for each command string.
#
# For example, it returns a new StopCommand instance when `stop` String is provided by `argv` Array.
#
# Primary used by `bin/timr`.
1
def create_command_from_argv(argv)
# -C <path>
0
cwd_opt = Pathname.new("#{Dir.home}/.timr/defaultc").expand_path # Default Client
0
command_name = nil
0
command_argv = Array.new
0
loop_c = 0
0
while loop_c < 1024 && argv.length > 0
0
loop_c += 1
0
arg = argv.shift
0
if command_name
0
command_argv << arg
else
0
case arg
when '-h', '--help', 'help'
0
command_name = 'help'
when '-V', '--version'
0
command_name = 'version'
when '-C'
0
cwd_opt = Pathname.new(argv.shift).expand_path
when '--install-basepath'
0
timr_gem = Gem::Specification.find_by_name('timr')
0
print timr_gem.gem_dir
0
exit
else
0
if arg[0] == '-'
0
raise CommandError, "Unknown argument '#{arg}'. See 'timr --help'."
else
0
command_name = arg
end
end
end
end
0
command_class = get_command_class_by_name(command_name)
0
command = command_class.new(command_argv)
0
command.cwd = cwd_opt
0
command
end
# Get the Class for each command string.
1
def get_command_class_by_name(name)
0
case name
when 'help', '', nil
0
command = HelpCommand
when 'version'
0
command = VersionCommand
when 'status', 's'
0
command = StatusCommand
when 'start'
0
command = StartCommand
when 'stop'
0
command = StopCommand
when 'push'
0
command = PushCommand
when 'pop'
0
command = PopCommand
when 'continue', 'cont', 'c'
0
command = ContinueCommand
when 'pause', 'p'
0
command = PauseCommand
when 'log'
0
command = LogCommand
when 'task'
0
command = TaskCommand
when 'track'
0
command = TrackCommand
when 'report'
0
command = ReportCommand
when 'reset'
0
command = ResetCommand
else
0
raise CommandError, "'%s' is not a timr command. See 'timr --help'." % [name]
end
end
end
1
private
# Uses
# @timr
# @edit_opt
# @task_id_opt
# @track_id_opt
1
def run_edit(task_id = nil, track_id = nil)
0
task_id ||= @task_id_opt
0
track_id ||= @track_id_opt
0
if @timr && @edit_opt
0
edit_text = Array.new
0
if @message_opt
0
edit_text << @message_opt.clone
else
0
track = @timr.get_track_by_task_id(task_id, track_id)
0
if track && track.message
0
edit_text << track.message.clone
else
0
edit_text << @message_opt.clone
end
end
0
TerminalHelper.external_editor_help(edit_text)
0
editor_message = TerminalHelper.run_external_editor(edit_text)
0
if editor_message.length > 0
0
@message_opt = editor_message
end
end
end
1
def check_foreign_id(foreign_id)
16
if foreign_id && foreign_id.match(/ /)
0
foreign_id_without_spaces = foreign_id.gsub(' ', '')
0
raise ForeignIdError, "Foreign ID (--id) cannot include spaces. Maybe you want to use '#{foreign_id_without_spaces}' instead."
end
end
end # class Command
end # module Command
end # module Timr
end # module TheFox