aboutsummaryrefslogtreecommitdiffstats
path: root/bin/secret
blob: 27391ff095c66e4e9bfbdfcb92015e22e52739fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
#
# Open and write to a gpg encrypted file
# Will create the file for you if it's not there yet
#
# secret <path/to/file>

writeback () {
    gpg --yes -eq -r "$gpgkey" -o $1 $tmpfile
    echo "Encrypted file using gpg public key: $gpgkey"
    chmod 600 $1
    echo "Finished working on file: $1."
    rm $tmpfile
    echo "Removed temp file $tmpfile"
}

filetotmp () {
  gpg --yes -o $tmpfile -d $1
}

tmpfile=$(mktemp)
gpgkey="David Runge <david.runge@frqrec.com>"

# if file is available, use it, else create it
if [[ -f "$1" ]];then
  filetotmp $1
  echo "File $1 now in $tmpfile ."
fi

echo "Opening file in $EDITOR"
# edit decrypted file in /tmp
$EDITOR $tmpfile
if  [[ $? -gt 0 ]];then
  echo "Something went wrong with $EDITOR ."
  echo "Aborting."
  rm $tmpfile
  echo "Deleted $tmpfile ."
else
  # write back the file
  writeback $1
fi