[Libguestfs] [PATCH v2 5/8] builder, v2v: Use imperative list functions to simplify curl arg code.

Richard W.M. Jones rjones at redhat.com
Thu Jul 7 15:22:08 UTC 2016


No functional change in this commit.
---
 builder/downloader.ml | 40 +++++++++++++++++++---------------------
 v2v/copy_to_local.ml  | 28 ++++++++++++----------------
 v2v/vCenter.ml        | 36 +++++++++++++++++-------------------
 3 files changed, 48 insertions(+), 56 deletions(-)

diff --git a/builder/downloader.ml b/builder/downloader.ml
index 382981c..1c7b086 100644
--- a/builder/downloader.ml
+++ b/builder/downloader.ml
@@ -101,17 +101,17 @@ and download_to t ?(progress_bar = false) ?proxy uri filename =
 
     (* Get the status code first to ensure the file exists. *)
     let curl_h =
-      let curl_args =
-        common_args @
-        (if verbose () then [] else quiet_args) @ [
-          "output", Some "/dev/null"; (* Write output to /dev/null. *)
-          "head", None;               (* Request only HEAD. *)
-          (* Write HTTP status code to stdout. *)
-          "write-out", Some "%{http_code}";
-          "url", Some uri
-        ] in
+      let curl_args = ref common_args in
+      if not (verbose ()) then append curl_args quiet_args;
+      append curl_args [
+        "output", Some "/dev/null"; (* Write output to /dev/null. *)
+        "head", None;               (* Request only HEAD. *)
+        (* Write HTTP status code to stdout. *)
+        "write-out", Some "%{http_code}";
+        "url", Some uri
+      ];
 
-      Curl.create ~curl:t.curl curl_args in
+      Curl.create ~curl:t.curl !curl_args in
 
     let lines = Curl.run curl_h in
     if List.length lines < 1 then
@@ -128,19 +128,17 @@ and download_to t ?(progress_bar = false) ?proxy uri filename =
 
     (* Now download the file. *)
     let curl_h =
-      let curl_args =
-        common_args @ [
-          "output", Some filename_new;
-          "url", Some uri
-        ] in
+      let curl_args = ref common_args in
+      append curl_args [
+        "output", Some filename_new;
+        "url", Some uri
+      ];
 
-      let curl_args =
-        curl_args @
-          if verbose () then []
-          else if progress_bar then [ "progress-bar", None ]
-          else quiet_args in
+      if verbose () then ()
+      else if progress_bar then push curl_args ("progress-bar", None)
+      else append curl_args quiet_args;
 
-      Curl.create ~curl:t.curl curl_args in
+      Curl.create ~curl:t.curl !curl_args in
 
     ignore (Curl.run curl_h)
   );
diff --git a/v2v/copy_to_local.ml b/v2v/copy_to_local.ml
index d791293..24ed03f 100644
--- a/v2v/copy_to_local.ml
+++ b/v2v/copy_to_local.ml
@@ -198,23 +198,19 @@ read the man page virt-v2v-copy-to-local(1).
          error (f_"ssh copy command failed, see earlier errors");
 
     | ESXi _ ->
-       let curl_args =
-         Curl.safe_args @ [
-           "url", Some remote_disk;
-           "output", Some local_disk;
-         ] in
-       let curl_args =
-         if sslverify then curl_args
-         else ("insecure", None) :: curl_args in
-       let curl_args =
-         match cookie with
-         | None -> curl_args
-         | Some cookie -> ("cookie", Some cookie) :: curl_args in
-       let curl_args =
-         if quiet () then ("silent", None) :: curl_args
-         else curl_args in
+       let curl_args = ref Curl.safe_args in
+       append curl_args [
+         "url", Some remote_disk;
+         "output", Some local_disk;
+       ];
+       if not sslverify then push curl_args ("insecure", None);
+       (match cookie with
+        | None -> ()
+        | Some cookie -> push curl_args ("cookie", Some cookie)
+       );
+       if quiet () then push curl_args ("silent", None);
 
-       let curl_h = Curl.create curl_args in
+       let curl_h = Curl.create !curl_args in
        if verbose () then
          Curl.print stderr curl_h;
        ignore (Curl.run curl_h)
diff --git a/v2v/vCenter.ml b/v2v/vCenter.ml
index dbfdf1a..0fa3967 100644
--- a/v2v/vCenter.ml
+++ b/v2v/vCenter.ml
@@ -45,26 +45,24 @@ let get_session_cookie password scheme uri sslverify url =
   if !session_cookie <> "" then
     Some !session_cookie
   else (
-    let curl_args =
-      Curl.safe_args @ [
-        "head", None;
-        "silent", None;
-        "url", Some url;
-      ] in
-    let curl_args =
-      match uri.uri_user, password with
-      | None, None -> curl_args
-      | None, Some _ ->
-         warning (f_"--password-file parameter ignored because 'user@' was not given in the URL");
-         curl_args
-      | Some user, None ->
-         ("user", Some user) :: curl_args
-      | Some user, Some password ->
-         ("user", Some (user ^ ":" ^ password)) :: curl_args in
-    let curl_args =
-      if not sslverify then ("insecure", None) :: curl_args else curl_args in
+    let curl_args = ref Curl.safe_args in
+    append curl_args [
+      "head", None;
+      "silent", None;
+      "url", Some url;
+    ];
+    (match uri.uri_user, password with
+     | None, None -> ()
+     | None, Some _ ->
+        warning (f_"--password-file parameter ignored because 'user@' was not given in the URL")
+     | Some user, None ->
+        push curl_args ("user", Some user)
+     | Some user, Some password ->
+        push curl_args ("user", Some (user ^ ":" ^ password))
+    );
+    if not sslverify then push curl_args ("insecure", None);
 
-    let curl_h = Curl.create curl_args in
+    let curl_h = Curl.create !curl_args in
     let lines = Curl.run curl_h in
 
     let dump_response chan =
-- 
2.7.4




More information about the Libguestfs mailing list