summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShokara <shokara@snopyta.org>2021-06-12 12:34:11 -0400
committerShokara <shokara@snopyta.org>2021-06-12 12:34:11 -0400
commit44278549ad2cb0c39cfec0f8316555d1e1fe3927 (patch)
treec58802785aa48643248e3c22bda5a3489986e4e9
parent8d3981e8ec02b1d428d195360071027491b13d52 (diff)
merge view and list commands into one like pass(1) does
Also added some checks for arguments into view, copy, and edit commands, and usage is printed if the checks fail.
-rwxr-xr-xangou41
1 files changed, 24 insertions, 17 deletions
diff --git a/angou b/angou
index 9f28111..f5e1f2a 100755
--- a/angou
+++ b/angou
@@ -4,31 +4,39 @@
4ANGOU_DIR=${ANGOU_DIR:-"$HOME/.angou/"} 4ANGOU_DIR=${ANGOU_DIR:-"$HOME/.angou/"}
5# sets the command to copy into your clipboard (must accept stdin) 5# sets the command to copy into your clipboard (must accept stdin)
6ANGOU_CLIPBOARD=${ANGOU_CLIPBOARD:-"xsel -ib"} 6ANGOU_CLIPBOARD=${ANGOU_CLIPBOARD:-"xsel -ib"}
7# sets command to use when listing directory structure
8ANGOU_LIST=${ANGOU_LIST:-"tree"}
7 9
8mkdir -p "$ANGOU_DIR" 10mkdir -p "$ANGOU_DIR"
9cd "$ANGOU_DIR" || exit 11cd "$ANGOU_DIR" || exit
10 12
11# print the directory structure of ANGOU_DIR 13# decrypt and print a password from ANGOU_DIR or print the directory structure
12list() {
13 tree "$@"
14}
15
16# decrypt and print a password from ANGOU_DIR
17view() { 14view() {
18 gpg -qd "${1%%.gpg}".gpg 15 if [ -f "${1%%.gpg}" ]; then
16 gpg -qd "${1%%.gpg}".gpg
17 elif [ -d "${1:-.}" ]; then
18 tree "${1:-.}"
19 else
20 usage
21 exit
22 fi
19} 23}
20 24
21# copy the first line from view to clipboard 25# copy the first line from view to clipboard
22copy() { 26copy() {
27 [ ! -f "$1" ] && usage && exit
28
23 view "$1" | sed 1q | $ANGOU_CLIPBOARD 29 view "$1" | sed 1q | $ANGOU_CLIPBOARD
24} 30}
25 31
26# create new or existing password in ANGOU_DIR 32# create new or existing password in ANGOU_DIR
27edit() { 33edit() {
34 [ -d "$1" ] && usage && exit
35
28 tmpfile="$(mktemp)" 36 tmpfile="$(mktemp)"
29 37
30 # edit a copy of a password if it already exists 38 # edit a copy of a password if it already exists
31 test [ -f "${1**.gpg}".gpg] && cp "$1" "$tmpfile" 39 [ -f "${1%%.gpg}".gpg ] && cp "$1" "$tmpfile"
32 40
33 "${VISUAL:-${EDITOR:-ed}}" "$tmpfile" 41 "${VISUAL:-${EDITOR:-ed}}" "$tmpfile"
34 42
@@ -38,22 +46,21 @@ edit() {
38 46
39# print usage 47# print usage
40usage() { 48usage() {
41 printf "Usage: %s [help|view|copy|edit|list [file or directory]]\n" "$1" 49 printf "Usage: %s [help|copy|edit|view [file or directory]]\n" "$0"
42 printf "\tThe view and edit commands expect a file as an argument,\n" 50 printf "\tThe edit and copy command expects a file as an argument,\n"
43 printf "\twhile the list commands expect a directory as an argument.\n" 51 printf "\twhile the view command expects a directory as an argument.\n"
44 printf "\tThe arguments for view, edit, and list are relative to\n" 52 printf "\tThe arguments for view and edit are relative to\n"
45 printf "\t\$ANGOU_DIR which is currently set to %s\n\n" "$ANGOU_DIR" 53 printf "\t\$ANGOU_DIR which is currently set to %s\n\n" "$ANGOU_DIR"
46 54
47 printf "\tThe list command is the default option if only a file or\n" 55 printf "\tThe view command is the default command if only a file or\n"
48 printf "\tdirectory is passed as an argument\n" 56 printf "\tdirectory is passed as an argument\n"
49} 57}
50 58
51option="$1" 59option="$1"
52case "$option" in 60case "$option" in
53 help) usage "$0" ;; 61 help) usage ;;
54 view) view "$2" ;;
55 copy) copy "$2" ;; 62 copy) copy "$2" ;;
56 edit) edit "$2" ;; 63 edit) edit "$2" ;;
57 list) shift && list "$@" ;; # fallthrough isn't in posix sh? 64 view) view "$2" ;;
58 *) list "$@" ;; 65 *) view "$1" ;;
59esac 66esac