Browse Source

commandlog-log add some query options; add SQL::Abstract to dependencies

Brennen Bearnes 2 months ago
parent
commit
e599de884e
2 changed files with 49 additions and 7 deletions
  1. 1
    0
      Build.PL
  2. 48
    7
      bin/commandlog-log

+ 1
- 0
Build.PL View File

@@ -12,6 +12,7 @@ my $build = Module::Build->new(
12 12
     'Cwd'           => 0,
13 13
     'DBI'           => 0,
14 14
     'DBD::SQLite'   => 0,
15
+    'SQL::Abstract' => 0,
15 16
     'Sys::Hostname' => 0,
16 17
     'perl'          => '5.10.0',
17 18
   },

+ 48
- 7
bin/commandlog-log View File

@@ -8,35 +8,76 @@ commandlog-log - show commandlog history
8 8
 
9 9
 =head1 SYNOPSIS
10 10
 
11
+    # Get most recent log entries:
12
+    commandlog log
13
+
14
+    # Get most recent log entries:
11 15
     commandlog log
12 16
 
13 17
 =head1 AUTHOR
14 18
 
15
-Brennen Bearnes <bbearnes@gmail.com>
19
+Brennen Bearnes
16 20
 
17 21
 =cut
18 22
 
19 23
 use warnings;
20 24
 use strict;
21 25
 use 5.10.0;
26
+
22 27
 use Cwd;
23 28
 use DBI;
29
+use Getopt::Long;
30
+use Pod::Usage;
31
+use SQL::Abstract;
24 32
 use Sys::Hostname;
33
+
25 34
 use App::CommandLog;
26 35
 
36
+# Handle options, including help generated from the POD above.
37
+my $limit = 10;
38
+my $before_date;
39
+my $after_date;
40
+GetOptions(
41
+  'number=s' => \$limit,
42
+  'before=s' => \$before_date,
43
+  'after=s'  => \$after_date,
44
+  'help'     => sub { pod2usage(0) },
45
+) or pod2usage(2);
46
+
47
+my %where = ();
48
+if (length($after_date) && length($before_date))
49
+{
50
+  # We have both a start and an endpoint.
51
+  $where{datetime} = [ '-and' => { '>', $after_date },
52
+                                 { '<', $before_date } ];
53
+}
54
+elsif (length($after_date))
55
+{
56
+  # Just a starting point.
57
+  $where{datetime} = { '>', $after_date };
58
+}
59
+elsif (length($before_date))
60
+{
61
+  # Just an ending point.
62
+  $where{datetime} = { '<', $before_date };
63
+}
64
+
65
+my $limit_clause = '';
66
+if ($limit) {
67
+  $limit_clause = sprintf(' LIMIT %d', $limit);
68
+}
69
+
27 70
 my $dbfile = $ENV{HOME} . "/cli.db";
28 71
 my $dbh = App::CommandLog::get_dbh($dbfile);
29 72
 
30
-my $sth = $dbh->prepare(q{
31
-  SELECT * FROM commands ORDER BY datetime DESC LIMIT 10
32
-});
73
+my $sql = SQL::Abstract->new;
74
+my ($query, @bind) = $sql->select('commands', '*', \%where, {-desc => 'datetime'});
33 75
 
34
-$sth->execute();
76
+my $sth = $dbh->prepare($query . $limit_clause);
77
+$sth->execute(@bind);
35 78
 
36 79
 while (my $data = $sth->fetchrow_hashref()) {
37 80
   say join "\t", $data->{datetime}, $data->{command};
38 81
 }
39 82
 
40 83
 $sth->execute();
41
-
42
-

Loading…
Cancel
Save