Support tags for individual tips
authorAlexander Wirt <formorer@grml.org>
Mon, 13 Jun 2011 14:16:21 +0000 (16:16 +0200)
committerChristian Hofstaedtler <ch@grml.org>
Tue, 6 Dec 2011 11:36:47 +0000 (12:36 +0100)
grml-tips
grml-tips-tags [new file with mode: 0755]

index ec790d9..8ba4df6 100755 (executable)
--- a/grml-tips
+++ b/grml-tips
@@ -4,7 +4,6 @@
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, (c) Alexander Wirt <formorer@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
-# Latest change: Sam Mär 03 15:35:38 CET 2007 [mika]
 ################################################################################
 
 use strict;
@@ -14,6 +13,7 @@ use feature 'say';
 use Term::ReadKey;
 use Time::HiRes;
 use LWP::UserAgent;
+use Getopt::Long;
 
 =head1 NAME
 
@@ -21,7 +21,7 @@ B<grml-tips> - query a signature file for a specific keyword and display results
 
 =head1 SYNOPSIS
 
-B<grml-tips> [OPTION] I<keyword>
+B<grml-tips> [OPTION] I<searchpattern|tag>
 
 =head1 DESCRIPTION
 
@@ -35,6 +35,10 @@ This manual page documents briefly the B<grml-tips> command.
 
 Print this help and exit.
 
+=item B<--tagsonly>
+
+Match on tags only instead of the whole tip 
+
 =back
 
 =head1 EXAMPLES
@@ -68,20 +72,29 @@ grml-tips was written by Alexander Wirt <formorer@grml.org>
 =cut
 
 my $grml_tips = '/usr/share/grml-tips/grml_tips';
+
+my $help;
+my $tagsonly;
+
+my $result = GetOptions (
+    "help" => \$help,
+    "tagsonly" => \$tagsonly
+);
+
 my $pattern   = shift;
 
-if ( $pattern eq '' ) {
-    pod2usage(
-        {   -message => 'No search pattern provided',
-            -exitval => -1,
-        }
-    );
-}
-elsif ( $pattern eq '--help' ) {
-    pod2usage();
-}
+#help if pattern is missing;
+pod2usage(
+    {   -message => 'No search pattern provided',
+        -exitval => -1,
+    }
+) unless $pattern;
+
+#help if help is wanted
+pod2usage() if $help;
 
 my @tips;
+
 if ( !open( my $fh, '<', "$grml_tips" ) ) {
     say STDERR "Error: File \"$grml_tips\" not found.";
     say STDERR "Exiting.";
@@ -96,19 +109,26 @@ else {
             $tip .= $line;
         }
         else {
-            $tips_found++;
-            if ( $tip =~ /$pattern/mi ) {
-
-                #$tip .= $line;
-
-                my $header = "Grml Tip Number $tips_found\n";
-                my $line = "-" x ( length($header) - 1 ) . "\n\n";
+            my $header = "Grml Tip Number $tips_found\n";
+            my $line = "-" x ( length($header) - 1 ) . "\n\n";
 
-                push @tips, $header . $line . $tip . "\n";
-                $tip = '';
-            }
-            else {
+            $tips_found++;
+            if ($tagsonly) {
+                #extract tags from tip
+                my ($tag) = $tip =~ /^Tags: (.*)$/m;
+                my @tags = split(/[, ]+/, $tag);
+                if (grep(/^$pattern$/i, @tags) ) {
+                    push @tips, $header . $line . $tip . "\n";
+                }
                 $tip = '';
+            } else {
+                if ( $tip =~ /$pattern/mi ) {
+                    push @tips, $header . $line . $tip . "\n";
+                    $tip = '';
+                }
+                else {
+                    $tip = '';
+                }
             }
         }
     }
diff --git a/grml-tips-tags b/grml-tips-tags
new file mode 100755 (executable)
index 0000000..c1cdd0d
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+# Filename:      grml-tips-tags
+# Purpose:       Extract available patterns from grml-tips file
+# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, (c) Alexander Wirt <formorer@grml.org>
+# Bug-Reports:   see http://grml.org/bugs/
+# License:       This file is licensed under the GPL v2.
+################################################################################
+
+use strict;
+
+my $grml_tips = '/usr/share/grml-tips/grml_tips';
+open (my $fh, '<', $grml_tips) or die "Could not open $grml_tips: $!";
+
+my @tags;
+
+while (my $line = <$fh>) {
+    next unless $line =~ /^Tags: (.*)$/;
+    push @tags, split(/[, ]+/, $1);
+}
+
+#make tags unique
+my %seen = ();
+@tags = grep { ! $seen{$_} ++ } @tags;
+
+print join("\n", @tags) . "\n";