Class: EJSON::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/ejson/cli.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(file) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ejson/cli.rb', line 15

def decrypt(file)
  ciphertext = File.read(file)
  ej = EJSON.new(pubkey, options[:privkey])
  puts JSON.pretty_generate(ej.load(ciphertext).decrypt_all)
rescue EJSON::Encryption::PrivateKeyMissing => e
  fatal("can't decrypt data without private key (specify path with -k)", e)
rescue EJSON::Encryption::ExpectedEncryptedString => e
  fatal("can't decrypt data with cleartext strings (use ejson recrypt first)", e)
end

#encrypt(file = "**/*.ejson") ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ejson/cli.rb', line 26

def encrypt(file="**/*.ejson")
  ej = EJSON.new(pubkey)
  fpaths = Dir.glob(file)
  if fpaths.empty?
    fatal("no ejson files found!", nil)
  end
  fpaths.each do |fpath|
    data = ej.load(File.read(fpath))
    dump = data.dump
    File.open(fpath, "w") { |f| f.puts dump }
    puts "Wrote #{dump.size+1} bytes to #{fpath}"
  end
rescue OpenSSL::X509::CertificateError => e
  fatal("invalid certificate", e)
end

#versionObject



43
44
45
# File 'lib/ejson/cli.rb', line 43

def version
  puts "ejson version #{EJSON::VERSION}"
end