Class: HCl
- Inherits:
-
Object
show all
- Includes:
- Utility
- Defined in:
- lib/hcl/task.rb,
lib/hcl/utility.rb,
lib/hcl/day_entry.rb,
lib/hcl/timesheet_resource.rb,
lib/hcl.rb
Defined Under Namespace
Modules: Utility
Classes: DayEntry, Project, Task, TimesheetResource, UnknownCommand
Constant Summary
collapse
- VERSION_FILE =
File.dirname(__FILE__) + '/../VERSION.yml'
- SETTINGS_FILE =
"#{ENV['HOME']}/.hcl_settings"
- CONFIG_FILE =
"#{ENV['HOME']}/.hcl_config"
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utility
#as_hours, #time2float
Constructor Details
#initialize ⇒ HCl
Returns a new instance of HCl.
60
61
62
63
64
|
# File 'lib/hcl.rb', line 60
def initialize
@version = YAML::load(File.read(VERSION_FILE))
read_config
read_settings
end
|
Class Method Details
.command(*args) ⇒ Object
37
38
39
|
# File 'lib/hcl.rb', line 37
def self.command *args
hcl = new.process_args(*args).run
end
|
Instance Method Details
#aliases ⇒ Object
173
174
175
|
# File 'lib/hcl.rb', line 173
def aliases
@settings.keys.select { |s| s =~ /^task\./ }.map { |s| s.slice(5..-1) }
end
|
#note(*args) ⇒ Object
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/hcl.rb', line 208
def note *args
message = args.join ' '
entry = DayEntry.with_timer
if entry
entry.append_note message
puts "Added note '#{message}' to #{entry}."
else
puts "No running timers found."
end
end
|
#process_args(*args) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/hcl.rb', line 70
def process_args *args
version_string = version
Trollop::options(args) do
version "HCl #{version_string}"
stop_on %w[ show tasks set unset note add rm start stop ]
banner <<-EOM
HCl is a command-line client for manipulating Harvest time sheets.
Commands:
hcl show [date]
hcl tasks
hcl aliases
hcl set <key> <value ...>
hcl start <task> [msg]
hcl stop [msg]
hcl note <msg>
Examples:
$ hcl tasks
$ hcl start 1234 4567 this is my log message
$ hcl set task.mytask 1234 4567
$ hcl start mytask this is my next log message
$ hcl show yesterday
$ hcl show last tuesday
Options:
EOM
end
@command = args.shift
@args = args
self
end
|
#read_config ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/hcl.rb', line 113
def read_config
if File.exists? CONFIG_FILE
config = YAML::load File.read(CONFIG_FILE)
TimesheetResource.configure config
elsif File.exists? old_conf = File.dirname(__FILE__) + "/../hcl_conf.yml"
config = YAML::load File.read(old_conf)
TimesheetResource.configure config
write_config config
else
config = {}
puts "Please specify your Harvest credentials.\n"
config['login'] = ask("Email Address: ")
config['password'] = ask("Password: ") { |q| q.echo = false }
config['subdomain'] = ask("Subdomain: ")
TimesheetResource.configure config
write_config config
end
end
|
#read_settings ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/hcl.rb', line 139
def read_settings
if File.exists? SETTINGS_FILE
@settings = YAML.load(File.read(SETTINGS_FILE))
else
@settings = {}
end
end
|
#run ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/hcl.rb', line 41
def run
if @command
if respond_to? @command
result = send @command, *@args
if not result.nil?
if result.respond_to? :to_a
puts result.to_a.join(', ')
elsif result.respond_to? :to_s
puts result
end
end
else
raise UnknownCommand, "unrecognized command `#{@command}'"
end
else
show
end
end
|
#set(key = nil, *args) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/hcl.rb', line 154
def set key = nil, *args
if key.nil?
@settings.each do |k, v|
puts "#{k}: #{v}"
end
else
value = args.join(' ')
@settings ||= {}
@settings[key] = value
write_settings
end
nil
end
|
#show(*args) ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/hcl.rb', line 219
def show *args
date = args.empty? ? nil : Chronic.parse(args.join(' '))
total_hours = 0.0
DayEntry.all(date).each do |day|
running = day.running? ? '(running) ' : ''
puts "\t#{day.formatted_hours}\t#{running}#{day.project} #{day.notes}"[0..78]
total_hours = total_hours + day.hours.to_f
end
puts "\t" + '-' * 13
puts "\t#{as_hours total_hours}\ttotal"
end
|
#start(*args) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/hcl.rb', line 177
def start *args
starting_time = args.detect {|x| x =~ /^\+\d*(\.|:)\d+$/ }
if starting_time
args.delete(starting_time)
starting_time = time2float starting_time
end
ident = args.shift
task_ids = if @settings.key? "task.#{ident}"
@settings["task.#{ident}"].split(/\s+/)
else
[ident, args.shift]
end
task = Task.find *task_ids
if task.nil?
puts "Unknown project/task alias, try one of the following: #{aliases.join(', ')}."
exit 1
end
timer = task.start(:starting_time => starting_time, :note => args.join(' '))
puts "Started timer for #{timer}."
end
|
#stop ⇒ Object
198
199
200
201
202
203
204
205
206
|
# File 'lib/hcl.rb', line 198
def stop
entry = DayEntry.with_timer
if entry
entry.toggle
puts "Stopped #{entry}."
else
puts "No running timers found."
end
end
|
#tasks ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'lib/hcl.rb', line 103
def tasks
tasks = Task.all
if tasks.empty?
puts "No cached tasks. Run `hcl show' to populate the cache and try again."
else
tasks.each { |task| puts "#{task.project.id} #{task.id}\t#{task}" }
end
nil
end
|
#unset(key) ⇒ Object
168
169
170
171
|
# File 'lib/hcl.rb', line 168
def unset key
@settings.delete key
write_settings
end
|
#version ⇒ Object
66
67
68
|
# File 'lib/hcl.rb', line 66
def version
[:major, :minor, :patch].map { |v| @version[v] }.join('.')
end
|
#write_config(config) ⇒ Object
132
133
134
135
136
137
|
# File 'lib/hcl.rb', line 132
def write_config config
puts "Writing configuration to #{CONFIG_FILE}."
File.open(CONFIG_FILE, 'w') do |f|
f.write config.to_yaml
end
end
|
#write_settings ⇒ Object
147
148
149
150
151
152
|
# File 'lib/hcl.rb', line 147
def write_settings
File.open(SETTINGS_FILE, 'w') do |f|
f.write @settings.to_yaml
end
nil
end
|