Browse Source

initial commit: very basic util, README, perl apparatus

pull/1/head
Brennen Bearnes 5 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 @@
blib
_build
Build
MANIFEST
MANIFEST.bak
MANIFEST.SKIP.bak
MYMETA.json
MYMETA.yml

+ 23
- 0
Build.PL View File

@ -0,0 +1,23 @@
use Module::Build;
my $build = Module::Build->new(
module_name => 'App::CommandLog',
license => 'gpl',
requires => {
'Getopt::Long' => 0,
'POSIX' => 0,
'Cwd' => 0,
'DBI' => 0,
'Sys::Hostname' => 0,
'perl' => '5.10.0',
},
recommends => {
},
);
$build->create_build_script;

+ 77
- 0
README.md View File

@ -0,0 +1,77 @@
commandlog
==========
A small set of tools for logging and annotating command-line history in a
Linux or Unixlike environment. Requires Perl, sqlite, and zsh. (It
probably won't take much to add to Bash, but I haven't tried yet.)
So far, this is about an hour's worth of messing around one evening. It
doesn't do much. Here's what I know works:
```sh
git clone git@github.com:brennen/commandlog
cd commandlog
perl Build.PL
sudo ./Build install
```
Next, put the following in your `.zshrc`:
```zsh
function preexec {
commandlog add "$@"
}
```
Or, if using Oh-My-Zsh:
```zsh
function commandlog_preexec {
commandlog add "$@"
}
add-zsh-hook preexec commandlog_preexec
```
Now, with any luck, you can use:
```sh
commandlog log
```
And you should get output something like the following:
id = 96
command = commandlog log
expanded_command = commandlog log
path = /home/brennen/code/commandlog
hostname = exuberance
username = brennen
shell = /usr/bin/zsh
terminal = xterm
notes =
datetime = 2016-10-26 03:56:12
id = 95
command = vim README.md
expanded_command = vim README.md
path = /home/brennen/code/commandlog
hostname = exuberance
username = brennen
shell = /usr/bin/zsh
terminal = xterm
notes =
datetime = 2016-10-26 03:48:40
id = 94
command = ll
expanded_command = ls --color -l
path = /home/brennen/code/commandlog
hostname = exuberance
username = brennen
shell = /usr/bin/zsh
terminal = xterm
notes =
datetime = 2016-10-26 03:48:30
No promises though. If anybody tries this and has thoughts, please do let me
know.

+ 25
- 0
benchmark.pl View File

@ -0,0 +1,25 @@
#!/usr/bin/perl
use Benchmark;
$script = $ARGV[0];
# declare array
my @data;
# start timer
$start = new Benchmark;
for ($x = 0; $x <= 100; $x++) {
$output = `perl ./$script`;
}
# end timer
$end = new Benchmark;
# calculate difference
$diff = timediff($end, $start);
# report
print "Content-type: text/plain\n\n";
print "Time taken was ", timestr($diff, 'all'), " seconds";

+ 25
- 0
bin/commandlog View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
# This is a wrapper for commandlog, a tool for logging command-line history
function print_help() {
echo "Usage: $0 [command] [args]"
echo " $0 add Add a log entry."
echo " $0 log Show log entries."
echo " $0 -h Print this help message"
echo
echo "You must specify a command."
exit 1
}
if [[ $# -lt 1 ]]; then
print_help
fi
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
print_help
fi
SUBPROG="commandlog-$1"
shift
exec $SUBPROG "$@"

+ 72
- 0
bin/commandlog-add View File

@ -0,0 +1,72 @@
#!/usr/bin/env perl
=pod
=head1 NAME
commandlog-add - log command line history and context to an sqlite db
=head1 SYNOPSIS
# in zsh
function preexec {
commandlog add "$@"
}
=head1 AUTHOR
Brennen Bearnes <bbearnes@gmail.com>
=cut
use warnings;
use strict;
use 5.10.0;
use Cwd;
use DBI;
use Sys::Hostname;
my $dbfile = "/home/brennen/cli.db";
my $init_new = 0;
$init_new = 1 unless -f $dbfile;
my $command = $ARGV[0];
my $expanded_command = $ARGV[2];
# say join "\n", @ARGV;
# Skip things prefixed with a space:
if ($command =~ m/^[ ]+/) {
exit;
}
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
# Make a new database and table(s), if needed:
create_log_db($dbh) if $init_new;
my $sth = $dbh->prepare(q{
INSERT INTO commands (command, expanded_command, path, hostname, username, shell, terminal, datetime)
VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))
});
$sth->execute($command, $expanded_command, cwd(), hostname(), $ENV{USER}, $ENV{SHELL}, $ENV{TERM});
# TODO: maybe log exit status, timing, memory, load, distro
sub create_log_db {
my ($dbh) = @_;
$dbh->do(<<'SQL'
CREATE TABLE commands (
id integer primary key,
command text,
expanded_command text,
path text,
hostname text,
username text,
shell text,
terminal text,
notes text,
datetime text
);
SQL
);
}

+ 3
- 0
bin/commandlog-log View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
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 @@
=pod
=head1 NAME
App::CommandLog - Tools for logging command line history
=head1 SYNOPSIS
tk tk tk
=head1 INSTALLING
$ perl Build.PL
$ ./Build
$ ./Build install
=head1 DESCRIPTION
This is, for the moment, a stub module. Shared code may later be moved here.
=cut
package App::CommandLog;
our ($VERSION) = '0.0.1';
use strict;
use warnings;
# tk tk tk
=head1 SEE ALSO
tk tk tk
=head1 AUTHOR
Copyright 2016 Brennen Bearnes
commandlog is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
=cut
1;

+ 0
- 0
t/.gitkeep View File


Loading…
Cancel
Save