From d437da40fe4d83f566734fe7f07a28803ea60306 Mon Sep 17 00:00:00 2001 From: zawz Date: Tue, 6 Apr 2021 14:56:16 +0200 Subject: [PATCH] add file operation + fixes --- Makefile | 2 +- src/help.sh | 3 ++- src/main.sh | 17 +++++++++-------- src/operation.sh | 20 ++++++++++++++++++++ src/util.sh | 2 +- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index f19df4a..679f7fb 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ var_exclude = ZPASS_.* XDG_.* REMOTE_.* DISPLAY CONFIGFILE TMPDIR fct_exclude = _tty_on zpass: src/* - lxsh -o zpass -m --minimize-var --exclude-var "$(var_exclude)" --minimize-fct --exclude-fct "$(fct_exclude)" --remove-unused src/main.sh + lxsh -o zpass -m --minify-quotes --minify-var --exclude-var "$(var_exclude)" --minify-fct --exclude-fct "$(fct_exclude)" --remove-unused src/main.sh debug: src/* lxsh -o zpass src/main.sh diff --git a/src/help.sh b/src/help.sh index b69b9ff..bfa54d1 100644 --- a/src/help.sh +++ b/src/help.sh @@ -15,7 +15,8 @@ usage() create Create a file or change key get Get values of targets copy Copy the target value to clipboard. Shortcut 'x' - set Set the value of target + set Set directly the value of target + file Set the targetted value from file content add Prompt for input value on paths new Generate a random password at target rm Delete targets diff --git a/src/main.sh b/src/main.sh index 2319a90..14df22f 100644 --- a/src/main.sh +++ b/src/main.sh @@ -20,15 +20,16 @@ case $arg in ch|cached) get_key_cached >/dev/null 2>&1 ;; rmc|rm-cache) delete_cache 0 >/dev/null 2>&1 ;; c|create) create ;; - t|tree) _tree "$@" ;; - s|set) _set "$@" ;; - a|add) add "$@" ;; - n|new) new "$@" ;; - g|get) get "$@" ;; - x|copy) copy "$1" ;; - e|exec) archive_exec "$@" ;; + t|tree) sanitize_paths "$@" && _tree "$@" ;; + s|set) sanitize_paths "$1" && _set "$@" ;; + f|file) sanitize_paths "$1" && fileset "$@" ;; + a|add) sanitize_paths "$@" && add "$@" ;; + n|new) sanitize_paths "$@" && new "$@" ;; + g|get) sanitize_paths "$@" && get "$@" ;; + x|copy) sanitize_paths "$1" && copy "$1" ;; l|ls|list) sanitize_paths "$@" && __NOPACK=y archive_exec ls -Apw1 -- "$@" ;; r|rm) sanitize_paths "$@" && archive_exec rm -rf -- "$@" ;; - m|mv) sanitize_paths "$@" && archive_exec mv -f -- "$@" ;; + m|mv) sanitize_paths "$@" && move "$@" ;; + e|exec) archive_exec "$@" ;; *) [ -n "$ZPASS_UNK_OP_CALL" ] && "$0" $ZPASS_UNK_OP_CALL "$arg" "$@" ;; esac diff --git a/src/operation.sh b/src/operation.sh index 3aec44e..821e632 100644 --- a/src/operation.sh +++ b/src/operation.sh @@ -73,3 +73,23 @@ add() _set "$N" "$val" || return $? done } + +fileset() +{ + contents=$(cat "$2") || return $? + _set "$1" "$2" +} + +move() +{ + [ $# -lt 1 ] && return 1 + archive_exec sh -c 'set -e + for last ; do true ; done + if [ "$#" -gt 2 ] ; then + mkdir -p "$last" + else + mkdir -p "$(dirname "$last")" + fi + mv -f -- "$@" + ' sh "$@" +} diff --git a/src/util.sh b/src/util.sh index 43be9c4..e8eb09d 100644 --- a/src/util.sh +++ b/src/util.sh @@ -23,7 +23,7 @@ sanitize_paths() for N do echo "$N" | grep -q "^/" && echo "Path cannot start with /" >&2 && return 1 - echo "$N" | grep -qw ".." && echo "Path cannot contain .." >&2 && return 1 + echo "$N" | grep -Fqw ".." && echo "Path cannot contain .." >&2 && return 1 done return 0 }