sekrit: add force flag to add subcommand

This commit is contained in:
Lucas 2020-06-15 02:43:34 +00:00
parent b37356c964
commit ec3f639580
2 changed files with 20 additions and 5 deletions

View File

@ -19,6 +19,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Cm add .Cm add
.Op Fl f
.Ar key .Ar key
.Op Ar value ... .Op Ar value ...
.Nm .Nm
@ -51,7 +52,7 @@ Because of this,
can be used as an account credentials manager, can be used as an account credentials manager,
or as a general-purpose key-value store of encrypted information. or as a general-purpose key-value store of encrypted information.
.Bl -tag -width Ds .Bl -tag -width Ds
.It Nm Cm add Ar key Op Ar value ... .It Nm Cm add Oo Fl f Oc Ar key Op Ar value ...
Adds a value to Adds a value to
.Ar key . .Ar key .
.Ar value .Ar value
@ -64,7 +65,9 @@ will read the value from standard input.
.Cm add .Cm add
will fail if will fail if
.Ar key .Ar key
already has a value. already has a value, unless
.Fl f
is specified.
.It Nm Cm cp Oo Fl k Oc Ar key .It Nm Cm cp Oo Fl k Oc Ar key
Decrypts the value associated with Decrypts the value associated with
.Ar key .Ar key

View File

@ -29,7 +29,7 @@ usage()
{ {
cat - <<. >&2 cat - <<. >&2
Usage: Usage:
${0##*/} add key [value ...] ${0##*/} add [-f] key [value ...]
${0##*/} cp [-k] key ${0##*/} cp [-k] key
${0##*/} gen [-l length] [chars] ${0##*/} gen [-l length] [chars]
${0##*/} get key ${0##*/} get key
@ -63,6 +63,18 @@ to_number()
sekrit_add() sekrit_add()
{ {
force=no
OPTIND=1
while getopts f flag; do
case "$flag" in
k) force=yes
;;
*) usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -ge 1 ] && [ -n "$1" ] || usage [ $# -ge 1 ] && [ -n "$1" ] || usage
key=$1 key=$1
check_key "$key" check_key "$key"
@ -71,14 +83,14 @@ sekrit_add()
f=$SEKRIT_DIR/$key.gpg f=$SEKRIT_DIR/$key.gpg
mkdir -p "${f%/*}" mkdir -p "${f%/*}"
[ -f "$f" ] && err "key $key already exists" [ -f "$f" ] && [ $force = no ] && err "key $key already exists"
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
# use all additional parameters as a single string # use all additional parameters as a single string
printf "%s\n" "$*" printf "%s\n" "$*"
else else
cat - cat -
fi | gpg2 -qae -r "$SEKRIT_GPG_ID" >"$f" fi | gpg2 -qae -r "$SEKRIT_GPG_ID" >|"$f"
# make it read-only # make it read-only
chmod -- 400 "$f" chmod -- 400 "$f"
} }