]> pd.if.org Git - zpackage/commitdiff
add zpm-log
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 10 Aug 2018 05:29:36 +0000 (05:29 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 10 Aug 2018 05:29:36 +0000 (05:29 +0000)
zpm-log [new file with mode: 0755]

diff --git a/zpm-log b/zpm-log
new file mode 100755 (executable)
index 0000000..198a407
--- /dev/null
+++ b/zpm-log
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+#-T timestamp
+#-a action
+#-t target
+#-i insert, otherwise search
+
+op=search
+action='log'
+
+pkgfile=${ZPMDB:-/var/lib/zpm/db.zpm}
+
+while getopts f:t:a:T:i opt; do
+       case $opt in
+               f) pkgfile="$OPTARG" ;;
+               t) target="$OPTARG" ;;
+               a) action="$OPTARG" ;;
+               T) timestamp="$OPTARG" ;;
+               i) op=insert ;;
+               *) printf '%s unknown option %s\n' "$0" "$opt" ; exit 1 ;;
+       esac
+done
+shift $((OPTIND - 1))
+
+if [ $op = 'insert' ]; then
+{
+       cols='action,target,info';
+       if [ -n "$timestamp" ]; then
+               cols='ts,action,target,info'
+       fi
+
+       printf "insert into zpmlog (%s)\n" $cols
+       printf 'values(';
+       if [ -n "$timestamp" ]; then
+               printf "'%s', " "$timestamp"
+       fi
+       if [ -n "$action" ]; then
+               printf "'%s', " "$action"
+       else
+               printf 'NULL,'
+       fi
+       if [ -n "$target" ]; then
+               printf "'%s', " "$target"
+       else
+               printf 'NULL,'
+       fi
+       printf "'%s')" "$*"
+       printf ';\n'
+} | zpm shell $pkgfile
+
+fi
+
+if [ $op = 'search' ]; then
+{
+       printf 'select ts,action,target,info from zpmlog where\n';
+       if [ -n "$timestamp" ]; then
+               printf "ts >= '%s' and " "$timestamp"
+       fi
+
+       if [ -n "$action" ]; then
+               printf "action = '%s' and " "$action"
+       fi
+       if [ -n "$target" ]; then
+               printf "target = '%s' and " "$target"
+       fi
+       printf "true;\n"
+} | zpm shell $pkgfile
+
+fi
+