[folks] inspect: Return error codes on failure in non-interactive mode



commit 90bf4d8403494040b10ddee97f78007fa816c965
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Mon Apr 27 16:59:58 2015 +0100

    inspect: Return error codes on failure in non-interactive mode
    
    If the user runs folks-inspect and it fails (for example, because the
    individual ID they’ve specified doesn’t exist), return a non-zero exit
    status, rather than returning zero and pretending everything is perfect.
    
    Interactive mode continues to return zero.

 tools/inspect/command-backends.vala       |    6 ++++--
 tools/inspect/command-debug.vala          |    3 ++-
 tools/inspect/command-help.vala           |   13 ++++++++++---
 tools/inspect/command-individuals.vala    |    6 ++++--
 tools/inspect/command-linking.vala        |   20 ++++++++++++--------
 tools/inspect/command-persona-stores.vala |    6 ++++--
 tools/inspect/command-personas.vala       |   15 ++++++++++++++-
 tools/inspect/command-quit.vala           |    2 +-
 tools/inspect/command-search.vala         |    4 +++-
 tools/inspect/command-set.vala            |   15 ++++++++-------
 tools/inspect/command-signals.vala        |   12 +++++++-----
 tools/inspect/inspect.vala                |   23 +++++++++++++++--------
 12 files changed, 84 insertions(+), 41 deletions(-)
---
diff --git a/tools/inspect/command-backends.vala b/tools/inspect/command-backends.vala
index 5f2564e..d241890 100644
--- a/tools/inspect/command-backends.vala
+++ b/tools/inspect/command-backends.vala
@@ -49,7 +49,7 @@ private class Folks.Inspect.Commands.Backends : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       if (command_string == null)
         {
@@ -74,7 +74,7 @@ private class Folks.Inspect.Commands.Backends : Folks.Inspect.Command
             {
               Utils.print_line ("Unrecognised backend name '%s'.",
                   command_string);
-              return;
+              return 1;
             }
 
           Utils.print_line ("Backend '%s' with %u persona stores " +
@@ -90,6 +90,8 @@ private class Folks.Inspect.Commands.Backends : Folks.Inspect.Command
             }
           Utils.unindent ();
         }
+
+      return 0;
     }
 
   public override string[]? complete_subcommand (string subcommand)
diff --git a/tools/inspect/command-debug.vala b/tools/inspect/command-debug.vala
index 8cc3e6a..08946c0 100644
--- a/tools/inspect/command-debug.vala
+++ b/tools/inspect/command-debug.vala
@@ -46,9 +46,10 @@ private class Folks.Inspect.Commands.Debug : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       var debug = Folks.Debug.dup ();
       debug.emit_print_status ();
+      return 0;
     }
 }
diff --git a/tools/inspect/command-help.vala b/tools/inspect/command-help.vala
index b2e5f2c..f5e1e4f 100644
--- a/tools/inspect/command-help.vala
+++ b/tools/inspect/command-help.vala
@@ -50,7 +50,7 @@ private class Folks.Inspect.Commands.Help : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       if (command_string == null)
         {
@@ -74,10 +74,17 @@ private class Folks.Inspect.Commands.Help : Folks.Inspect.Command
           /* Help for a given command */
           Command command = this.client.commands.get (command_string);
           if (command == null)
-            Utils.print_line ("Unrecognised command '%s'.", command_string);
+            {
+              Utils.print_line ("Unrecognised command '%s'.", command_string);
+              return 1;
+            }
           else
-            Utils.print_line ("%s", command.help);
+            {
+              Utils.print_line ("%s", command.help);
+            }
         }
+
+      return 0;
     }
 
   public override string[]? complete_subcommand (string subcommand)
diff --git a/tools/inspect/command-individuals.vala b/tools/inspect/command-individuals.vala
index 019f861..e76812b 100644
--- a/tools/inspect/command-individuals.vala
+++ b/tools/inspect/command-individuals.vala
@@ -53,7 +53,7 @@ private class Folks.Inspect.Commands.Individuals : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       if (command_string == null)
         {
@@ -74,11 +74,13 @@ private class Folks.Inspect.Commands.Individuals : Folks.Inspect.Command
             {
               Utils.print_line ("Unrecognised individual ID '%s'.",
                   command_string);
-              return;
+              return 1;
             }
 
           Utils.print_individual (individual, true);
         }
+
+      return 0;
     }
 
   public override string[]? complete_subcommand (string subcommand)
diff --git a/tools/inspect/command-linking.vala b/tools/inspect/command-linking.vala
index aa04fc9..f342960 100644
--- a/tools/inspect/command-linking.vala
+++ b/tools/inspect/command-linking.vala
@@ -65,7 +65,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       string[] parts = {};
 
@@ -77,7 +77,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
 
       if (!Utils.validate_subcommand (this.name, command_string, parts[0],
               Linking._valid_subcommands))
-          return;
+          return 1;
 
       if (parts[0] == "link-personas" || parts[0] == "link-individuals")
         {
@@ -96,7 +96,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
                       "'link-individuals' subcommand.");
                 }
 
-              return;
+              return 1;
             }
 
           /* Link the personas in the given individuals. We must have at least
@@ -116,7 +116,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
                           parts[i]);
                     }
 
-                  return;
+                  return 1;
                 }
 
               var found = false;
@@ -148,7 +148,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
                     {
                       Utils.print_line ("Unrecognised persona UID '%s'.",
                           parts[i]);
-                      return;
+                      return 1;
                     }
                 }
               else
@@ -174,7 +174,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
                     {
                       Utils.print_line ("Unrecognised individual ID '%s'.",
                           parts[i]);
-                      return;
+                      return 1;
                     }
                 }
             }
@@ -189,6 +189,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
               Utils.print_line ("Error (domain: %u, code: %u) linking %u " +
                       "personas: %s",
                   e.domain, e.code, personas.size, e.message);
+              return 1;
             }
 
           /* We can't print out the individual which was produced, as
@@ -205,7 +206,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
             {
               Utils.print_line ("Must pass exactly one individual ID to an " +
                   "'unlink-individual' subcommand.");
-              return;
+              return 1;
             }
 
           var ind = this.client.aggregator.individuals.get (parts[1]);
@@ -213,7 +214,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
           if (ind == null)
             {
               Utils.print_line ("Unrecognised individual ID '%s'.", parts[1]);
-              return;
+              return 1;
             }
 
           /* Unlink the individual. */
@@ -226,6 +227,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
               Utils.print_line ("Error (domain: %u, code: %u) unlinking " +
                       "individual '%s': %s",
                   e.domain, e.code, ind.id, e.message);
+              return 1;
             }
 
           /* Success! */
@@ -236,6 +238,8 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
         {
           assert_not_reached ();
         }
+
+      return 0;
     }
 
   /* FIXME: These can't be in the subcommand_name_completion_cb() function
diff --git a/tools/inspect/command-persona-stores.vala b/tools/inspect/command-persona-stores.vala
index c8b28a8..c310be4 100644
--- a/tools/inspect/command-persona-stores.vala
+++ b/tools/inspect/command-persona-stores.vala
@@ -53,7 +53,7 @@ private class Folks.Inspect.Commands.PersonaStores : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       if (command_string == null)
         {
@@ -91,11 +91,13 @@ private class Folks.Inspect.Commands.PersonaStores : Folks.Inspect.Command
             {
               Utils.print_line ("Unrecognised persona store ID '%s'.",
                   command_string);
-              return;
+              return 1;
             }
 
           Utils.print_persona_store (store, true);
         }
+
+      return 0;
     }
 
   public override string[]? complete_subcommand (string subcommand)
diff --git a/tools/inspect/command-personas.vala b/tools/inspect/command-personas.vala
index 121ace6..9c28582 100644
--- a/tools/inspect/command-personas.vala
+++ b/tools/inspect/command-personas.vala
@@ -52,8 +52,10 @@ private class Folks.Inspect.Commands.Personas : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
+      bool found_persona = false;
+
       foreach (var individual in this.client.aggregator.individuals.values)
         {
           foreach (Persona persona in individual.personas)
@@ -66,8 +68,19 @@ private class Folks.Inspect.Commands.Personas : Folks.Inspect.Command
 
               if (command_string == null)
                 Utils.print_line ("");
+              else
+                found_persona = true;
             }
         }
+
+      /* Return an error if the persona wasn’t found. */
+      if (!found_persona && command_string != null)
+        {
+          Utils.print_line ("Unrecognised persona UID '%s'.", command_string);
+          return 1;
+        }
+
+      return 0;
     }
 
   public override string[]? complete_subcommand (string subcommand)
diff --git a/tools/inspect/command-quit.vala b/tools/inspect/command-quit.vala
index 4b5eb62..4a92ff6 100644
--- a/tools/inspect/command-quit.vala
+++ b/tools/inspect/command-quit.vala
@@ -50,7 +50,7 @@ private class Folks.Inspect.Commands.Quit : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       Process.exit (0);
     }
diff --git a/tools/inspect/command-search.vala b/tools/inspect/command-search.vala
index a4c2612..4c8d644 100644
--- a/tools/inspect/command-search.vala
+++ b/tools/inspect/command-search.vala
@@ -51,7 +51,7 @@ private class Folks.Inspect.Commands.Search : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       if (command_string == null)
         {
@@ -79,5 +79,7 @@ private class Folks.Inspect.Commands.Search : Folks.Inspect.Command
                   individual.display_name);
             }
         }
+
+      return 0;
     }
 }
diff --git a/tools/inspect/command-set.vala b/tools/inspect/command-set.vala
index e350235..76bb773 100644
--- a/tools/inspect/command-set.vala
+++ b/tools/inspect/command-set.vala
@@ -56,7 +56,7 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       string[] parts = {};
 
@@ -68,7 +68,7 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
 
       if (!Utils.validate_subcommand (this.name, command_string, parts[0],
               Set._valid_subcommands))
-          return;
+          return 1;
 
       if (parts[0] == "alias")
         {
@@ -77,7 +77,7 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
               Utils.print_line ("Must pass at least one individual ID and a new alias to an " +
                   "'alias' subcommand.");
 
-              return;
+              return 1;
             }
 
           /* To set an attribute on an individual, we must have at least one. */
@@ -86,7 +86,7 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
               Utils.print_line ("Unrecognised individual ID '%s'.",
                   parts[1]);
 
-              return;
+              return 1;
             }
 
           var id = parts[1].strip ();
@@ -95,7 +95,7 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
           if (individual == null)
             {
               Utils.print_line ("Unrecognized individual ID '%s'.", id);
-              return;
+              return 1;
             }
             
           try
@@ -111,14 +111,15 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
             {
               Utils.print_line ("Setting of individual's alias to '%s' failed.",
                   parts[2]);
+              return 1;
             }
-
-          return;
         }
       else
         {
           assert_not_reached ();
         }
+
+      return 0;
     }
 
   /* FIXME: These can't be in the subcommand_name_completion_cb() function
diff --git a/tools/inspect/command-signals.vala b/tools/inspect/command-signals.vala
index ead4584..2bb54ef 100644
--- a/tools/inspect/command-signals.vala
+++ b/tools/inspect/command-signals.vala
@@ -100,7 +100,7 @@ private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
       base (client);
     }
 
-  public override async void run (string? command_string)
+  public override async int run (string? command_string)
     {
       if (command_string == null)
         {
@@ -114,7 +114,7 @@ private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
 
           if (!Utils.validate_subcommand (this.name, command_string, parts[0],
                  Signals._valid_subcommands))
-              return;
+              return 1;
 
           Type class_type;
           Object class_instance;
@@ -128,14 +128,14 @@ private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
                 {
                   Utils.print_line ("Unrecognised signal identifier '%s'.",
                       parts[1]);
-                  return;
+                  return 1;
                 }
 
               if (this.parse_signal_id (parts[1].strip (), out class_type,
                   out class_instance, out signal_name,
                   out detail_string) == false)
                 {
-                  return;
+                  return 1;
                 }
 
               /* FIXME: Handle "disconnect <signal ID>" */
@@ -164,7 +164,7 @@ private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
                   out class_instance, out signal_name,
                   out detail_string) == false)
                 {
-                  return;
+                  return 1;
                 }
 
               if (signal_name == null)
@@ -183,6 +183,8 @@ private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
                 }
             }
         }
+
+      return 0;
     }
 
   public override string[]? complete_subcommand (string subcommand)
diff --git a/tools/inspect/inspect.vala b/tools/inspect/inspect.vala
index 5f4af1b..4400127 100644
--- a/tools/inspect/inspect.vala
+++ b/tools/inspect/inspect.vala
@@ -51,6 +51,8 @@ public class Folks.Inspect.Client : Object
 
   public static int main (string[] args)
     {
+      int retval = 0;
+
       Intl.setlocale (LocaleCategory.ALL, "");
       Intl.bindtextdomain (BuildConf.GETTEXT_PACKAGE, BuildConf.LOCALE_DIR);
       Intl.textdomain (BuildConf.GETTEXT_PACKAGE);
@@ -95,6 +97,7 @@ public class Folks.Inspect.Client : Object
       if (args.length == 1)
         {
           main_client.run_interactive.begin ();
+          retval = 0;
         }
       else
         {
@@ -115,14 +118,14 @@ public class Folks.Inspect.Client : Object
 
           main_client.run_non_interactive.begin (command_line, (obj, res) =>
           {
-            main_client.run_non_interactive.end (res);
+            retval = main_client.run_non_interactive.end (res);
             main_client.quit ();
           });
         }
         
       main_client.main_loop.run ();
 
-      return 0;
+      return retval;
     }
 
   public Client ()
@@ -204,7 +207,7 @@ public class Folks.Inspect.Client : Object
         }
     }
 
-  public async void run_non_interactive (string command_line)
+  public async int run_non_interactive (string command_line)
     {
       /* Non-interactive mode: run a single command and output the results.
        * We do this all from the main thread, in a main loop, waiting for
@@ -219,7 +222,7 @@ public class Folks.Inspect.Client : Object
       if (command == null)
         {
           GLib.stdout.printf ("Unrecognised command ‘%s’.\n", command_name);
-          return;
+          return 1;
         }
 
       /* Wait until we reach quiescence, or the results will probably be
@@ -231,15 +234,17 @@ public class Folks.Inspect.Client : Object
       catch (GLib.Error e1)
         {
           GLib.stderr.printf ("Error preparing aggregator: %s\n", e1.message);
-          Process.exit (1);
+          return 1;
         }
 
       /* Run the command */
-      yield command.run (subcommand);
+      int retval = yield command.run (subcommand);
       this.quit ();
+
+      return retval;
     }
 
-  public async void run_interactive ()
+  public async int run_interactive ()
     {
       /* Interactive mode: have a little shell which allows the data from
        * libfolks to be browsed and edited in real time. We do this by watching
@@ -288,6 +293,8 @@ public class Folks.Inspect.Client : Object
 
       /* Run the aggregator and the main loop. */
       this.aggregator.prepare.begin ();
+
+      return 0;
     }
 
   private void _install_readline_and_stdin ()
@@ -591,7 +598,7 @@ public abstract class Folks.Inspect.Command
   public abstract string description { get; }
   public abstract string help { get; }
 
-  public abstract async void run (string? command_string);
+  public abstract async int run (string? command_string);
 
   public virtual string[]? complete_subcommand (string subcommand)
     {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]