summaryrefslogtreecommitdiff
path: root/angou
blob: 179775df58a59192f4c3f12bc269574892963e92 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh -e

# 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 -i"}
# sets command to use when listing directory structure
ANGOU_LIST=${ANGOU_LIST:-"tree"}
# custom command for generating passwords/users
ANGOU_GEN_PASS=${ANGOU_GEN_PASS:-"random pass"}
ANGOU_GEN_USER=${ANGOU_GEN_USER:-"random user"}

mkdir -p "$ANGOU_DIR"
cd "$ANGOU_DIR"

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

# view the first line
view_single() {
	[ ! -f "$1" ] && usage && exit 1

	view "$1" | sed 1q | tr -d \\n
}

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

	view_single "$1" | $ANGOU_CLIPBOARD
}

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

	tmpfile="$(mktemp)"

	if [ "$2" = "generate" ]; then
		$ANGOU_GEN_PASS >"$tmpfile"
		echo "Username: $($ANGOU_GEN_USER)" >>"$tmpfile"
	fi

	# edit a copy of a password if it already exists
	[ -f "$1" ] && $ANGOU_DECRYPT "$1" > "$tmpfile"

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

	mkdir -p "$(dirname "$1")"
	$ANGOU_ENCRYPT "$tmpfile" >"$1"
}

totp_view() {
	[ -d "$1" ] && usage && exit 1

	secret=$(view "$1" | grep TOTP | cut -d' ' -f 2)
	oathtool -b --totp "$secret"
}

totp() {
	totp_view "$1" | tr -d '\n' | $ANGOU_CLIPBOARD
}

# print usage
usage() {
	printf "Usage: %s [help|copy|edit|generate|view|view_single [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" ;;
	generate) edit "$2" generate ;;
	view) view "$2" ;;
	pass) view_single "$2" ;;
	totp_view) totp_view "$2" ;;
	totp) totp "$2" ;;
	*) view "$1" ;;
esac