class Dotgpg::Cli
Public Instance Methods
add(file=nil)
click to toggle source
# File lib/dotgpg/cli.rb, line 42 def add(file=nil) return if helped? dir = Dotgpg::Dir.closest fail "not in a dotgpg directory" unless dir key = read_key_file_for_add(file) fail "#{file || "<stdin>"}: not a valid GPG key" unless key if dir.has_key?(key) && !options[:force] fail "#{dir.key_path(key)}: already exists" end info "Adding #{key.name} to #{dir.path}" info " #{dir.key_path(key).relative_path_from(dir.path)}" dir.add_key(key) rescue GPGME::Error::BadPassphrase => e fail e.message end
cat(*files)
click to toggle source
# File lib/dotgpg/cli.rb, line 88 def cat(*files) return if helped? dir = Dotgpg::Dir.closest(*files) fail "not in a dotgpg directory" unless dir files.each do |f| dir.decrypt f, $stdout end rescue GPGME::Error::BadPassphrase => e fail e.message end
edit(*files)
click to toggle source
# File lib/dotgpg/cli.rb, line 102 def edit(*files) return if helped? dir = Dotgpg::Dir.closest(*files) fail "not in a dotgpg directory" unless dir dir.reencrypt files do |tempfiles| if tempfiles.any? to_edit = tempfiles.values.map do |temp| Shellwords.escape(temp.path) end system "#{Dotgpg.editor} #{to_edit.join(" ")}" fail "Problem with editor. Not saving changes" unless $?.success? end end rescue GPGME::Error::BadPassphrase => e fail e.message end
init(directory=".")
click to toggle source
# File lib/dotgpg/cli.rb, line 10 def init(directory=".") return if helped? dir = Dotgpg::Dir.new directory if dir.dotgpg.exist? fail "#{directory}/.gpg already exists" end key = Dotgpg::Key.secret_key(options[:email], options[:"new-key"]) info "Initializing new dotgpg directory" info " #{directory}/README.md" info " #{directory}/.gpg/#{key.email}" FileUtils.mkdir_p(dir.dotgpg) FileUtils.cp Pathname.new(__FILE__).dirname.join("template/README.md"), dir.path.join("README.md") dir.add_key(key) end
key()
click to toggle source
# File lib/dotgpg/cli.rb, line 33 def key return if helped? key = Dotgpg::Key.secret_key(options[:email], options[:"new-key"]) $stdout.print key.export(armor: true).to_s end
rm(file=nil)
click to toggle source
# File lib/dotgpg/cli.rb, line 65 def rm(file=nil) return if helped?(file.nil?) dir = Dotgpg::Dir.closest fail "not in a dotgpg directory" unless dir key = read_key_file_for_rm(file) fail "#{file}: not a valid GPG key" if !key && !options[:force] if key if GPGME::Key.find(:secret).include?(key) && !options[:force] fail "#{file}: refusing to remove your own secret key" end info "Removing #{key.name} from #{dir.path}" info "D #{dir.key_path(key).relative_path_from(dir.path)}" dir.remove_key(key) end rescue GPGME::Error::BadPassphrase => e fail e.message end
Private Instance Methods
fail(msg)
click to toggle source
Fail with a message.
In interactive mode, exits the program with status 1. Otherwise raises a Dotgpg::Failure
.
@param [String] msg
# File lib/dotgpg/cli.rb, line 153 def fail(msg) if Dotgpg.interactive? $stderr.puts msg exit 1 else raise Dotgpg::Failure, msg, caller[1] end end
helped?(force=false)
click to toggle source
If the global –help or -h flag is passed, show help.
Should be invoked at the start of every command.
@param [Boolean] force force showing help @return [Boolean] help was shown
# File lib/dotgpg/cli.rb, line 131 def helped?(force=false) if options[:help] || force invoke :help, @_invocations[self.class] true end end
info(msg)
click to toggle source
Print an informational message in interactive mode.
@param [String] msg The message to show
# File lib/dotgpg/cli.rb, line 141 def info(msg) if Dotgpg.interactive? $stdout.puts msg end end
read_key_file_for_add(file)
click to toggle source
Read a key from a given file or stdin.
@param [nil, String] the file the user specified. @return [nil, GPGME::Key]
# File lib/dotgpg/cli.rb, line 166 def read_key_file_for_add(file) if file.nil? if $stdin.tty? info "Paste a public key, then hit <ctrl+d> twice." key = Dotgpg::Key.read($stdin) else key = Dotgpg::Key.read($stdin) $stdin.reopen "/dev/tty" end elsif File.readable?(file) key = Dotgpg::Key.read(File.read(file)) end end
read_key_file_for_rm(file)
click to toggle source
Read a key from a given file or from the .gpg directory
@param [String] the file the user specified @return [nil, GPGME::Key]
# File lib/dotgpg/cli.rb, line 184 def read_key_file_for_rm(file) if !File.exist?(file) && File.exist?(".gpg/" + file) file = ".gpg/" + file end if File.readable?(file) Dotgpg::Key.read(File.read(file)) end end