Browse Source

some nonfunctional v2 changes

Brennen Bearnes 1 year ago
parent
commit
e5b6aab060
3 changed files with 84 additions and 38 deletions
  1. 3
    31
      bin/commandlog-add
  2. 42
    0
      bin/commandlog-log.pl
  3. 39
    7
      lib/App/CommandLog.pm

+ 3
- 31
bin/commandlog-add View File

@@ -22,51 +22,23 @@ Brennen Bearnes <bbearnes@gmail.com>
22 22
 use warnings;
23 23
 use strict;
24 24
 use 5.10.0;
25
+
26
+use App::CommandLog;
25 27
 use Cwd;
26
-use DBI;
27 28
 use Sys::Hostname;
28 29
 
29
-my $dbfile = $ENV{HOME} . "/cli.db";
30
-
31
-my $init_new = 0;
32
-$init_new = 1 unless -f $dbfile;
33
-
34 30
 my $command = $ARGV[0];
35 31
 my $expanded_command = $ARGV[2];
36
-# say join "\n", @ARGV;
37 32
 
38 33
 # Skip things prefixed with a space:
39 34
 if ($command =~ m/^[ ]+/) {
40 35
   exit;
41 36
 }
42 37
 
43
-my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
44
-
45
-# Make a new database and table(s), if needed:
46
-create_log_db($dbh) if $init_new;
38
+my $dbh = App::CommandLog::get_dbh();
47 39
 
48 40
 my $sth = $dbh->prepare(q{
49 41
   INSERT INTO commands (command, expanded_command, path, hostname, username, shell, terminal, datetime)
50 42
      VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))
51 43
 });
52 44
 $sth->execute($command, $expanded_command, cwd(), hostname(), $ENV{USER}, $ENV{SHELL}, $ENV{TERM});
53
-
54
-# TODO: maybe log exit status, timing, memory, load, distro
55
-sub create_log_db {
56
-  my ($dbh) = @_;
57
-  $dbh->do(<<'SQL'
58
-    CREATE TABLE commands (
59
-      id integer primary key,
60
-      command text,
61
-      expanded_command text,
62
-      path text,
63
-      hostname text,
64
-      username text,
65
-      shell text,
66
-      terminal text,
67
-      notes text,
68
-      datetime text
69
-    );
70
-SQL
71
-  );
72
-}

+ 42
- 0
bin/commandlog-log.pl View File

@@ -0,0 +1,42 @@
1
+#!/usr/bin/env perl
2
+
3
+=pod
4
+
5
+=head1 NAME
6
+
7
+commandlog-log - show commandlog history
8
+
9
+=head1 SYNOPSIS
10
+
11
+    commandlog log
12
+
13
+=head1 AUTHOR
14
+
15
+Brennen Bearnes <bbearnes@gmail.com>
16
+
17
+=cut
18
+
19
+use warnings;
20
+use strict;
21
+use 5.10.0;
22
+use Cwd;
23
+use DBI;
24
+use Sys::Hostname;
25
+use App::CommandLog;
26
+
27
+my $dbfile = $ENV{HOME} . "/cli.db";
28
+my $dbh = App::CommandLog::get_dbh($dbfile);
29
+
30
+my $sth = $dbh->prepare(q{
31
+  SELECT * FROM commands ORDER BY datetime DESC LIMIT 3
32
+});
33
+
34
+$sth->execute();
35
+
36
+while (my $data = $sth->fetchrow_hashref()) {
37
+  say join "\t", $data->{datetime}, $data->{command};
38
+}
39
+
40
+$sth->execute();
41
+
42
+

+ 39
- 7
lib/App/CommandLog.pm View File

@@ -14,10 +14,6 @@ tk tk tk
14 14
     $ ./Build
15 15
     $ ./Build install
16 16
 
17
-=head1 DESCRIPTION
18
-
19
-This is, for the moment, a stub module.  Shared code may later be moved here.
20
-
21 17
 =cut
22 18
 
23 19
 package App::CommandLog;
@@ -27,11 +23,47 @@ our ($VERSION) = '0.0.1';
27 23
 use strict;
28 24
 use warnings;
29 25
 
30
-# tk tk tk
26
+use DBI;
31 27
 
32
-=head1 SEE ALSO
28
+=item get_dbh()
33 29
 
34
-tk tk tk
30
+Get database handle for default commandlog database.
31
+
32
+=cut
33
+
34
+sub get_dbh {
35
+  my $dbfile = $ENV{HOME} . "/cli.db";
36
+
37
+  my $init_new = 0;
38
+  $init_new = 1 unless -f $dbfile;
39
+
40
+  my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
41
+
42
+  # Make a new database and table(s), if needed:
43
+  create_log_db($dbh) if $init_new;
44
+
45
+  return $dbh;
46
+}
47
+
48
+sub create_log_db {
49
+  # TODO: maybe log exit status, timing, memory, load, distro
50
+  my ($dbh) = @_;
51
+  $dbh->do(<<'SQL'
52
+    CREATE TABLE commands (
53
+      id integer primary key,
54
+      command text,
55
+      expanded_command text,
56
+      path text,
57
+      hostname text,
58
+      username text,
59
+      shell text,
60
+      terminal text,
61
+      notes text,
62
+      datetime text
63
+    );
64
+SQL
65
+  );
66
+}
35 67
 
36 68
 =head1 AUTHOR
37 69
 

Loading…
Cancel
Save