class PGP::CLI
Currently, this is pretty quick-and-dirty. I should expand options into accessor methods, I know.
Currently, this is pretty quick-and-dirty. I should expand options into accessor methods, I know.
Constants
- Encrypted_Extension_Regexp
Attributes
opt_parser[RW]
options[RW]
Public Class Methods
ensure_dir_exists!(dir)
click to toggle source
# File lib/pgp/cli.rb, line 16 def self.ensure_dir_exists!(dir) raise "The directory #{dir.inspect} does not appear to exist!" unless File.directory?(dir) end
ensure_file_exists!(file)
click to toggle source
# File lib/pgp/cli.rb, line 12 def self.ensure_file_exists!(file) raise "The file #{file.inspect} does not appear to exist!" unless File.exist?(file) end
new()
click to toggle source
# File lib/pgp/cli.rb, line 20 def initialize self.options = { :public_keys => [], :private_keys => [], :input_files => [], :output_files => [], :outdir => Pathname(Dir.pwd), :same_dir => false, :action => nil, :signature => false, # We do not currently support signing or verifying signatures } end
Public Instance Methods
[](arg)
click to toggle source
# File lib/pgp/cli.rb, line 33 def [](arg) options[arg] end
[]=(arg, val)
click to toggle source
# File lib/pgp/cli.rb, line 37 def []=(arg, val) options[arg] = val end
action()
click to toggle source
# File lib/pgp/cli.rb, line 95 def action options[:action] ||= begin if input_files.grep(Encrypted_Extension_Regexp).any? :decrypt else :encrypt end end end
decrypt!()
click to toggle source
# File lib/pgp/cli.rb, line 80 def decrypt! cryptor = Decryptor.new private_keys.each {|priv| cryptor.add_keys_from_file(priv) } input_files.each_with_index do |infile, idx| outfile = output_files[idx] output = cryptor.decrypt_file(infile) File.open(outfile, "w") do |fi| fi.write output end end end
encrypt!()
click to toggle source
# File lib/pgp/cli.rb, line 65 def encrypt! cryptor = Encryptor.new public_keys.each {|pub| cryptor.add_keys_from_file(pub) } input_files.each_with_index do |infile, idx| outfile = output_files[idx] output = cryptor.encrypt_file(infile) File.open(outfile, "w") do |fi| fi.write output end end end
input_files()
click to toggle source
# File lib/pgp/cli.rb, line 113 def input_files options[:input_files] end
outdir()
click to toggle source
# File lib/pgp/cli.rb, line 121 def outdir options[:outdir] end
output_files()
click to toggle source
# File lib/pgp/cli.rb, line 117 def output_files options[:output_files] end
private_keys()
click to toggle source
# File lib/pgp/cli.rb, line 109 def private_keys options[:private_keys] end
public_keys()
click to toggle source
# File lib/pgp/cli.rb, line 105 def public_keys options[:public_keys] end
run!()
click to toggle source
# File lib/pgp/cli.rb, line 56 def run! validate_options! case options[:action] when :encrypt then encrypt! when :decrypt then decrypt! end end
same_dir?()
click to toggle source
# File lib/pgp/cli.rb, line 125 def same_dir? options[:same_dir] end
validate_options!()
click to toggle source
# File lib/pgp/cli.rb, line 41 def validate_options! raise "Input file(s) must be specified!" if input_files.none? case action when :encrypt then validate_encrypt_options! when :decrypt then validate_decrypt_options! else raise "Valid actions are encrypt or decrypt. Action specified: #{options[:action]}" end rescue RuntimeError => e $stderr.puts opt_parser raise e end
Protected Instance Methods
set_outfile_dir(file)
click to toggle source
# File lib/pgp/cli.rb, line 130 def set_outfile_dir(file) return file if same_dir? outdir + File.basename(file) end
validate_decrypt_options!()
click to toggle source
# File lib/pgp/cli.rb, line 145 def validate_decrypt_options! raise "Private Keys are required for decryption" if options[:private_keys].none? raise "Public Keys are required for verifying signatures" if options[:signature] and options[:public_keys].none? options[:input_files].each_with_index do |infile, idx| next if options[:output_files][idx] outfile = infile.gsub(Encrypted_Extension_Regexp, '') outfile = "#{infile} - decrypted" if outfile == infile options[:output_files][idx] = set_outfile_dir(outfile) end end
validate_encrypt_options!()
click to toggle source
# File lib/pgp/cli.rb, line 135 def validate_encrypt_options! raise "Public Keys are required for encryption" if options[:public_keys].none? raise "Private Keys are required for signing files" if options[:signature] and options[:private_keys].none? options[:input_files].each_with_index do |infile, idx| next if options[:output_files][idx] options[:output_files][idx] = set_outfile_dir("#{infile}.gpg") end end