class Dotgpg

Public Class Methods

editor() click to toggle source

This method copied directly from Pry and is Copyright © 2013 John Mair (banisterfiend) github.com/pry/pry/blob/master/LICENSE

# File lib/dotgpg.rb, line 21
def self.editor
  configured = ENV["VISUAL"] || ENV["EDITOR"] || guess_editor
  case configured
  when /^mate/, /^subl/
    configured << " -w"
  when /^[gm]vim/
    configured << " --nofork"
  when /^jedit/
    configured << " -wait"
  end

  configured
end
guess_editor() click to toggle source
# File lib/dotgpg.rb, line 35
def self.guess_editor
  %w(subl sublime-text sensible-editor editor mate nano vim vi open).detect do |editor|
    system("which #{editor} > /dev/null 2>&1")
  end
end
interactive=(bool) click to toggle source
# File lib/dotgpg.rb, line 55
def self.interactive=(bool)
  @interactive = bool
  if interactive?
    # get rid of stack trace on <ctrl-c>
    trap(:INT){ exit 2 }
  else
    trap(:INT, "DEFAULT")
  end
end
interactive?() click to toggle source
# File lib/dotgpg.rb, line 65
def self.interactive?
  !!@interactive
end
passfunc(hook, uid_hint, passphrase_info, prev_was_bad, fd) click to toggle source
# File lib/dotgpg.rb, line 84
def self.passfunc(hook, uid_hint, passphrase_info, prev_was_bad, fd)
  if interactive? && (!@passphrase || prev_was_bad != 0)
    uid_hint = $1 if uid_hint =~ /<(.*)>/
    @passphrase = read_passphrase "GPG passphrase for #{uid_hint}: "
  elsif !@passphrase
    raise "You must set Dotgpg.password or Dotgpg.interactive"
  end

  io = IO.for_fd(fd, 'w')
  io.puts(@passphrase)
  io.flush
end
passphrase=(passphrase) click to toggle source

TODO: it’d be nice not to store the passphrase in plaintext in RAM.

# File lib/dotgpg.rb, line 71
def self.passphrase=(passphrase)
  @passphrase = passphrase
end
read_input(prompt) click to toggle source
# File lib/dotgpg.rb, line 41
def self.read_input(prompt)
  $stderr.print prompt
  $stderr.flush
  $stdin.readline.strip
end
read_passphrase(prompt) click to toggle source
# File lib/dotgpg.rb, line 47
def self.read_passphrase(prompt)
  `stty -echo`
  read_input prompt
ensure
  $stderr.print "\n"
  `stty echo`
end
warn(context, error) click to toggle source
# File lib/dotgpg.rb, line 75
def self.warn(context, error)
  if interactive?
    $stderr.puts "#{context}: #{error.message}"
  else
    puts "raising warning"
    raise error
  end
end