2012-05-17 21:00:33 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2012-09-21 17:17:38 +00:00
|
|
|
require 'persistent-shell-history/binary-history-store'
|
2012-08-24 01:18:15 +00:00
|
|
|
require 'persistent-shell-history/old-history-store'
|
2012-08-24 00:17:36 +00:00
|
|
|
require 'optparse'
|
2012-05-17 21:00:33 +00:00
|
|
|
|
2012-08-24 00:17:36 +00:00
|
|
|
options = {}
|
2012-09-21 17:17:38 +00:00
|
|
|
bh_options = {:archive_file => File.expand_path("~/.bash_history.db")}
|
|
|
|
opts = OptionParser.new do |opts|
|
2012-08-24 00:17:36 +00:00
|
|
|
opts.on('--inspect','inspect the data') do |o|
|
|
|
|
options[:inspect] = o
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
2012-08-24 00:17:36 +00:00
|
|
|
opts.on('-h','--history FILE','use bash_history FILE instead of the default (~/.bash_history)') do |o|
|
|
|
|
bh_options[:file] = o
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
2012-08-24 00:17:36 +00:00
|
|
|
opts.on('-d','--db FILE','use database FILE instead of the default (~/.bash_history.db)') do |o|
|
|
|
|
bh_options[:archive_file] = o
|
2012-06-01 04:22:57 +00:00
|
|
|
end
|
2012-09-21 17:17:38 +00:00
|
|
|
opts.on('--migrate','check-for and migrate, only') do |o|
|
|
|
|
options[:migrate] = o
|
|
|
|
end
|
2012-08-24 00:17:36 +00:00
|
|
|
opts.on('-l','--list','list history') do |o|
|
|
|
|
options[:list] = o
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
2012-08-24 00:17:36 +00:00
|
|
|
opts.on('--format FORMAT','specify a different strftime format. (default is "%F %T")') do |o|
|
|
|
|
bh_options[:time_format] = o
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
2012-08-24 00:17:36 +00:00
|
|
|
opts.on('-f','--find PAT','find a command with pattern PAT') do |o|
|
|
|
|
options[:find] = o
|
|
|
|
end
|
2012-09-21 17:17:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
opts.parse!(ARGV)
|
|
|
|
rescue => ex
|
|
|
|
puts ex
|
|
|
|
puts opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate(old_bashhistorystore)
|
|
|
|
require 'fileutils'
|
|
|
|
# migrate
|
|
|
|
temp_db = GDBM.new("#{old_bashhistorystore.archive}.#{$$}")
|
|
|
|
old_bashhistorystore.keys.each {|key|
|
|
|
|
h = old_bashhistorystore[key]
|
|
|
|
h[:time] = h[:time].map {|t| t.to_i }
|
|
|
|
temp_db[key] = Marshal.dump(h)
|
|
|
|
}
|
|
|
|
old_bashhistorystore.db.close()
|
|
|
|
temp_db.close()
|
|
|
|
ts = "#{Time.now.year}#{Time.now.month}#{Time.now.day}#{Time.now.hour}#{Time.now.min}"
|
|
|
|
old_filename = "#{old_bashhistorystore.archive}.old.#{ts}"
|
|
|
|
FileUtils.mv(old_bashhistorystore.archive, old_filename)
|
|
|
|
puts "archived [#{old_bashhistorystore.archive}] to [#{old_filename}] ..."
|
|
|
|
FileUtils.mv("#{old_bashhistorystore.archive}.#{$$}", old_bashhistorystore.archive)
|
|
|
|
end
|
2012-05-18 16:08:32 +00:00
|
|
|
|
2012-09-21 17:17:38 +00:00
|
|
|
# First check the database, for whether it is the old format,
|
|
|
|
# if so, convert it, and reopen it.
|
2012-08-24 01:20:08 +00:00
|
|
|
bh = Persistent::Shell::OldHistoryStore.new(bh_options)
|
2012-09-21 17:17:38 +00:00
|
|
|
migrate(bh) if bh.is_oldformat?
|
|
|
|
bh.db.close unless bh.db.closed?
|
|
|
|
|
|
|
|
exit(0) if options[:migrate]
|
|
|
|
|
|
|
|
# re-open the history storage
|
|
|
|
bh = Persistent::Shell::BinaryHistoryStore.new(bh_options)
|
|
|
|
|
|
|
|
# load the new bash_history into the database
|
2013-06-18 04:39:09 +00:00
|
|
|
unless (options[:inspect] or options[:find] or options[:list])
|
|
|
|
bh.load()
|
|
|
|
bh.db.reorganize()
|
|
|
|
end
|
2012-05-18 16:28:58 +00:00
|
|
|
|
2012-08-24 00:17:36 +00:00
|
|
|
if options[:inspect]
|
|
|
|
p bh
|
2013-06-18 04:39:09 +00:00
|
|
|
#p "storing #{bh.keys.count} commands"
|
2012-08-24 00:17:36 +00:00
|
|
|
end
|
|
|
|
if options[:find]
|
|
|
|
bh.find(options[:find]).sort_by {|x| x[:time] }.each do |val|
|
2012-09-21 17:17:38 +00:00
|
|
|
puts bh.fmt(val)
|
2012-05-18 16:08:32 +00:00
|
|
|
end
|
2012-08-24 00:17:36 +00:00
|
|
|
elsif options[:list]
|
|
|
|
bh.values_by_time.each do |val|
|
2012-09-21 17:17:38 +00:00
|
|
|
puts bh.fmt(val)
|
2012-05-18 16:08:32 +00:00
|
|
|
end
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
|
|
|
|
2012-08-24 01:09:09 +00:00
|
|
|
# vim: set sts=2 sw=2 et ai:
|