Class: EJSON::CLI

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

Instance Method Summary collapse

Instance Method Details

#decrypt(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ejson/cli.rb', line 16

def decrypt(file)
  ciphertext = File.read(file)
  ej = EJSON.new(pubkey, options[:privkey])
  output = JSON.pretty_generate(ej.load(ciphertext).decrypt_all)
  if options[:inplace]
    File.open(file, "w") { |f| f.puts output }
    puts "Wrote #{output.size} bytes to #{file}"
  else
    puts output
  end
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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ejson/cli.rb', line 33

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



50
51
52
53
# File 'lib/ejson/cli.rb', line 50

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