From e217399c7d616bf358e4ca93e6d9f90d6f9e8469 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Fri, 20 Nov 2009 00:48:26 +0000
Subject: [PATCH] Fix for issue 2761 (ldapcompare: wrong return code when comparison is successfull or unsuccessful) Add a new option --script-friendly that eases the scripting of the output of the ldapcompare command.  This way the current return code behavior can be kept (which is useful to detect errors in the command-line).

---
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java |   48 +++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
index 90dfe29..414dc90 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
@@ -93,6 +93,9 @@
   // The print stream to use for standard output.
   private PrintStream out;
 
+  // Tells whether the command-line is being executed in script friendly mode
+  // or not.
+  private boolean isScriptFriendly;
 
 
   /**
@@ -196,9 +199,12 @@
                                      attributeType, attrValOctetStr);
 
 
-    out.println(INFO_PROCESSING_COMPARE_OPERATION.get(
-            attributeType, String.valueOf(attrValOctetStr),
-            String.valueOf(dnOctetStr)));
+    if (!isScriptFriendly())
+    {
+      out.println(INFO_PROCESSING_COMPARE_OPERATION.get(
+          attributeType, String.valueOf(attrValOctetStr),
+          String.valueOf(dnOctetStr)));
+    }
 
     if(!compareOptions.showOperations())
     {
@@ -244,12 +250,24 @@
       {
         if(resultCode == COMPARE_FALSE)
         {
-
-          out.println(INFO_COMPARE_OPERATION_RESULT_FALSE.get(line));
+          if (isScriptFriendly())
+          {
+            out.println(line+": "+COMPARE_FALSE);
+          }
+          else
+          {
+            out.println(INFO_COMPARE_OPERATION_RESULT_FALSE.get(line));
+          }
         } else if(resultCode == COMPARE_TRUE)
         {
-
-          out.println(INFO_COMPARE_OPERATION_RESULT_TRUE.get(line));
+          if (isScriptFriendly())
+          {
+            out.println(line+": "+COMPARE_TRUE);
+          }
+          else
+          {
+            out.println(INFO_COMPARE_OPERATION_RESULT_TRUE.get(line));
+          }
         } else
         {
 
@@ -362,6 +380,7 @@
     StringArgument    saslOptions            = null;
     StringArgument    trustStorePath         = null;
     StringArgument    trustStorePassword     = null;
+    BooleanArgument   scriptFriendlyArgument = null;
     StringArgument    propertiesFileArgument = null;
     BooleanArgument   noPropertiesFileArgument = null;
 
@@ -378,6 +397,15 @@
 
     try
     {
+      scriptFriendlyArgument = new BooleanArgument(
+          "script-friendly",
+          's',
+          "script-friendly",
+          INFO_DESCRIPTION_SCRIPT_FRIENDLY.get());
+      scriptFriendlyArgument.setPropertyName(
+          scriptFriendlyArgument.getLongIdentifier());
+      argParser.addInputOutputArgument(scriptFriendlyArgument);
+
       propertiesFileArgument = new StringArgument("propertiesFilePath",
           null, OPTION_LONG_PROP_FILE_PATH,
           false, false, true, INFO_PROP_FILE_PATH_PLACEHOLDER.get(), null, null,
@@ -949,6 +977,7 @@
 
 
       ldapCompare = new LDAPCompare(nextMessageID, out, err);
+      ldapCompare.isScriptFriendly = scriptFriendlyArgument.isPresent();
       if(fileNameValue == null && dnStrings.isEmpty())
       {
         // Read from stdin.
@@ -1035,5 +1064,10 @@
     return 0;
   }
 
+  private boolean isScriptFriendly()
+  {
+    return isScriptFriendly;
+  }
+
 }
 

--
Gitblit v1.10.0