Skip to content

Commit f2f50c5

Browse files
committed
Allow task to be optional when there is only one in a project.
1 parent 60c8a3f commit f2f50c5

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

exe/fa

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ def match type, collection, needle, *values
1818
found.first
1919
end
2020

21+
def select_task project, task_name
22+
tasks = api.tasks(project)
23+
if task_name.nil?
24+
raise ArgumentError, "Expecting only one task in project #{project.name}, found #{tasks.size}" if not tasks.one?
25+
tasks.first
26+
else
27+
match :task, tasks, task_name, :name
28+
end
29+
end
30+
2131
module Freeagent
2232
class Timeslips < Thor
2333
no_commands do
@@ -60,16 +70,16 @@ module Freeagent
6070
end
6171
end
6272

63-
desc 'create USER PROJECT TASK [FROM [TO]]', "Create timeslips"
73+
desc 'create USER PROJECT [TASK [FROM [TO]]]', "Create timeslips"
6474
method_option :hours, aliases: '-h', type: :numeric, default: 8, desc: "Number of hours per day"
6575
method_option :weekends, aliases: '-w', type: :boolean, default: false, desc: "Create timeslips on Saturdays and Sundays"
6676
method_option :comment, aliases: '-c', type: :string, default: nil, desc: "Comment to add to any created timeslips"
67-
def create user, project, task, from = nil, to = nil
77+
def create user, project, task = nil, from = nil, to = nil
6878
from = from.nil? ? Date::today : Date::parse(from)
6979
to = to.nil? ? from : Date::parse(to)
7080
user = match :user, api.users, user, :first_name, :last_name, :email
7181
project = match :project, api.projects, project, :name
72-
task = match :task, api.tasks(project), task, :name
82+
task = select_task project, task
7383

7484
puts "Creating timeslips for #{user.first_name} #{user.last_name} for task '#{task.name}' in project '#{project.name}' between #{from} and #{to} inclusive"
7585
timeslips = from.step(to).map do |date|
@@ -79,16 +89,16 @@ module Freeagent
7989
api.batch_create_timeslips timeslips
8090
end
8191

82-
desc 'fill USER PROJECT TASK [FROM [TO]]', 'Fill remaining hours with new timeslips'
92+
desc 'fill USER PROJECT [TASK [FROM [TO]]]', 'Fill remaining hours with new timeslips'
8393
method_option :hours, aliases: '-h', type: :numeric, default: 8, desc: "Number of hours per day"
8494
method_option :weekends, aliases: '-w', type: :boolean, default: false, desc: "Create timeslips on Saturdays and Sundays"
8595
method_option :comment, aliases: '-c', type: :string, default: nil, desc: "Comment to add to any created timeslips"
86-
def fill user, project, task, from = nil, to = nil
96+
def fill user, project, task = nil, from = nil, to = nil
8797
from = from.nil? ? Date::today : Date::parse(from)
8898
to = to.nil? ? from : Date::parse(to)
8999
user = match :user, api.users, user, :first_name, :last_name, :email
90100
project = match :project, api.projects, project, :name
91-
task = match :task, api.tasks(project), task, :name
101+
task = select_task project, task
92102

93103
existing = api.timeslips(user: user, from: from, to: to).reduce(Hash.new) do |hash, timeslip|
94104
date = Date::parse timeslip.dated_on

0 commit comments

Comments
 (0)