Note to Self... er... Google Calendar

Created 2007-04-12 / Edited 2007-04-12

A few years ago, while doing some contract work, I created a simple yet effective way to monitor my time. The idea was taken from some other command line junkie, but I simpliefied it a bit. I dropped a command called "L" into my ~/bin directory that looks like this:

#!/bin/sh

if [ -n "$*" ]
then
  echo "`date '+%Y%m%d%H%M%S'` $*" >> ~/tlt/private/log/LOG-$HOSTNAME
else
  (for i in ~/tlt/private/log/LOG-* ; do tail -20 $i ; done) | sort | tail -20 | perl -pe 's/^(....)(..)(..)(..)(..)(..)/$1.$2.$3 $4:$5/'
fi

Well... in truth this is what it evolved into. The command has two modes, logging and tailing. In logging mode, it takes the rest of the line, adds a timestamp, and shoves it in a logfile. In the tail mode it shows you the tail of all available log files (they are created on a per-host basis), sorted and formatted for human consumption.

This creates a quite grep-able log of the things I do. Typical usage:

L just got into work
L the server went down again
L dont forget to go to the store tonight

Speaking of not forgetting things, I'm trying to use Google Calendar in conjunction with Beth (using a shared calendar) to keep track of our schedule. It has a pretty good UI, and Beth uses gmail all the time anyway. I created a separate calendar to take some random notes. You can see where this is going... can't you?

Looking at cpan:Net::Google::Calendar, the whole thing is quite easy. First I converted the 'L' script to perl (or, partially converted it). Then I added just a bit more to get:

#!/usr/bin/perl

use strict;
use Net::Google::Calendar;
use Net::Google::Calendar::Entry;

my $host = `hostname`;
chop $host;

if(@ARGV) {
  my $text = "@ARGV";

  # First we add it to the local LOG file
  my $ts = `date '+%Y%m%d%H%M%S'`;
  chop $ts;
  `echo "$ts $text" >> ~/tlt/private/log/LOG-$host`;

  # Now lets add it to the google calendar
  my $username = 'awwaiid@gmail.com';
  my $password = 'awwaiidxxx';

  my $cal = Net::Google::Calendar->new;
  unless($cal->login($username, $password)) {
    print "Error: $@\n";
    die;
  }

  # Gotta select the 'Notes' calendar
  for ($cal->get_calendars) {
    $cal->set_calendar($_) if ($_->title eq 'Notes');
  }

  # Set up the entry
  my $entry = Net::Google::Calendar::Entry->new();
  $entry->title($text);
  $entry->location($host);
  $entry->transparency('transparent');
  $entry->when(DateTime->now,
    DateTime->now() + DateTime::Duration->new( minutes => 1 ) );

  # And finally, add it
  $cal->add_entry($entry);

} else {

  print `(for i in ~/tlt/private/log/LOG-* ; do tail -20 \$i ; done) | sort | tail -20 | perl -pe 's/^(....)(..)(..)(..)(..)(..)/\$1.\$2.\$3 \$4:\$5/'`;

}

... and now I can use my good old 'L' command from any random terminal, and if it is properly connected to the internet then it'll post up my note. Of course that brings up the point that I might not have internet available (like on my laptop or something)... but I'll leave other forms of syncing as an excercise for the reader.