summaryrefslogtreecommitdiff
path: root/angou
blob: 756df71e46e2561a06f6f1cee859d7e1b110a6ea (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
#!/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"}

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

# print the directory structure of ANGOU_DIR
list() {
	tree "$@"
}

# decrypt and print a password from ANGOU_DIR
view() {
	gpg -qd "${1%%.gpg}".gpg
}

# copy the first line from view to clipboard
copy() {
	view "$1" | "$ANGOU_CLIPBOARD"
}

# create new or existing password in ANGOU_DIR
edit() {
	tmpfile="$(mktemp)"

	# edit a copy of a password if it already exists
	test [ -f "${1**.gpg}".gpg] && cp "$1" "$tmpfile"

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

	mkdir -p "$(dirname "$1")"
	mv "$tmpfile" "${1%%.gpg}".gpg
}

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

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

option="$1"
case "$option" in
	help) usage "$0" ;;
	view) view "$2" ;;
	copy) copy "$2" ;;
	edit) edit "$2" ;;
	list) shift && list "$@" ;;	# fallthrough isn't in posix sh?
	*) list "$@" ;;
esac