xin/bins/rage.nix

93 lines
2.2 KiB
Nix
Raw Normal View History

2022-08-25 12:21:35 -06:00
{ pkgs }:
2022-08-28 16:17:50 -06:00
let
oathPkg = pkgs.oath-toolkit or pkgs.oathToolkit;
2022-09-04 15:29:29 -06:00
clip = if pkgs.system == "aarch64-darwin" then
2022-08-28 16:17:50 -06:00
"pbcopy"
else
"${pkgs.xclip}/bin/xclip";
2022-08-25 12:21:35 -06:00
in ''
2022-09-23 09:35:21 -06:00
#!${pkgs.yash}/bin/yash
2022-08-25 12:21:35 -06:00
set -e
rage_dir=~/.rage
. ''${rage_dir}/config
cmd=$1
list() {
find $rage_dir -type f -name \*.age
}
if [ -z $cmd ]; then
list
exit
fi
case $cmd in
ls)
list
;;
re)
F=""
if [ -f $2 ]; then
F=$2
else
F=$(list | grep $2)
fi
echo "Re-encrypting: '$F'"
pass="$(${pkgs.age}/bin/age -i $identity -d "$F")"
echo "$pass" | ${pkgs.age}/bin/age -a -R "$recipients" > "$F"
;;
en)
printf 'Password: '
stty -echo
read pass
stty echo
echo ""
printf 'Location: '
read loc
echo ""
mkdir -p "$(dirname ~/.rage/$loc)"
echo "$pass" | ${pkgs.age}/bin/age -a -R "$recipients" > ~/.rage/''${loc}.age
;;
de)
if [ -f $2 ]; then
${pkgs.age}/bin/age -i $identity -d $2
else
F=$(list | grep $2)
${pkgs.age}/bin/age -i $identity -d "$F"
fi
;;
cp)
if [ -f $2 ]; then
2022-08-28 16:17:50 -06:00
${pkgs.age}/bin/age -i $identity -d $2 | ${clip}
2022-08-25 12:21:35 -06:00
else
F=$(list | grep $2)
2022-08-28 16:17:50 -06:00
${pkgs.age}/bin/age -i $identity -d "$F" | ${clip}
2022-08-25 12:21:35 -06:00
fi
;;
otp)
if [ -f $2 ]; then
${pkgs.age}/bin/age -i $identity -d $2 | ${oathPkg}/bin/oathtool -b --totp -
else
F=$(list | grep $2)
${pkgs.age}/bin/age -i $identity -d "$F" | ${oathPkg}/bin/oathtool -b --totp -
fi
;;
push)
cd $rage_dir
2022-08-28 16:17:50 -06:00
${pkgs.git}/bin/git push
2022-08-25 12:21:35 -06:00
;;
sync)
cd $rage_dir
2022-08-28 16:17:50 -06:00
${pkgs.git-sync}/bin/git-sync
2022-08-25 12:21:35 -06:00
;;
default)
list
esac
''