From e84382e9f823070d4dcf88456015e7b228a74cd4 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 7 Jul 2016 14:58:05 +0100 Subject: [PATCH] builder, v2v: Use imperative list functions to simplify curl arg code. No functional change in this commit. --- builder/downloader.ml | 41 ++++++++++++++++++++--------------------- v2v/copy_to_local.ml | 26 +++++++++++--------------- v2v/vCenter.ml | 34 ++++++++++++++++------------------ 3 files changed, 47 insertions(+), 54 deletions(-) diff --git a/builder/downloader.ml b/builder/downloader.ml index 3c9ba1877..62a7a21d3 100644 --- a/builder/downloader.ml +++ b/builder/downloader.ml @@ -95,17 +95,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 @@ -122,19 +122,18 @@ 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 not (verbose ()) then ( + 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 2e3b59b29..9dfb37848 100644 --- a/v2v/copy_to_local.ml +++ b/v2v/copy_to_local.ml @@ -198,22 +198,18 @@ read the man page virt-v2v-copy-to-local(1). error (f_"ssh copy command failed, see earlier errors"); | ESXi _ -> - let curl_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 [ + "url", Some remote_disk; + "output", Some local_disk; + ] in + 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 ed4a9b238..f534a6d58 100644 --- a/v2v/vCenter.ml +++ b/v2v/vCenter.ml @@ -45,25 +45,23 @@ let get_session_cookie password scheme uri sslverify url = if !session_cookie <> "" then Some !session_cookie else ( - let curl_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 [ + "head", None; + "silent", None; + "url", Some url; + ] in + (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 =