Browse Source

initial commit: very basic util, README, perl apparatus

Brennen Bearnes 2 years ago
commit
efd63a6211
9 changed files with 285 additions and 0 deletions
  1. 8
    0
      .gitignore
  2. 23
    0
      Build.PL
  3. 77
    0
      README.md
  4. 25
    0
      benchmark.pl
  5. 25
    0
      bin/commandlog
  6. 72
    0
      bin/commandlog-add
  7. 3
    0
      bin/commandlog-log
  8. 52
    0
      lib/App/CommandLog.pm
  9. 0
    0
      t/.gitkeep

+ 8
- 0
.gitignore View File

@@ -0,0 +1,8 @@
1
+blib
2
+_build
3
+Build
4
+MANIFEST
5
+MANIFEST.bak
6
+MANIFEST.SKIP.bak
7
+MYMETA.json
8
+MYMETA.yml

+ 23
- 0
Build.PL View File

@@ -0,0 +1,23 @@
1
+use Module::Build;
2
+
3
+my $build = Module::Build->new(
4
+
5
+  module_name => 'App::CommandLog',
6
+
7
+  license  => 'gpl',
8
+
9
+  requires => {
10
+    'Getopt::Long'             => 0,
11
+    'POSIX'                    => 0,
12
+    'Cwd'                      => 0,
13
+    'DBI'                      => 0,
14
+    'Sys::Hostname'            => 0,
15
+    'perl'                     => '5.10.0',
16
+  },
17
+
18
+  recommends => {
19
+  },
20
+
21
+);
22
+
23
+$build->create_build_script;

+ 77
- 0
README.md View File

@@ -0,0 +1,77 @@
1
+commandlog
2
+==========
3
+
4
+A small set of tools for logging and annotating command-line history in a
5
+Linux or Unixlike environment.  Requires Perl, sqlite, and zsh.  (It
6
+probably won't take much to add to Bash, but I haven't tried yet.)
7
+
8
+So far, this is about an hour's worth of messing around one evening.  It
9
+doesn't do much.  Here's what I know works:
10
+
11
+```sh
12
+git clone git@github.com:brennen/commandlog
13
+cd commandlog
14
+perl Build.PL
15
+sudo ./Build install
16
+```
17
+
18
+Next, put the following in your `.zshrc`:
19
+
20
+```zsh
21
+function preexec {
22
+  commandlog add "$@"
23
+}
24
+```
25
+
26
+Or, if using Oh-My-Zsh:
27
+
28
+```zsh
29
+function commandlog_preexec {
30
+  commandlog add "$@"
31
+}
32
+add-zsh-hook preexec commandlog_preexec
33
+```
34
+
35
+Now, with any luck, you can use:
36
+
37
+```sh
38
+commandlog log
39
+```
40
+
41
+And you should get output something like the following:
42
+
43
+                  id = 96
44
+             command = commandlog log
45
+    expanded_command = commandlog log
46
+                path = /home/brennen/code/commandlog
47
+            hostname = exuberance
48
+            username = brennen
49
+               shell = /usr/bin/zsh
50
+            terminal = xterm
51
+               notes = 
52
+            datetime = 2016-10-26 03:56:12
53
+
54
+                  id = 95
55
+             command = vim README.md
56
+    expanded_command = vim README.md
57
+                path = /home/brennen/code/commandlog
58
+            hostname = exuberance
59
+            username = brennen
60
+               shell = /usr/bin/zsh
61
+            terminal = xterm
62
+               notes = 
63
+            datetime = 2016-10-26 03:48:40
64
+
65
+                  id = 94
66
+             command = ll
67
+    expanded_command = ls --color -l
68
+                path = /home/brennen/code/commandlog
69
+            hostname = exuberance
70
+            username = brennen
71
+               shell = /usr/bin/zsh
72
+            terminal = xterm
73
+               notes = 
74
+            datetime = 2016-10-26 03:48:30
75
+
76
+No promises though.  If anybody tries this and has thoughts, please do let me
77
+know.

+ 25
- 0
benchmark.pl View File

@@ -0,0 +1,25 @@
1
+#!/usr/bin/perl
2
+
3
+use Benchmark;
4
+
5
+$script = $ARGV[0];
6
+
7
+# declare array
8
+my @data;
9
+
10
+# start timer
11
+$start = new Benchmark;
12
+
13
+for ($x = 0; $x <= 100; $x++) {
14
+  $output = `perl ./$script`;
15
+}
16
+
17
+# end timer
18
+$end = new Benchmark;
19
+
20
+# calculate difference
21
+$diff = timediff($end, $start);
22
+
23
+# report
24
+print "Content-type: text/plain\n\n";
25
+print "Time taken was ", timestr($diff, 'all'), " seconds";

+ 25
- 0
bin/commandlog View File

@@ -0,0 +1,25 @@
1
+#!/usr/bin/env bash
2
+
3
+# This is a wrapper for commandlog, a tool for logging command-line history
4
+
5
+function print_help() {
6
+  echo "Usage: $0 [command] [args]"
7
+  echo "    $0 add  Add a log entry."
8
+  echo "    $0 log  Show log entries."
9
+  echo "    $0 -h   Print this help message"
10
+  echo
11
+  echo "You must specify a command."
12
+  exit 1
13
+}
14
+
15
+if [[ $# -lt 1 ]]; then
16
+  print_help
17
+fi
18
+
19
+if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
20
+  print_help
21
+fi
22
+
23
+SUBPROG="commandlog-$1"
24
+shift
25
+exec $SUBPROG "$@"

+ 72
- 0
bin/commandlog-add View File

@@ -0,0 +1,72 @@
1
+#!/usr/bin/env perl
2
+
3
+=pod
4
+
5
+=head1 NAME
6
+
7
+commandlog-add - log command line history and context to an sqlite db
8
+
9
+=head1 SYNOPSIS
10
+
11
+    # in zsh
12
+    function preexec {
13
+      commandlog add "$@"
14
+    }
15
+
16
+=head1 AUTHOR
17
+
18
+Brennen Bearnes <bbearnes@gmail.com>
19
+
20
+=cut
21
+
22
+use warnings;
23
+use strict;
24
+use 5.10.0;
25
+use Cwd;
26
+use DBI;
27
+use Sys::Hostname;
28
+
29
+my $dbfile = "/home/brennen/cli.db";
30
+
31
+my $init_new = 0;
32
+$init_new = 1 unless -f $dbfile;
33
+
34
+my $command = $ARGV[0];
35
+my $expanded_command = $ARGV[2];
36
+# say join "\n", @ARGV;
37
+
38
+# Skip things prefixed with a space:
39
+if ($command =~ m/^[ ]+/) {
40
+  exit;
41
+}
42
+
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;
47
+
48
+my $sth = $dbh->prepare(q{
49
+  INSERT INTO commands (command, expanded_command, path, hostname, username, shell, terminal, datetime)
50
+     VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))
51
+});
52
+$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
+}

+ 3
- 0
bin/commandlog-log View File

@@ -0,0 +1,3 @@
1
+#!/usr/bin/env bash
2
+
3
+sqlite3 -line ~/cli.db 'SELECT * FROM commands ORDER BY datetime DESC LIMIT 3;'

+ 52
- 0
lib/App/CommandLog.pm View File

@@ -0,0 +1,52 @@
1
+=pod
2
+
3
+=head1 NAME
4
+
5
+App::CommandLog - Tools for logging command line history
6
+
7
+=head1 SYNOPSIS
8
+
9
+tk tk tk
10
+
11
+=head1 INSTALLING
12
+
13
+    $ perl Build.PL
14
+    $ ./Build
15
+    $ ./Build install
16
+
17
+=head1 DESCRIPTION
18
+
19
+This is, for the moment, a stub module.  Shared code may later be moved here.
20
+
21
+=cut
22
+
23
+package App::CommandLog;
24
+
25
+our ($VERSION) = '0.0.1';
26
+
27
+use strict;
28
+use warnings;
29
+
30
+# tk tk tk
31
+
32
+=head1 SEE ALSO
33
+
34
+tk tk tk
35
+
36
+=head1 AUTHOR
37
+
38
+Copyright 2016 Brennen Bearnes
39
+
40
+    commandlog is free software; you can redistribute it and/or modify
41
+    it under the terms of the GNU General Public License as published by
42
+    the Free Software Foundation; either version 2 of the License, or
43
+    (at your option) any later version.
44
+
45
+    This program is distributed in the hope that it will be useful,
46
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
47
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48
+    GNU General Public License for more details.
49
+
50
+=cut
51
+
52
+1;

+ 0
- 0
t/.gitkeep View File