summaryrefslogtreecommitdiff
path: root/angou
blob: b7427ba1b2ff941e43a693a6ab1373c365c9c9e3 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh

# sets the directory with all your passwords
ANGOU_DIR=${ANGOU_DIR:-"$HOME/.angou/"}
# sets the command to copy into your clipboard (must accept stdin)
ANGOU_CLIPBOARD=${ANGOU_CLIPBOARD:-"xsel -ib"}
# sets command to use when listing directory structure
ANGOU_LIST=${ANGOU_LIST:-"tree"}

mkdir -p "$ANGOU_DIR"
cd "$ANGOU_DIR" || exit

[ ! -f ".gpg-id" ] && echo "please put your gpg key id into \$ANGOU_DIR/.gpg-id"
GPG_ID="$(cat .gpg-id)"

# decrypt and print a password from ANGOU_DIR or print the directory structure
view() {
	if [ -f "${1%%.gpg}" ]; then
		gpg -qd "${1%%.gpg}".gpg
	elif [ -d "${1:-.}" ]; then
		tree "${1:-.}"
	else
		usage
		exit
	fi
}

# copy the first line from view to clipboard
copy() {
	[ ! -f "$1" ] && usage && exit

	view "$1" | sed 1q | $ANGOU_CLIPBOARD
}

# create new or existing password in ANGOU_DIR
edit() {
	[ -d "$1" ] && usage && exit

	tmpfile="$(mktemp)"

	# edit a copy of a password if it already exists
	[ -f "${1%%.gpg}".gpg ] && gpg -qd "${1%%.gpg}".gpg > "$tmpfile"

	"${VISUAL:-${EDITOR:-ed}}" "$tmpfile"

	mkdir -p "$(dirname "$1")"
	gpg -er "$GPG_ID" "$tmpfile"

	rm "$tmpfile"
	mv "$tmpfile.gpg" "${1%%.gpg}".gpg
}

# print usage
usage() {
	printf "Usage: %s [help|copy|edit|view [file or directory]]\n" "$0"
	printf "\tThe edit and copy command expects a file as an argument,\n"
	printf "\twhile the view command expects a directory as an argument.\n"
	printf "\tThe arguments for view and edit are relative to\n"
	printf "\t\$ANGOU_DIR which is currently set to %s\n\n" "$ANGOU_DIR"

	printf "\tThe view command is the default command if only a file or\n"
	printf "\tdirectory is passed as an argument\n"
}

option="$1"
case "$option" in
	help) usage ;;
	copy) copy "$2" ;;
	edit) edit "$2" ;;
	view) view "$2" ;;
	*) view "$1" ;;
esac