]> pd.if.org Git - zpackage/blob - zpm-log
add -v option to log to write to stderr
[zpackage] / zpm-log
1 #!/bin/sh
2
3 #-T timestamp
4 #-a action
5 #-t target
6 #-i insert, otherwise search
7
8 op=search
9 action=
10 json=0
11 verbose=0
12
13 pkgfile=${ZPMDB:-/var/lib/zpm/local.db}
14
15 while getopts f:t:a:T:ijv opt; do
16         case $opt in
17                 f) pkgfile="$OPTARG" ;;
18                 t) target="$OPTARG" ;;
19                 a) action="$OPTARG" ;;
20                 T) timestamp="$OPTARG" ;;
21                 i) op=insert ;;
22                 j) json=1 ;;
23                 v) verbose=1 ;;
24                 *) printf '%s unknown option %s\n' "$0" "$opt" ; exit 1 ;;
25         esac
26 done
27 shift $((OPTIND - 1))
28
29 if [ $op = 'insert' ]; then
30 {
31         cols='action,target,info';
32         if [ -n "$timestamp" ]; then
33                 cols='ts,action,target,info'
34         fi
35
36         printf "insert into zpmlog (%s)\n" $cols
37         printf 'values(';
38         if [ -n "$timestamp" ]; then
39                 printf "'%s', " "$timestamp"
40         fi
41         if [ -n "$action" ]; then
42                 printf "'%s', " "$action"
43         else
44                 printf 'NULL,'
45         fi
46         if [ -n "$target" ]; then
47                 printf "'%s', " "$target"
48         else
49                 printf 'NULL,'
50         fi
51         printf "'%s')" "$*"
52         printf ';\n'
53 } | zpm shell $pkgfile
54
55 if [ $verbose -ne 0 ]; then
56         printf "%s %s %s\n" "$action" "$target" "$*" 1>&2
57 fi
58
59 fi
60
61 cols='ts,target,action,info'
62
63 if [ $op = 'search' ]; then
64
65 if [ $json -eq 1 ]; then
66         cols="json_group_array(json_object('ts',ts,'target',target,'action',action,'info',info))"
67 fi
68
69 {
70         printf ".separator ' '\n"
71         printf 'select %s from zpmlog where\n' "$cols";
72         if [ -n "$timestamp" ]; then
73                 printf "ts >= '%s' and " "$timestamp"
74         fi
75
76         if [ -n "$action" ]; then
77                 printf "action like '%s' and " "$action"
78         fi
79         if [ -n "$target" ]; then
80                 printf "target like '%s' and " "$target"
81         fi
82         printf "true order by ts;\n"
83 } | zpm shell $pkgfile
84
85 fi