Show commands alphabetically sorted in help in bin/gruta.
[gruta.git] / bin / gruta
blob4ba47386b7997b87a073eaafe58b34db224cd17d
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use File::Temp;
8 use Gruta;
9 use Gruta::Source::DBI;
10 use Gruta::Source::FS;
11 use Gruta::Source::Mbox;
13 my $X = {
14 'copy' => [
15 'copy {src} {dst}',
16 'Copies the full source {src} to {dst}',
17 sub {
18 my $g = init();
19 my $new_src = arg();
20 my $dst = new_source( $new_src );
22 $dst->create();
24 $g->transfer_to_source( $dst );
27 'topics' => [
28 'topics {src}',
29 'Lists the topics in {src}',
30 sub {
31 my $g = init();
33 foreach my $t ($g->topics()) {
34 print $t, "\n";
38 'topic' => [
39 'topic {src} {topic_id}',
40 'Dumps topic data',
41 sub {
42 my $g = init();
43 my $topic_id = arg();
45 print get_topic($g, $topic_id);
48 'edit_topic' => [
49 'edit_topic {src} {topic_id}',
50 'Edits topic data',
51 sub {
52 my $g = init();
53 my $topic_id = arg();
55 my $fh = File::Temp->new();
56 print $fh get_topic($g, $topic_id);
57 my $fn = $fh->filename();
58 $fh->close();
60 my $mtime = (stat($fn))[9];
61 system('$EDITOR ' . $fn);
63 if ($mtime != (stat($fn))[9]) {
64 save_topic($g, $topic_id, $fn);
69 'stories' => [
70 'stories {src} {topic_id}',
71 'Lists all stories of a topic',
72 sub {
73 my $g = init();
74 my $topic_id = arg();
76 foreach my $s ($g->stories($topic_id)) {
77 print $s, "\n";
81 'story' => [
82 'story {src} {topic_id} {id}',
83 'Dumps story data',
84 sub {
85 my $g = init();
86 my $topic_id = arg();
87 my $id = arg();
89 print get_story($g, $topic_id, $id);
92 'edit_story' => [
93 'edit_story {src} {topic_id} {id}',
94 'Calls $EDITOR to edit story data',
95 sub {
96 my $g = init();
97 my $topic_id = arg();
98 my $id = arg();
100 my $fh = File::Temp->new();
101 print $fh get_story($g, $topic_id, $id);
102 my $fn = $fh->filename();
103 $fh->close();
105 my $mtime = (stat($fn))[9];
106 system('$EDITOR ' . $fn);
108 if ($mtime != (stat($fn))[9]) {
109 save_story($g, $topic_id, $id, $fn);
113 'filter_story' => [
114 'filter_story {src} {topic_id} {id} {command}',
115 'Filters story data through command (STDIN, STDOUT)',
116 sub {
117 my $g = init();
118 my $topic_id = arg();
119 my $id = arg();
120 my $filter_cmd = arg();
122 my $fhr = File::Temp->new();
123 print $fhr get_story($g, $topic_id, $id);
124 my $fnr = $fhr->filename();
125 $fhr->close();
127 my $fhw = File::Temp->new();
128 my $fnw = $fhw->filename();
129 $fhw->close();
131 system("$filter_cmd < $fnr > $fnw");
133 save_story($g, $topic_id, $id, $fnw);
136 'create' => [
137 'create {src}',
138 'Creates {src}',
139 sub {
140 init();
143 'tags' => [
144 'tags {src}',
145 'Lists all tags in {src}',
146 sub {
147 my $g = init();
149 foreach my $t ($g->tags()) {
150 print join(' ', @{$t}), "\n";
154 'stories_by_date' => [
155 'stories_by_date {src} {topic(s)} {num} {offset} [{from}] [{to}] [{future}]',
156 'Searches stories by date',
157 sub {
158 my $g = init();
159 my $topics = arg();
160 my $num = arg();
161 my $offset = arg();
162 my $from = arg_o();
163 my $to = arg_o();
164 my $future = arg_o();
166 if ($topics) {
167 $topics = [ split(':', $topics) ];
170 foreach my $s ($g->stories_by_date(
171 $topics,
172 num => $num,
173 offset => $offset,
174 from => $from,
175 to => $to,
176 future => $future
177 ) ) {
178 print join(' ', @{$s}), "\n";
182 'stats' => [
183 'stats {src}',
184 'Dumps statistics for {src}',
185 sub {
186 my $g = init();
187 my $n_topics = 0;
188 my $n_stories = 0;
189 my $n_hits = 0;
191 foreach my $t ($g->topics()) {
192 $n_topics++;
194 foreach my $s ($g->stories($t)) {
195 $n_stories++;
197 my $story = $g->story($t, $s);
199 $n_hits += $story->get('hits') || 0;
203 print "Topics: $n_topics, Stories: $n_stories, Hits: $n_hits\n";
206 'stories_by_tag' => [
207 'stories_by_tag {src} {topic(s)} {tag(s)}',
208 'Searches stories by tag(s)',
209 sub {
210 my $g = init();
211 my $topics = arg();
212 my $tags = arg();
214 if ($topics) {
215 $topics = [ split(':', $topics) ];
218 foreach my $s ($g->stories_by_tag($topics, $tags)) {
219 print join(' ', @{$s}), "\n";
225 my $cmd = arg();
227 my $c;
229 if (not $c = $X->{$cmd}) {
230 $cmd = undef;
231 usage();
234 # execute
235 $c->[2]();
237 exit 0;
240 sub arg
242 if (@ARGV) {
243 return shift(@ARGV);
246 usage();
250 sub arg_o
252 return shift(@ARGV) || shift;
255 sub init
257 my $src = new_source(arg());
258 my $g = Gruta->new( sources => $src );
260 return $g;
264 sub usage
266 if ($cmd) {
267 my $c = $X->{$cmd};
269 print "Usage: gruta ", $c->[0], "\n\n\t", $c->[1], "\n";
271 else {
272 print "Usage: gruta {cmd} [args...]\n\n";
274 foreach my $k (sort keys %{$X}) {
275 my $c = $X->{$k};
277 print ' ', sprintf('%-50s %s',
278 $c->[0], $c->[1]), "\n";
282 exit 1;
285 sub new_source
287 my $src_str = shift;
288 my $src;
290 if ($src_str =~ /^dbi:/) {
291 $src = Gruta::Source::DBI->new( string => $src_str );
293 elsif ($src_str =~ /^mbox:(.+)/) {
294 my $file = $1;
295 my ($topic_id) = ($file =~ /^(\w+)\.?.*$/);
297 $src = Gruta::Source::Mbox->new(
298 file => $file,
299 topic_id => $topic_id,
300 topic_name => $topic_id,
301 index_file => '/tmp/' . $topic_id . '.idx'
304 else {
305 $src = Gruta::Source::FS->new( path => $src_str );
308 return $src;
311 sub get_story
313 my $g = shift;
314 my $topic_id = shift;
315 my $id = shift;
316 my @r = ();
318 my $story = $g->story($topic_id, $id);
320 foreach my $f ($story->fields()) {
321 if ($f ne 'content') {
322 push (@r, $f . ': ' . ($story->get($f) || ''));
326 push(@r, 'tags: ' . join(', ', $story->tags()));
327 push(@r, '');
328 push(@r, $story->get('content'));
329 push(@r, '');
331 return join("\n", @r);
335 sub get_topic
337 my $g = shift;
338 my $topic_id = shift;
339 my @r = ();
341 my $topic = $g->topic($topic_id);
343 foreach my $f ($topic->afields()) {
344 push(@r, $f . ': ' . ($topic->get($f) || ''));
347 return join("\n", @r);
351 sub save_story
353 my $g = shift;
354 my $topic_id = shift;
355 my $id = shift;
356 my $fn = shift;
358 open F, $fn or die "Can't open $fn";
360 my $story = undef;
362 if ($id) {
363 $story = $g->story($topic_id, $id);
365 else {
366 $story = Gruta::Data::Story->new (
367 topic_id => $topic_id,
368 id => $id
372 while (<F>) {
373 chomp();
375 last if /^$/;
377 my ($key, $value) = (/^(\w+):\s*(.*)$/);
379 if ($key eq 'tags') {
380 $story->tags(split(/,\s*/, $value));
382 else {
383 $story->set($key, $value);
387 my $c = join('', <F>);
388 close F;
390 $story->set('content', $c);
392 if ($story->source()) {
393 $story->save();
395 else {
396 $g->insert_story($story);
401 sub save_topic
403 my $g = shift;
404 my $topic_id = shift;
405 my $fn = shift;
407 open F, $fn or die "Can't open $fn";
409 my $topic = undef;
411 if ($topic_id) {
412 $topic = $g->topic($topic_id);
414 else {
415 $topic = Gruta::Data::Topic->new (
416 topic_id => $topic_id,
420 while (<F>) {
421 chomp();
423 last if /^$/;
425 my ($key, $value) = (/^(\w+):\s*(.*)$/);
427 $topic->set($key, $value);
430 if ($topic->source()) {
431 $topic->save();
433 else {
434 $g->insert_topic($topic);