[2022] in Moira Commits

home help back first fref pref prev next nref lref last post

/svn/moira r4164 - trunk/moira/clients/mrtest

daemon@ATHENA.MIT.EDU (Garry Zacheiss)
Wed Jun 11 16:32:49 2014

Date: Wed, 11 Jun 2014 16:32:38 -0400
From: Garry Zacheiss <zacheiss@MIT.EDU>
Message-Id: <201406112032.s5BKWcet012412@drugstore.mit.edu>
To: moira-commits@MIT.EDU
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Author: zacheiss
Date: 2014-06-11 16:32:37 -0400 (Wed, 11 Jun 2014)
New Revision: 4164

Modified:
   trunk/moira/clients/mrtest/qy
Log:
Support json output.

Modified: trunk/moira/clients/mrtest/qy
===================================================================
--- trunk/moira/clients/mrtest/qy	2014-06-10 12:15:34 UTC (rev 4163)
+++ trunk/moira/clients/mrtest/qy	2014-06-11 20:32:37 UTC (rev 4164)
@@ -12,7 +12,7 @@
 
 sub usage {
   my $info = shift(@_) || '';
-  die "Usage: qy query args... [-db database] [-p user] [-n] [-s] [-f field,field,...]\n${info}Aborting";
+  die "${info}\nUsage: qy query args... [-db database] [-p user] [-n] [-s] [-f field,field,...]\n";
 }
 
 # Parse the command-line arguments
@@ -24,6 +24,7 @@
 
 $debug = 0;
 $short = 0;
+$json = 0;
 
 while (@ARGV) {
   my $arg = shift(@ARGV);
@@ -40,6 +41,13 @@
     @auth = "";
   } elsif ($arg eq "-s") {
     $short = 1;
+  } elsif ($arg eq "-json") {
+    my $rc = eval { require JSON; JSON->import(); 1; };
+    if (!$rc) {
+	usage "JSON support not available. Is the perl JSON module installed?\n";
+    } else {
+	$json = 1;
+    }
   } elsif ($arg eq "-f") {
     $arg = shift(@ARGV);
     @outfields = split(/, */, $arg);
@@ -51,6 +59,10 @@
 }
 usage "No query specified.\n"           unless @query;
 
+if ($short && $json) {
+    usage "-s and -json arguments are mutually exclusive.\n";
+}
+
 # $reply = read_reply()
 #     Reads the reply from mrtest, up to the next 'moira:  '.
 # Returns:
@@ -187,6 +199,16 @@
 			     } else {
 			       "";
 			     } } @outfields;
+    } elsif ($json) {
+	%outhash = ();
+	foreach (@outfields) {
+	    if (defined $out{$_}) {
+		$outhash{$_} = $out{$_};
+	    } else {
+		$outhash{$_} = "";
+	    }
+	}
+	print encode_json(\%outhash);
     } else {
       @names{@retl} = @fields;
       print join "\n", map { if (defined $out{$_}) {


home help back first fref pref prev next nref lref last post