Class: HarvestTimeOff::CLI
- Inherits:
-
Object
- Object
- HarvestTimeOff::CLI
- Defined in:
- lib/harvest_time_off.rb
Class Method Summary collapse
- .holiday_regions_from_environment ⇒ Object
- .option_parser(options) ⇒ Object
- .print_dry_run(output, dates, options) ⇒ Object
- .resolve_assignment(client, project_name, task_name) ⇒ Object
- .run(arguments, output: $stdout, error: $stderr, client: nil) ⇒ Object
- .validate!(dates, options) ⇒ Object
Class Method Details
.holiday_regions_from_environment ⇒ Object
106 107 108 |
# File 'lib/harvest_time_off.rb', line 106 def self.holiday_regions_from_environment ENV.fetch("HARVEST_HOLIDAY_REGIONS", "ca_yt").split(",").map { |region| region.strip.downcase }.reject(&:empty?) end |
.option_parser(options) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/harvest_time_off.rb', line 70 def self.option_parser() OptionParser.new do |opts| opts. = <<~USAGE Usage: harvest-time-off FROM TO --project NAME --task NAME [options] harvest-time-off FROM TO --project-id ID --task-id ID [options] Creates one Harvest duration entry for each local business day from FROM through TO. USAGE opts.on("--project NAME", "Harvest project name") { |value| [:project] = value } opts.on("--task NAME", "Harvest task name") { |value| [:task] = value } opts.on("--project-id ID", Integer, "Harvest project ID") { |value| [:project_id] = value } opts.on("--task-id ID", Integer, "Harvest task ID") { |value| [:task_id] = value } opts.on("--hours HOURS", Float, "Hours per day (default: 7)") { |value| [:hours] = value } opts.on("--notes NOTES", "Optional note on every entry") { |value| [:notes] = value } opts.on("--holiday-region REGION", "Holidays region; repeat for each locality") { |value| [:holiday_regions] << value.strip.downcase } opts.on("--dry-run", "Print entries without calling Harvest") { [:dry_run] = true } opts.on("-h", "--help", "Show this help") do puts opts exit 0 end end end |
.print_dry_run(output, dates, options) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/harvest_time_off.rb', line 125 def self.print_dry_run(output, dates, ) project = [:project] || "project ##{[:project_id]}" task = [:task] || "task ##{[:task_id]}" notes = [:notes] ? "; #{[:notes]}" : "" dates.each do |date| output.puts "Would create #{date.iso8601}: #{HarvestTimeOff.display_hours([:hours])}h on #{project} / #{task}#{notes}" end end |
.resolve_assignment(client, project_name, task_name) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/harvest_time_off.rb', line 110 def self.resolve_assignment(client, project_name, task_name) # ponytail: one 2,000-item page covers personal assignments; follow cursor pagination if that ceiling is exceeded. matches = client.active_personal_task_assignments.select do |assignment| assignment.dig("project", "name")&.casecmp?(project_name) && assignment.dig("task", "name")&.casecmp?(task_name) end return [matches.first.dig("project", "id"), matches.first.dig("task", "id")] if matches.one? qualifier = "project #{project_name.inspect} and task #{task_name.inspect}" raise Error, "No active task assignment matches #{qualifier}. Pass --project-id and --task-id instead." if matches.empty? raise Error, "Multiple active task assignments match #{qualifier}. Pass --project-id and --task-id instead." end |
.run(arguments, output: $stdout, error: $stderr, client: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/harvest_time_off.rb', line 30 def self.run(arguments, output: $stdout, error: $stderr, client: nil) = { hours: 7.0, dry_run: false, holiday_regions: holiday_regions_from_environment } parser = option_parser() dates = parser.parse(arguments) validate!(dates, ) from = Date.iso8601(dates[0]) to = Date.iso8601(dates[1]) workdays = HarvestTimeOff.dates_between(from, to, holiday_regions: [:holiday_regions]) raise Error, "date range contains no days to enter" if workdays.empty? if [:dry_run] print_dry_run(output, workdays, ) return 0 end client ||= Marlens::HarvestApiV2::Client.from_environment project_id, task_id = if [:project_id] [[:project_id], [:task_id]] else resolve_assignment(client, [:project], [:task]) end workdays.each do |date| entry = client.create_time_entry( project_id:, task_id:, spent_date: date, hours: [:hours], notes: [:notes] ) output.puts "Created #{date.iso8601}: #{HarvestTimeOff.display_hours([:hours])}h (entry ##{entry.fetch("id")})" end 0 rescue Error, Marlens::HarvestApiV2::Error, OptionParser::ParseError, Date::Error => e error.puts "Error: #{e.}" error.puts parser if parser 1 end |
.validate!(dates, options) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/harvest_time_off.rb', line 93 def self.validate!(dates, ) raise Error, "FROM and TO are required" unless dates.length == 2 raise Error, "--hours must be a positive finite number" unless [:hours].positive? && [:hours].finite? raise Error, "--holiday-region or HARVEST_HOLIDAY_REGIONS is required" if [:holiday_regions].empty? names_provided = [:project] || [:task] ids_provided = [:project_id] || [:task_id] valid_names = [:project] && [:task] valid_ids = [:project_id] && [:task_id] raise Error, "supply --project and --task, or --project-id and --task-id" unless valid_names || valid_ids raise Error, "do not combine project/task names with IDs" if names_provided && ids_provided end |