aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRasmus Steinke <rasi@xssn.at>2015-09-13 15:37:31 +0200
committerRasmus Steinke <rasi@xssn.at>2015-09-13 15:37:31 +0200
commitc36257f2cee88615751b24bf147a8e208a7cd0d2 (patch)
treed31dbd8b81c6e1aeb6e06aaa65b92e375aa29333
parentbcafceef757f473a8fe088d970bb21549d9df63c (diff)
downloadrofi-pass-c36257f2cee88615751b24bf147a8e208a7cd0d2.tar.gz
mirror passed script
-rw-r--r--passed48
1 files changed, 48 insertions, 0 deletions
diff --git a/passed b/passed
new file mode 100644
index 0000000..ea9e121
--- /dev/null
+++ b/passed
@@ -0,0 +1,48 @@
+#!/usr/bin/env ruby
+
+abort("usage: #{$PROGRAM_NAME} <sed options>") if ARGV.empty?
+
+sed_command = ['sed', *ARGV]
+PASS_DIR = ENV['PASSWORD_STORE_DIR'] || "#{ENV['HOME']}/.password-store"
+
+def keys(dir)
+ key_file = "#{dir}/.gpg-id"
+ return unless File.exist?(key_file)
+ File.read(key_file).lines.map(&:chomp)
+end
+
+def each_entry_with_key(dir = PASS_DIR, keys = nil, &block)
+ keys = keys(dir) || keys
+ fail('no encryption keys found') unless keys
+ Dir[File.join(dir, '*.gpg')].each do |entry|
+ yield(entry, keys)
+ end
+
+ Dir[File.join(dir, '*/')].each do |subdir|
+ each_entry_with_key(subdir, keys, &block)
+ end
+end
+
+each_entry_with_key do |entry, keys|
+ new_content = nil
+ IO.popen(['gpg', '--batch', '-q', '-d', entry]) do |gpg|
+ IO.popen(sed_command, 'w+') do |sed|
+ sed.write gpg.read
+ sed.close_write
+ new_content = sed.read
+ end
+ end
+
+ puts entry
+ puts new_content
+ puts 'overwrite? (y/N)'
+ answer = STDIN.gets
+ next unless answer && answer.chomp =~ /^y/i
+
+ recipients = keys.map { |key| ['-r', key] }.flatten
+ File.delete(entry)
+ encrypt_cmd = ['gpg', '--batch', '-q', '-e', '-o', entry, *recipients]
+ IO.popen(encrypt_cmd, 'w+') do |gpg|
+ gpg.write(new_content)
+ end
+end