Module: HCl::Commands

Included in:
App
Defined in:
lib/hcl/commands.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deprecate(command, explanation) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/hcl/commands.rb', line 8

def self.deprecate command, explanation
  private_command = :"_deprecated_#{command}"
  alias_method private_command, command
  define_method command do |*args|
    $stderr.puts "warning: #{command} command is deprecated, #{explanation}"
    send(private_command, *args)
  end
end

Instance Method Details

#alias(task_name, *value) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/hcl/commands.rb', line 95

def alias task_name, *value
  task = Task.find(*value)
  if task
    set "task.#{task_name}", *value
    "Added alias @#{task_name} for #{task}."
  else
    fail "Unrecognized project and task ID: #{value.inspect}"
  end
end

#aliasesObject



113
114
115
# File 'lib/hcl/commands.rb', line 113

def aliases
  @settings.keys.select { |s| s =~ /^task\./ }.map { |s| "@"+s.slice(5..-1) }
end

#cancelObject Also known as: oops, nvm



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hcl/commands.rb', line 67

def cancel
  entry = DayEntry.with_timer(http) || DayEntry.last(http)
  if entry
    confirmed = /^y/.match(ask("#{entry}\nDelete this entry? (y/n): ").downcase)
    return unless confirmed

    if entry.cancel http
      "Deleted entry #{entry}."
    else
      fail "Failed to delete #{entry}!"
    end
  else
    fail 'Nothing to cancel.'
  end
end

#completion(command = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/hcl/commands.rb', line 105

def completion command=nil
  command ||= $PROGRAM_NAME.split('/').last
  $stderr.puts \
    "The hcl completion command is deprecated (and slow!), instead use something like:",
    "> complete -W \"`cat #{HCl::App::ALIAS_LIST}`\" #{command}"
  %[complete -W "#{aliases.join ' '}" #{command}]
end

#configObject

Display a sanitized view of your auth credentials.



18
19
20
# File 'lib/hcl/commands.rb', line 18

def config
  http.config_hash.merge(password:'***').map {|k,v| "#{k}: #{v}" }.join("\n")
end

#consoleObject



35
36
37
38
# File 'lib/hcl/commands.rb', line 35

def console
  Console.new(self)
  nil
end

#log(*args) ⇒ Object



129
130
131
132
133
# File 'lib/hcl/commands.rb', line 129

def log *args
  fail "There is already a timer running." if DayEntry.with_timer(http)
  start(*args)
  stop
end

#note(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/hcl/commands.rb', line 166

def note *args
  entry = DayEntry.with_timer http
  if entry
    if args.empty?
      return entry.notes
    else
      entry.append_note http, args.join(' ')
      "Added note to #{entry}."
    end
  else
    fail "No running timers found."
  end
end

#resume(*args) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/hcl/commands.rb', line 194

def resume *args
  ident = get_ident args
  entry = if ident
      task_ids = get_task_ids ident, args
      DayEntry.last_by_task http, *task_ids
    else
      DayEntry.last(http)
    end
  if entry
    entry.toggle http
  else
    fail "No matching timer found."
  end
end

#set(key = nil, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hcl/commands.rb', line 53

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



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hcl/commands.rb', line 180

def show *args
  date = args.empty? ? nil : Chronic.parse(args.join(' '))
  total_hours = 0.0
  result = ''
  DayEntry.daily(http, date).each do |day|
    running = day.running? ? '(running) ' : ''
    columns = HighLine::SystemExtensions.terminal_size[0] rescue 80
    result << "\t#{day.formatted_hours}\t#{running}#{day.project}: #{day.notes.lines.to_a.last}\n"[0..columns-1]
    total_hours = total_hours + day.hours.to_f
  end
  result << ("\t" + '-' * 13) << "\n"
  result << "\t#{as_hours total_hours}\ttotal (as of #{current_time})\n"
end

#start(*args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hcl/commands.rb', line 117

def start *args
  starting_time = get_starting_time args
  task = get_task args
  if task.nil?
    fail "Unknown task alias, try one of the following: ", aliases.join(', ')
  end
  timer = task.start http,
    :starting_time => starting_time,
    :note => args.join(' ')
  "Started timer for #{timer} (at #{current_time})"
end

#statusObject

Show the network status of the Harvest service.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hcl/commands.rb', line 23

def status
  result = Faraday.new("https://kccljmymlslr.statuspage.io/api/v2") do |f|
    f.adapter Faraday.default_adapter
  end.get('status.json').body

  json = Yajl::Parser.parse result, symbolize_keys: true
  status = json[:status][:description]
  updated_at = DateTime.parse(json[:page][:updated_at]).strftime "%F %T %:z"

  "#{status} [#{updated_at}]"
end

#stop(*args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/hcl/commands.rb', line 155

def stop *args
  entry = DayEntry.with_timer(http) || DayEntry.with_timer(http, Date.today - 1)
  if entry
    entry.append_note(http, args.join(' ')) if args.any?
    entry.toggle http
    "Stopped #{entry} (at #{current_time})"
  else
    fail "No running timers found."
  end
end

#tasks(project_code = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hcl/commands.rb', line 40

def tasks project_code=nil
  tasks = Task.all
  if tasks.empty? # cache tasks
    DayEntry.today(http)
    tasks = Task.all
  end
  tasks.select! {|t| t.project.code == project_code } if project_code
  if tasks.empty?
    fail "No matching tasks."
  end
  tasks.map { |task| "#{task.project.id} #{task.id}\t#{task}" }.join("\n")
end

#toggle(*args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hcl/commands.rb', line 135

def toggle *args
  ident = get_ident args
  entry = if ident
      task_ids = get_task_ids ident, args
      DayEntry.last_by_task http, *task_ids
    else
      DayEntry.last(http)
    end
  if entry
    if entry.running?
      "Stopping #{entry} (at #{current_time})"
    else
      "Starting #{entry} (at #{current_time})"
    end
    entry.toggle http
  else
    fail "No timer found."
  end
end

#unalias(task) ⇒ Object



90
91
92
93
# File 'lib/hcl/commands.rb', line 90

def unalias task
  unset "task.#{task}"
  "Removed task alias @#{task}."
end

#unset(key) ⇒ Object



85
86
87
88
# File 'lib/hcl/commands.rb', line 85

def unset key
  @settings.delete key
  write_settings
end