Command-line history logging utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.4 KiB

  1. #!/usr/bin/env perl
  2. =pod
  3. =head1 NAME
  4. commandlog-log - show commandlog history
  5. =head1 SYNOPSIS
  6. # Get most recent log entries:
  7. commandlog log
  8. # Get the 100 most recent log entries:
  9. commandlog log -n 100
  10. # Get all log entries:
  11. commandlog log -n 0
  12. # Get entries for January 1st (this is crap and needs work):
  13. commandlog log --after="2019-01-01 00:00:00" --before="2019-01-02 00:00:00"
  14. # Get entries for the current directory:
  15. commandlog log --path .
  16. # Run some query with debugging output enabled:
  17. commandlog log --debug [...]
  18. =head1 AUTHOR
  19. Brennen Bearnes
  20. =cut
  21. use warnings;
  22. use strict;
  23. use 5.10.0;
  24. use Cwd;
  25. use Data::Dumper;
  26. use DBI;
  27. use File::Spec;
  28. use Getopt::Long;
  29. use Pod::Usage;
  30. use SQL::Abstract;
  31. use Sys::Hostname;
  32. use App::CommandLog;
  33. # Handle options, including help generated from the POD above.
  34. my $debug = 0;
  35. my $limit;
  36. my $before_date;
  37. my $after_date;
  38. my $path;
  39. GetOptions(
  40. 'debug' => \$debug,
  41. 'number=s' => \$limit,
  42. 'before=s' => \$before_date,
  43. 'after=s' => \$after_date,
  44. 'path=s' => \$path,
  45. 'help' => sub { pod2usage(0) },
  46. ) or pod2usage(2);
  47. unless (defined $limit) {
  48. unless ($before_date || $after_date) {
  49. $limit = 10;
  50. }
  51. }
  52. my %where = ();
  53. if (length($after_date) && length($before_date))
  54. {
  55. # We have both a start and an endpoint.
  56. $where{datetime} = [ '-and' => { '>', $after_date },
  57. { '<', $before_date } ];
  58. }
  59. elsif (length($after_date))
  60. {
  61. # Just a starting point.
  62. $where{datetime} = { '>', $after_date };
  63. }
  64. elsif (length($before_date))
  65. {
  66. # Just an ending point.
  67. $where{datetime} = { '<', $before_date };
  68. }
  69. if (defined $path) {
  70. # Resolve stuff like ".", "..", etc.:
  71. my $abs_path = File::Spec->rel2abs($path);
  72. $where{path} = { '=', $abs_path };
  73. }
  74. my $limit_clause = '';
  75. if ($limit) {
  76. $limit_clause = sprintf(' LIMIT %d', $limit);
  77. }
  78. my $dbfile = $ENV{HOME} . "/cli.db";
  79. my $dbh = App::CommandLog::get_dbh($dbfile);
  80. my $sql = SQL::Abstract->new;
  81. my ($query, @bind) = $sql->select(
  82. 'commands',
  83. '*',
  84. \%where,
  85. {-desc => 'datetime'}
  86. );
  87. if ($debug) {
  88. say STDERR $query . $limit_clause;
  89. say STDERR Dumper(%where);
  90. say STDERR Dumper(@bind);
  91. }
  92. my $sth = $dbh->prepare($query . $limit_clause);
  93. $sth->execute(@bind);
  94. while (my $data = $sth->fetchrow_hashref()) {
  95. say join "\t", $data->{id}, $data->{datetime}, $data->{command};
  96. }
  97. $sth->execute();