lib/chronicle/shell/shell_history_extractor.rb in chronicle-shell-0.2.1 vs lib/chronicle/shell/shell_history_extractor.rb in chronicle-shell-0.2.2
- old
+ new
@@ -4,14 +4,14 @@
module Shell
class ShellHistoryExtractor < Chronicle::ETL::Extractor
register_connector do |r|
r.provider = 'shell'
r.description = 'shell command history'
- r.identifier = 'shell-history'
+ r.identifier = 'history'
end
- setting :filename, default: ENV['HISTFILE']
+ setting :input
setting :shell, default: 'bash'
BASH_TIMESTAMP_REGEX = /^\#(?<timestamp>[0-9]{10})/
def prepare
@@ -35,11 +35,12 @@
private
# TODO: modularize the shell-specific stuff
def history_filename
- @config.filename || __send__("history_filename_default_#{@config.shell}")
+ # Ideally we coudl just use ENV['HISTFILE] but it's not available outside of Bash
+ @config.input&.first || __send__("history_filename_default_#{@config.shell}")
end
def history_filename_default_bash
File.join(Dir.home, ".bash_history")
end
@@ -78,16 +79,18 @@
commands
end
def load_commands_from_bash
- timestamp = nil
- File.foreach(history_filename) do |line|
- if match = line.scrub.match(BASH_TIMESTAMP_REGEX)
- timestamp = Time.at(match[:timestamp].to_i)
- elsif timestamp
- command = { timestamp: timestamp, command: line.scrub.strip }
+ command = nil
+ File.readlines(history_filename).reverse_each do |line|
+ timestamp_line = line.scrub.match(BASH_TIMESTAMP_REGEX)
+ if timestamp_line && command
+ timestamp = Time.at(timestamp_line[:timestamp].to_i)
+ command = { timestamp: timestamp, command: command }
yield command
+ else
+ command = line.scrub.strip
end
end
end
def load_commands_from_zsh