gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] branch master updated: rework anastasis-cli-redux to s


From: gnunet
Subject: [taler-anastasis] branch master updated: rework anastasis-cli-redux to support redirections
Date: Sat, 17 Oct 2020 17:03:49 +0200

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new fe988cb  rework anastasis-cli-redux to support redirections
fe988cb is described below

commit fe988cbe750250b3164468e3a36a36be5adce5c1
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Sat Oct 17 17:02:55 2020 +0200

    rework anastasis-cli-redux to support redirections
---
 src/cli/anastasis-cli-redux.c | 300 +++++++++++++++++++++++++++++-------------
 1 file changed, 209 insertions(+), 91 deletions(-)

diff --git a/src/cli/anastasis-cli-redux.c b/src/cli/anastasis-cli-redux.c
index 869936f..57a76e4 100644
--- a/src/cli/anastasis-cli-redux.c
+++ b/src/cli/anastasis-cli-redux.c
@@ -49,9 +49,9 @@ static int b_flag;
 static int r_flag;
 
 /**
- * -i option given.
+ * -a option given.
  */
-static int i_flag;
+static int a_flag;
 
 /**
  * JSON containing previous state
@@ -78,11 +78,16 @@ static json_t *arguments;
  */
 static char *action;
 
-
+/**
+ * Persist a json state.
+ * 
+ * @param state to persist
+ * @return 0 if operation failed
+ */
 static int
-persist_new_state ()
+persist_new_state (json_t *state)
 {
-  return json_dump_file (new_state, path_new_state, JSON_COMPACT);
+  return json_dump_file (state, path_new_state, JSON_COMPACT);
 }
 
 
@@ -100,10 +105,23 @@ action_cb (void *cls,
            json_t *result_state)
 {
   new_state = result_state;
+  
   if (NULL != new_state)
-    if (0 != persist_new_state ())
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Failed to persist new state! \n");
+  {
+    if (! isatty (STDOUT_FILENO))
+    {
+      fprintf (stdout, 
+              "%s",
+              json_dumps (new_state,
+                          JSON_COMPACT));
+    }
+    else
+    {
+      if (0 != persist_new_state (new_state))
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    "Failed to persist new state! \n");
+    }
+  }
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -162,117 +180,217 @@ run (void *cls,
                  GNUNET_log_setup ("anastasis-reducer",
                                    "WARNING",
                                    NULL));
-
-  if (! (r_flag ^ b_flag))
+  
+  
+  if (r_flag ^ b_flag)
   {
-    printf ("Please set one option: -b, -r, -i!\n");
-    printf (
-      "Example: anastasis-reducer -b prev.json action arguments.json 
new.json\n");
-    printf ("Example: anastasis-reducer -b -i init.json\n");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (i_flag)
-  {
-    if (args[0])
+    json_t *init_state;
+    if (b_flag)
     {
-      path_new_state = (char *) args[0];
-      if (b_flag)
-        new_state = ANASTASIS_backup_start (cfg);
-      else if (r_flag)
-        new_state = ANASTASIS_recovery_start (cfg);
-      if (! new_state)
+      init_state = ANASTASIS_backup_start (cfg);
+      if (NULL == init_state)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "At %s:%u failed to get an initial state!\n",
+                    "At %s:%u failed to get an initial backup state!\n",
                     __FILE__,
                     __LINE__);
         GNUNET_SCHEDULER_shutdown ();
         return;
       }
-      if (0 != persist_new_state ())
+    }
+    else
+    {
+      init_state = ANASTASIS_recovery_start (cfg);
+      if (NULL == init_state)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "At %s:%u failed to dump json into file!\n",
+                    "At %s:%u failed to get an initial recovery state!\n",
                     __FILE__,
                     __LINE__);
         GNUNET_SCHEDULER_shutdown ();
         return;
       }
-      return; // success!
+    }
+    if (isatty (STDOUT_FILENO))
+    {
+      if (args[0])
+      {
+        path_new_state = (char *) args[0];
+        if (0 != persist_new_state (init_state))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                      "At %s:%u failed to dump json into file!\n",
+                      __FILE__,
+                      __LINE__);
+          GNUNET_SCHEDULER_shutdown ();
+          return;
+        }
+        return; // success!
+      }
+      else
+      {
+        fprintf (stderr,
+          "Please give a path to store initial json state!\n");
+        fprintf (stderr,
+          "Example: anastasis-reducer -b init_state.json\n");
+        GNUNET_SCHEDULER_shutdown ();
+        return;
+      }
     }
     else
     {
-      printf ("Please give path for json containing initial state!\n");
-      printf ("Example: anastasis-reducer -b -i init.json\n");
-      GNUNET_SCHEDULER_shutdown ();
-      return;
+      fprintf (stdout,
+                "%s",
+                json_dumps (init_state,
+                            JSON_COMPACT));
+      return; // success!
     }
   }
-
-  if ((r_flag ^ b_flag))
+  else
   {
-    if (args[0])
-      prev_state = json_load_file ((char *) args[0], JSON_DECODE_ANY, &error);
-    else
+    // check if we have redirection to this reducer
+    if (! isatty (STDIN_FILENO))
     {
-      printf ("Please give path for json containing previous state!\n");
-      printf (
-        "Example: anastasis-reducer -b prev.json action arguments.json 
new.json\n");
-      GNUNET_SCHEDULER_shutdown ();
-      return;
+      if (a_flag)
+      {
+        if (NULL == args[0])
+        {
+          fprintf (stderr,
+                   "Please give arguments!\n");
+          fprintf (stderr,
+                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
< prev_state.json > new_state.json\n");
+          GNUNET_SCHEDULER_shutdown ();
+          return;
+        }
+        arguments = json_loads (args[0],
+                                JSON_DECODE_ANY,
+                                &error);
+        if (NULL == arguments)
+        {
+          fprintf (stderr,
+                   "Failed to parse arguments!\n");
+          fprintf (stderr,
+                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
+          GNUNET_SCHEDULER_shutdown ();
+          return;
+        }
+        if (NULL == args[1])
+        {
+          fprintf (stderr,
+                   "Failed to parse arguments!\n");
+          fprintf (stderr,
+                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
+          GNUNET_SCHEDULER_shutdown ();
+          return;
+        }
+        action = (char *) args[1];
+        if (NULL == action)
+        {
+          fprintf (stderr,
+                   "Failed to parse arguments!\n");
+          fprintf (stderr,
+                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
+          GNUNET_SCHEDULER_shutdown ();
+          return;
+        }
+        char *prev_state_str = NULL; 
+        size_t size;
+        if (getline (&prev_state_str,
+                     &size,
+                     stdin)
+            == EOF)
+        {
+          fprintf (stderr,
+                   "No redirection given!\n");
+        }
+        prev_state = json_loads (prev_state_str,
+                                 JSON_DECODE_ANY,
+                                 &error);
+        if (NULL == prev_state)
+        {
+          fprintf (stderr,
+                   "Failed to load previous state!\n");
+          fprintf (stderr,
+                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
+          GNUNET_SCHEDULER_shutdown ();
+          return;
+        }
+      }
+      else
+      {
+        fprintf (stderr,
+                   "Please give arguments!\n");
+        fprintf (stderr,
+                  "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
+        GNUNET_SCHEDULER_shutdown ();
+        return;
+      }
     }
-
-    if (args[1])
-      action = (char *) args[1];
     else
     {
-      printf ("Please give an action!\n");
-      printf (
-        "Example: anastasis-reducer -b prev.json action arguments.json 
new.json\n");
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    }
+      if (args[0])
+        prev_state = json_load_file ((char *) args[0], JSON_DECODE_ANY, 
&error);
+      else
+      {
+        fprintf (stderr,
+          "Please give path for json containing previous state!\n");
+        fprintf (stderr,
+          "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
+        GNUNET_SCHEDULER_shutdown ();
+        return;
+      }
 
-    if (args[2])
-      arguments = json_load_file ((char *) args[2], JSON_DECODE_ANY, &error);
-    else
-    {
-      printf ("Please give path for json containing arguments for action!\n");
-      printf (
-        "Example: anastasis-reducer -b prev.json action arguments.json 
new.json\n");
-      GNUNET_SCHEDULER_shutdown ();
-      return;
-    }
+      if (args[1])
+        action = (char *) args[1];
+      else
+      {
+        fprintf (stderr,
+          "Please give an action!\n");
+        fprintf (stderr,
+          "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
+        GNUNET_SCHEDULER_shutdown ();
+        return;
+      }
 
-    if (args[3])
-      path_new_state = (char *) args[3];
-    else
-    {
-      printf ("Please give path for json containing new state!\n");
-      printf (
-        "Example: anastasis-reducer -b prev.json action arguments.json 
new.json\n");
-      GNUNET_SCHEDULER_shutdown ();
-      return;
+      if (args[2])
+        arguments = json_load_file ((char *) args[2], JSON_DECODE_ANY, &error);
+      else
+      {
+        fprintf (stderr,
+          "Please give path for json containing arguments for action!\n");
+        fprintf (stderr,
+          "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
+        GNUNET_SCHEDULER_shutdown ();
+        return;
+      }
+
+      if (args[3])
+        path_new_state = (char *) args[3];
+      else
+      {
+        fprintf (stderr,
+          "Please give path for json containing new state!\n");
+        fprintf (stderr,
+          "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
+        GNUNET_SCHEDULER_shutdown ();
+        return;
+      }
     }
+    /* initialize HTTP client FIXME: Do we need http client?*/
+    ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
+                            &rc);
+    rc = GNUNET_CURL_gnunet_rc_create (ctx);
+
+    // FIXME: return value in global, shutdown handler to clean up, yada yada
+    ANASTASIS_redux_action (prev_state,
+                            action,
+                            arguments,
+                            ctx,
+                            &action_cb,
+                            cls);
   }
-
-  /* initialize HTTP client FIXME: Do we need http client?*/
-  ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
-                          &rc);
-  rc = GNUNET_CURL_gnunet_rc_create (ctx);
-
-  // FIXME: return value in global, shutdown handler to clean up, yada yada
-  ANASTASIS_redux_action (prev_state,
-                          action,
-                          arguments,
-                          ctx,
-                          &action_cb,
-                          cls);
 }
 
-
 int
 main (int argc,
       char *const *argv)
@@ -289,10 +407,10 @@ main (int argc,
                                "restore",
                                "use reducer to handle states for restore 
process",
                                &r_flag),
-    GNUNET_GETOPT_option_flag ('i',
-                               "init",
-                               "returns an initial state to start with",
-                               &i_flag),
+    GNUNET_GETOPT_option_flag ('a',
+                               "arguments",
+                               "pass a json string containing arguments to 
reducer",
+                               &a_flag),
 
     GNUNET_GETOPT_OPTION_END
   };

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]