Class: HCl::App
Constant Summary collapse
- HCL_DIR =
(ENV['HCL_DIR'] || "#{ENV['HOME']}/.hcl").freeze
- SETTINGS_FILE =
"#{HCL_DIR}/settings.yml".freeze
- ALIAS_LIST =
"#{HCL_DIR}/aliases".freeze
- CONFIG_FILE =
"#{HCL_DIR}/config.yml".freeze
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the value of attribute http.
Class Method Summary collapse
-
.command(*args) ⇒ Object
Run the given command and arguments.
Instance Method Summary collapse
-
#command?(command) ⇒ true, false
Return true if the string is a known command, false otherwise.
-
#initialize ⇒ App
constructor
A new instance of App.
- #process_args(*args) ⇒ Object
-
#run ⇒ Object
Start the application.
Methods included from Commands
#alias, #aliases, #cancel, #completion, #config, #console, #log, #note, #resume, #set, #show, #start, #status, #stop, #tasks, #unalias, #unset
Methods included from Utility
#as_hours, #current_time, #fail, #get_ident, #get_starting_time, #get_task, #get_task_ids, #time2float
Constructor Details
#initialize ⇒ App
19 20 21 22 23 24 |
# File 'lib/hcl/app.rb', line 19 def initialize FileUtils.mkdir_p(HCL_DIR) read_config read_settings self end |
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the value of attribute http.
17 18 19 |
# File 'lib/hcl/app.rb', line 17 def http @http end |
Class Method Details
.command(*args) ⇒ Object
Run the given command and arguments.
27 28 29 |
# File 'lib/hcl/app.rb', line 27 def self.command *args new.process_args(*args).run end |
Instance Method Details
#command?(command) ⇒ true, false
Return true if the string is a known command, false otherwise.
35 36 37 |
# File 'lib/hcl/app.rb', line 35 def command? command Commands.method_defined? command end |
#process_args(*args) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/hcl/app.rb', line 86 def process_args *args = Trollop::(args) do stop_on Commands.instance_methods version "HCl version #{VERSION}" "HCl is a command-line client for manipulating Harvest time sheets.\n\nCommands:\n# show all available tasks\nhcl tasks\n\n# create a task alias\nhcl alias <task_alias> <project_id> <task_id>\n\n# remove a task alias\nhcl unalias <task_alias>\n\n# list task aliases\nhcl aliases\n\n# start a task using an alias\nhcl [start] @<task_alias> [+<time>] [<message>]\n\n# add a line to a running timer\nhcl note <message>\n\n# stop a running timer\nhcl stop [<message>]\n\n# log a task and time without leaving a timer running\nhcl log @<task_alias> [+<time>] [<message>]\n\n# resume the last stopped timer or a specific task\nhcl resume [@<task_alias>]\n\n# delete the current or last running timer\nhcl (cancel | oops | nvm)\n\n# display the daily timesheet\nhcl [show [<date>]]\n\n# show your current credentials\nhcl config\n\n# display Harvest service status\nhcl status\n\nExamples:\nhcl alias mytask 1234 4567\nhcl @mytask +:15 Doing a thing that I started 15 minutes ago.\nhcl note Adding a note to my running task.\nhcl stop That's enough for now.\nhcl resume\nhcl show yesterday\nhcl show last tuesday\n\nOptions:\n" opt :reauth, "Force refresh of auth details" opt :changelog, "Review the HCl changelog" end @command = args.shift @args = args self end |
#run ⇒ Object
Start the application.
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hcl/app.rb', line 40 def run request_config if [:reauth] if [:changelog] system %[ more "#{File.join(File.dirname(__FILE__), '../../CHANGELOG.markdown')}" ] exit end begin if @command if command? @command result = send @command, *@args if not result.nil? if result.respond_to? :join, include_all=false puts result.join(', ') elsif result.respond_to? :to_s, include_all=false puts result end end else puts start(@command, *@args) end else puts show end rescue CommandError => e $stderr.puts e exit 1 rescue RuntimeError => e $stderr.puts "Error: #{e}" exit 1 rescue Faraday::Error => e $stderr.puts "Connection failed. (#{e.message})" exit 1 rescue HarvestMiddleware::ThrottleFailure => e $stderr.puts "Too many requests, retrying in #{e.retry_after+5} seconds..." sleep e.retry_after+5 run rescue HarvestMiddleware::AuthFailure => e $stderr.puts "Unable to authenticate: #{e}" request_config run rescue HarvestMiddleware::Failure => e $stderr.puts "API failure: #{e}" exit 1 end end |