- #!/usr/bin/env perl
-
- =pod
-
- =head1 NAME
-
- commandlog-log - show commandlog history
-
- =head1 SYNOPSIS
-
- # Get most recent log entries:
- commandlog log
-
- # Get the 100 most recent log entries:
- commandlog log -n 100
-
- # Get all log entries:
- commandlog log -n 0
-
- # Get entries for January 1st (this is crap and needs work):
- commandlog log --after="2019-01-01 00:00:00" --before="2019-01-02 00:00:00"
-
- # Get entries for the current directory:
- commandlog log --path .
-
- # Run some query with debugging output enabled:
- commandlog log --debug [...]
-
- =head1 AUTHOR
-
- Brennen Bearnes
-
- =cut
-
- use warnings;
- use strict;
- use 5.10.0;
-
- use Cwd;
- use Data::Dumper;
- use DBI;
- use File::Spec;
- use Getopt::Long;
- use Pod::Usage;
- use SQL::Abstract;
- use Sys::Hostname;
-
- use App::CommandLog;
-
- # Handle options, including help generated from the POD above.
- my $debug = 0;
- my $limit;
- my $before_date;
- my $after_date;
- my $path;
- GetOptions(
- 'debug' => \$debug,
- 'number=s' => \$limit,
- 'before=s' => \$before_date,
- 'after=s' => \$after_date,
- 'path=s' => \$path,
- 'help' => sub { pod2usage(0) },
- ) or pod2usage(2);
-
- unless (defined $limit) {
- unless ($before_date || $after_date) {
- $limit = 10;
- }
- }
-
- my %where = ();
- if (length($after_date) && length($before_date))
- {
- # We have both a start and an endpoint.
- $where{datetime} = [ '-and' => { '>', $after_date },
- { '<', $before_date } ];
- }
- elsif (length($after_date))
- {
- # Just a starting point.
- $where{datetime} = { '>', $after_date };
- }
- elsif (length($before_date))
- {
- # Just an ending point.
- $where{datetime} = { '<', $before_date };
- }
-
- if (defined $path) {
- # Resolve stuff like ".", "..", etc.:
- my $abs_path = File::Spec->rel2abs($path);
- $where{path} = { '=', $abs_path };
- }
-
- my $limit_clause = '';
- if ($limit) {
- $limit_clause = sprintf(' LIMIT %d', $limit);
- }
-
- my $dbfile = $ENV{HOME} . "/cli.db";
- my $dbh = App::CommandLog::get_dbh($dbfile);
-
- my $sql = SQL::Abstract->new;
- my ($query, @bind) = $sql->select(
- 'commands',
- '*',
- \%where,
- {-desc => 'datetime'}
- );
-
- if ($debug) {
- say STDERR $query . $limit_clause;
- say STDERR Dumper(%where);
- say STDERR Dumper(@bind);
- }
-
- my $sth = $dbh->prepare($query . $limit_clause);
- $sth->execute(@bind);
-
- while (my $data = $sth->fetchrow_hashref()) {
- say join "\t", $data->{id}, $data->{datetime}, $data->{command};
- }
-
- $sth->execute();
|