Class: PGP::CLI
- Inherits:
-
Object
- Object
- PGP::CLI
- Defined in:
- lib/pgp/cli.rb,
lib/pgp/cli/runner.rb
Overview
Currently, this is pretty quick-and-dirty. I should expand options into accessor methods, I know.
Defined Under Namespace
Modules: Runner
Constant Summary collapse
- Encrypted_Extension_Regexp =
/\.(pgp|gpg|asc)$/i
Instance Attribute Summary collapse
-
#opt_parser ⇒ Object
Returns the value of attribute opt_parser.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #[]=(arg, val) ⇒ Object
- #action ⇒ Object
- #decrypt! ⇒ Object
- #encrypt! ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #input_files ⇒ Object
- #outdir ⇒ Object
- #output_files ⇒ Object
- #private_keys ⇒ Object
- #public_keys ⇒ Object
- #run! ⇒ Object
- #same_dir? ⇒ Boolean
- #validate_options! ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pgp/cli.rb', line 20 def initialize self. = { :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 |
Instance Attribute Details
#opt_parser ⇒ Object
Returns the value of attribute opt_parser.
8 9 10 |
# File 'lib/pgp/cli.rb', line 8 def opt_parser @opt_parser end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/pgp/cli.rb', line 8 def @options end |
Class Method Details
.ensure_dir_exists!(dir) ⇒ Object
16 17 18 |
# 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) ⇒ Object
12 13 14 |
# 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 |
Instance Method Details
#[](arg) ⇒ Object
33 34 35 |
# File 'lib/pgp/cli.rb', line 33 def [](arg) [arg] end |
#[]=(arg, val) ⇒ Object
37 38 39 |
# File 'lib/pgp/cli.rb', line 37 def []=(arg, val) [arg] = val end |
#action ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/pgp/cli.rb', line 95 def action [:action] ||= begin if input_files.grep(Encrypted_Extension_Regexp).any? :decrypt else :encrypt end end end |
#decrypt! ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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! ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
113 114 115 |
# File 'lib/pgp/cli.rb', line 113 def input_files [:input_files] end |
#outdir ⇒ Object
121 122 123 |
# File 'lib/pgp/cli.rb', line 121 def outdir [:outdir] end |
#output_files ⇒ Object
117 118 119 |
# File 'lib/pgp/cli.rb', line 117 def output_files [:output_files] end |
#private_keys ⇒ Object
109 110 111 |
# File 'lib/pgp/cli.rb', line 109 def private_keys [:private_keys] end |
#public_keys ⇒ Object
105 106 107 |
# File 'lib/pgp/cli.rb', line 105 def public_keys [:public_keys] end |
#run! ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/pgp/cli.rb', line 56 def run! case [:action] when :encrypt then encrypt! when :decrypt then decrypt! end end |
#same_dir? ⇒ Boolean
125 126 127 |
# File 'lib/pgp/cli.rb', line 125 def same_dir? [:same_dir] end |
#validate_options! ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pgp/cli.rb', line 41 def raise "Input file(s) must be specified!" if input_files.none? case action when :encrypt then when :decrypt then else raise "Valid actions are encrypt or decrypt. Action specified: #{[:action]}" end rescue RuntimeError => e $stderr.puts opt_parser raise e end |