Class: Dotgpg::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/dotgpg/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(file = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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 = ".") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

#keyObject



33
34
35
36
37
38
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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