Class: PGP::CLI

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

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.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

Instance Attribute Details

#opt_parserObject

Returns the value of attribute opt_parser.



8
9
10
# File 'lib/pgp/cli.rb', line 8

def opt_parser
  @opt_parser
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/pgp/cli.rb', line 8

def options
  @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)
  options[arg]
end

#[]=(arg, val) ⇒ Object



37
38
39
# File 'lib/pgp/cli.rb', line 37

def []=(arg, val)
  options[arg] = val
end

#actionObject



95
96
97
98
99
100
101
102
103
# 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!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_filesObject



113
114
115
# File 'lib/pgp/cli.rb', line 113

def input_files
  options[:input_files]
end

#outdirObject



121
122
123
# File 'lib/pgp/cli.rb', line 121

def outdir
  options[:outdir]
end

#output_filesObject



117
118
119
# File 'lib/pgp/cli.rb', line 117

def output_files
  options[:output_files]
end

#private_keysObject



109
110
111
# File 'lib/pgp/cli.rb', line 109

def private_keys
  options[:private_keys]
end

#public_keysObject



105
106
107
# File 'lib/pgp/cli.rb', line 105

def public_keys
  options[:public_keys]
end

#run!Object



56
57
58
59
60
61
62
63
# 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?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/pgp/cli.rb', line 125

def same_dir?
  options[: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 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