source: bookmarks/trunk/bkmk @ 85

Last change on this file since 85 was 85, checked in by peter, 9 years ago

issue #9: enhanced command line tool

  • added action tag to set the tag list for a bookmark
  • fixed the list query to actually query by tags
  • changed the output to just display the YAML of the public bookmark properties
  • Property svn:executable set to *
File size: 2.1 KB
RevLine 
[2]1#!/usr/bin/perl -w
2use strict;
3
[83]4use FindBin;
5use lib "$FindBin::RealBin/lib";
6
[2]7use YAML;
[15]8use Getopt::Long;
[2]9
[83]10use Bookmarks;
11
[15]12GetOptions(
13    'file|f=s' => \my $DBNAME,
[83]14    'title=s' => \my $TITLE,
[15]15);
[2]16
[83]17my $dbname = $DBNAME || $ENV{BKMK_DBNAME};
18die "Usage: $0 --file <dbname> <command>\n" unless $dbname;
[2]19
20my $bookmarks = Bookmarks->new({
[83]21    dbname => $dbname,
[2]22});
23
24my $command = shift;
25
26my %action_for = (
[47]27    init => sub {
28        $bookmarks->create_tables;
29    },
[83]30   
[2]31    get => sub {
32        my $identifier = shift;
[85]33        my $bookmark = find_bookmark($identifier);
34        print $bookmark ? Dump($bookmark->TO_JSON) : "Not Found\n";
[2]35    },
[83]36   
[2]37    add => sub {
[83]38        my ($uri, @tags) = @_;
39        my $title = defined $TITLE ? $TITLE : fetch_title($uri);
[2]40        my $bookmark = $bookmarks->add({ uri => $uri, title => $title, tags => \@tags });
[85]41        print Dump($bookmark->TO_JSON);
[2]42    },
[83]43
[14]44    list => sub {
[83]45        my @tags = @_;
[85]46        my $resources = $bookmarks->get_bookmarks({
47            tags => \@tags
[20]48        });
[14]49        # TODO: list by tags, date, etc.
50        # TODO: coordinate this commandline script with the CGI app
[85]51        print Dump([ map { $_->TO_JSON } @{ $resources->results } ]);
[15]52    },
[83]53
[85]54    tag => sub {
55        my ($identifier, @tags) = @_;
56        my $bookmark = find_bookmark($identifier);
57        if ($bookmark) {
58            $bookmark->tags(\@tags);
59            $bookmarks->update($bookmark);
60            print Dump($bookmark->TO_JSON);
61        } else {
62            die "Not found\n";
63        }
64    },
65
66    #TODO: interactive editing of a bookmark
67
[83]68    # bulk loading
[15]69    load => sub {
70        my ($src_file) = @_;
71        my $src_bookmarks = YAML::LoadFile($src_file);
72        for my $bookmark (@{ $src_bookmarks }) {
73            $bookmarks->add($bookmark);
74        }
75    },
[2]76);
77
78$action_for{$command}->(@ARGV);
79
[85]80sub find_bookmark {
81    my $identifier = shift;
82    my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier };
83    return $bookmarks->get_bookmark($query);
84}
85
[83]86sub fetch_title {
87    my $uri = shift;
88    require WWW::Mechanize;
89    my $mech = WWW::Mechanize->new;
90    $mech->get($uri);
91    return $mech->title || $uri;
92}
Note: See TracBrowser for help on using the repository browser.