Skip to content

Instantly share code, notes, and snippets.

@IvanShamatov
Last active February 21, 2020 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanShamatov/ac8c5252b55da5ae1193e8d87356aefb to your computer and use it in GitHub Desktop.
Save IvanShamatov/ac8c5252b55da5ae1193e8d87356aefb to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
require 'resolv'
require 'uri'
gemfile do
gem 'dry-cli', branch: 'master', git: 'https://github.com/dry-rb/dry-cli', require: 'dry/cli/inline'
end
def history_files
%w[.bash_history .zsh_history].map do |file|
history_file = File.join(ENV['HOME'], file)
File.read(history_file, encoding: 'iso-8859-1') if File.exist?(history_file)
end
end
option :skip, aliases: %w[s],desc: 'Skip matching commands', type: :array
option :expression, aliases: %w[e], desc: 'Pass your expression to filter any match'
run do |**options|
history_files.each do |file|
file.each_line do |line|
next if options[:skip]&.map { |cmd| line.include?(cmd) }&.any?
line = line.gsub(/(\S+)=\S+/, '\1=[FILTERED_VALUE]')
line = line.gsub(Resolv::IPv4::Regex, '[FILTERED_IP]')
line = line.gsub(Resolv::IPv6::Regex, '[FILTERED_IP]')
line = line.gsub(URI::DEFAULT_PARSER.make_regexp, '[FILTERED_URI]')
line = line.gsub(URI::MailTo::EMAIL_REGEXP, '[FILTERED_EMAIL]')
begin
line = line.gsub(/#{options[:expression]}/, '[FILTERED_DATA]') if options[:expression]
rescue RegexpError => e
puts "#{e.class}: #{e.message}"
exit(1)
end
puts line
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment