system.die

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2017-04-19 18:29:39 +03:00
parent 1e757b7168
commit 5e223b78b7
1 changed files with 12 additions and 15 deletions

27
jspass
View File

@ -1,27 +1,24 @@
#!/usr/bin/env bash-hammer
set +e
require /toolbox/system
require /system
require /args
# Alias some stuff
die() { Toolbox::System.die "$@"; }
err() { Toolbox::System.err "$@"; }
# Helpers
vault_is_json() { pass "$1" | jshon -Q &>/dev/null; }
vault_exists() [[ -f "${Opts[store]}/$1.gpg" ]]
check_argn() {
# Check for the number of args the dumb way.
(( $# < 2 )) && die 113
(( $# > 3 )) && die 113
(( $# < 2 )) && System.die 113
(( $# > 3 )) && System.die 113
declare length=$1 min=$2 max=$3
(( length < min )) && die 1 "Too few arguments"
(( length < min )) && System.die 1 "Too few arguments"
[[ "$max" ]] && {
(( length > max )) && die 1 "Too many arguments"
(( length > max )) && System.die 1 "Too many arguments"
}
return 0
@ -47,12 +44,12 @@ Args "$@" -- error argv \
-s,p,"Opts[selection]" \
-c,f,"Flags[clip]" \
-e,f,"Flags[edit]" \
|| { die 1 "$error"; }
|| { System.die 1 "$error"; }
set -- "${argv[@]}"
case "$1" in
(new|insert|show|add)
(new|show|add)
Opts[action]=$1
shift
;;
@ -65,7 +62,7 @@ case "${Opts[action]:-show}" in
vault="$1"
# Check if the damn thing already exists
vault_exists "$vault" && die 1 "Vault '$vault' already exists"
vault_exists "$vault" && System.die 1 "Vault '$vault' already exists"
new_pass=$(pwgen "${Opts[pw_len]}" 1)
@ -88,7 +85,7 @@ case "${Opts[action]:-show}" in
vault=$1
field=${2:-password}
vault_exists "$vault" || die 1 "Vault does not exist: $1"
vault_exists "$vault" || System.die 1 "Vault does not exist: $1"
if vault_is_json "$vault"; then
pass=$(pass "$vault" | jshon -e "$field" -u)
@ -108,8 +105,8 @@ case "${Opts[action]:-show}" in
(add) check_argn "${#@}" 2 3;
vault=$1 field=$2; data=$3
vault_exists "$vault" || die 1 "Vault does not exist: $1"
vault_is_json "$vault" || die 1 "Not a valid json vault: $1"
vault_exists "$vault" || System.die 1 "Vault does not exist: $1"
vault_is_json "$vault" || System.die 1 "Not a valid json vault: $1"
json=$( pass "$vault" )
jshon -s "$data" -i "$field" <<< "$json" | pass insert -m "$vault"