[Libguestfs] [PATCH 2/2] OCaml tools: simplify machine-readable handling

Pino Toscano ptoscano at redhat.com
Mon Aug 20 15:29:53 UTC 2018


Make use of the helper provided by Tools_utils.create_standard_options,
so there is no need to implement the logic in each tool.

This affects all the OCaml tools with --machine-readable, namely:
virt-builder, virt-builder-repository, virt-dib, virt-get-kernel,
virt-resize, virt-sparsify, and virt-v2v.
---
 builder/cmdline.ml         |  8 ++------
 builder/repository_main.ml |  6 ++----
 dib/cmdline.ml             |  8 ++------
 get-kernel/get_kernel.ml   |  6 ++----
 resize/resize.ml           | 15 ++++++---------
 sparsify/cmdline.ml        |  9 ++-------
 sparsify/cmdline.mli       |  1 -
 sparsify/copying.ml        |  4 ++--
 sparsify/copying.mli       |  2 +-
 sparsify/in_place.ml       |  4 ++--
 sparsify/in_place.mli      |  2 +-
 sparsify/sparsify.ml       |  6 ++----
 v2v/cmdline.ml             |  8 ++------
 13 files changed, 26 insertions(+), 53 deletions(-)

diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index fa8a83d3e..9c854ed49 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -104,8 +104,6 @@ let parse_cmdline () =
      * Getopt handling of Symbol. *)
     list_format := List_entries.list_format_of_string arg in
 
-  let machine_readable = ref false in
-
   let memsize = ref None in
   let set_memsize arg = memsize := Some arg in
 
@@ -155,7 +153,6 @@ let parse_cmdline () =
     [ L"long" ],    Getopt.Unit list_set_long,    s_"Shortcut for --list-format long";
     [ L"list-format" ], Getopt.Symbol (formats_string, formats, list_set_format),
                                              s_"Set the format for --list (default: short)";
-    [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable";
     [ S 'm'; L"memsize" ],        Getopt.Int ("mb", set_memsize),        s_"Set memory size";
     [ L"network" ], Getopt.Set network,           s_"Enable appliance network (default)";
     [ L"no-network" ], Getopt.Clear network,      s_"Disable appliance network";
@@ -193,7 +190,7 @@ A short summary of the options is given below.  For detailed help please
 read the man page virt-builder(1).
 ")
       prog in
-  let opthandle = create_standard_options argspec ~anon_fun usage_msg in
+  let opthandle = create_standard_options argspec ~anon_fun ~machine_readable:true usage_msg in
   Getopt.parse opthandle;
 
   (* Dereference options. *)
@@ -209,7 +206,6 @@ read the man page virt-builder(1).
   let format = match !format with "" -> None | s -> Some s in
   let gpg = !gpg in
   let list_format = !list_format in
-  let machine_readable = !machine_readable in
   let memsize = !memsize in
   let network = !network in
   let ops = get_customize_ops () in
@@ -221,7 +217,7 @@ read the man page virt-builder(1).
   let warn_if_partition = !warn_if_partition in
 
   (* No arguments and machine-readable mode?  Print some facts. *)
-  if args = [] && machine_readable then (
+  if args = [] && machine_readable () then (
     printf "virt-builder\n";
     printf "arch\n";
     printf "config-file\n";
diff --git a/builder/repository_main.ml b/builder/repository_main.ml
index 5dc4d57cd..0b2b917f4 100644
--- a/builder/repository_main.ml
+++ b/builder/repository_main.ml
@@ -47,7 +47,6 @@ let parse_cmdline () =
 
   let interactive = ref false in
   let compression = ref true in
-  let machine_readable = ref false in
 
   let argspec = [
     [ L"gpg" ], Getopt.Set_string ("gpg", gpg), s_"Set GPG binary/command";
@@ -55,7 +54,6 @@ let parse_cmdline () =
       s_"ID of the GPG key to sign the repo with";
     [ S 'i'; L"interactive" ], Getopt.Set interactive, s_"Ask the user about missing data";
     [ L"no-compression" ], Getopt.Clear compression, s_"Don’t compress the new images in the index";
-    [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable";
   ] in
 
   let args = ref [] in
@@ -70,13 +68,13 @@ A short summary of the options is given below.  For detailed help please
 read the man page virt-builder-repository(1).
 ")
       prog in
-  let opthandle = create_standard_options argspec ~anon_fun usage_msg in
+  let opthandle = create_standard_options argspec ~anon_fun ~machine_readable:true usage_msg in
   Getopt.parse opthandle;
 
   (* Machine-readable mode?  Print out some facts about what
    * this binary supports.
    *)
-  if !machine_readable then (
+  if machine_readable () then (
     printf "virt-builder-repository\n";
     exit 0
   );
diff --git a/dib/cmdline.ml b/dib/cmdline.ml
index d013a181e..f5e8ec9cb 100644
--- a/dib/cmdline.ml
+++ b/dib/cmdline.ml
@@ -145,8 +145,6 @@ read the man page virt-dib(1).
   let mkfs_options = ref None in
   let set_mkfs_options arg = mkfs_options := Some arg in
 
-  let machine_readable = ref false in
-
   let extra_packages = ref [] in
   let append_extra_packages arg =
     List.push_front_list (List.rev (String.nsplit "," arg)) extra_packages in
@@ -191,14 +189,13 @@ read the man page virt-dib(1).
     [ L"smp" ],        Getopt.Int ("vcpus", set_smp),           s_"Set number of vCPUs";
     [ L"no-delete-on-failure" ], Getopt.Clear delete_on_failure,
                                                s_"Don’t delete output file on failure";
-    [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable";
 
     [ L"debug" ],      Getopt.Int ("level", set_debug),         s_"Set debug level";
     [ S 'B' ],           Getopt.Set_string ("path", basepath),   s_"Base path of diskimage-builder library";
   ] in
   let argspec = argspec @ Output_format.extra_args () in
 
-  let opthandle = create_standard_options argspec ~anon_fun:append_element usage_msg in
+  let opthandle = create_standard_options argspec ~anon_fun:append_element ~machine_readable:true usage_msg in
   Getopt.parse opthandle;
 
   let debug = !debug in
@@ -226,13 +223,12 @@ read the man page virt-dib(1).
   let is_ramdisk = !is_ramdisk in
   let ramdisk_element = !ramdisk_element in
   let mkfs_options = !mkfs_options in
-  let machine_readable = !machine_readable in
   let extra_packages = List.rev !extra_packages in
   let checksum = !checksum in
   let python = !python in
 
   (* No elements and machine-readable mode?  Print some facts. *)
-  if elements = [] && machine_readable then (
+  if elements = [] && machine_readable () then (
     printf "virt-dib\n";
     let formats_list = Output_format.list_formats () in
     List.iter (printf "output:%s\n") formats_list;
diff --git a/get-kernel/get_kernel.ml b/get-kernel/get_kernel.ml
index fca8a06db..f2949da89 100644
--- a/get-kernel/get_kernel.ml
+++ b/get-kernel/get_kernel.ml
@@ -31,7 +31,6 @@ let parse_cmdline () =
   let libvirturi = ref "" in
   let format = ref "auto" in
   let output = ref "" in
-  let machine_readable = ref false in
   let unversioned = ref false in
   let prefix = ref None in
 
@@ -57,7 +56,6 @@ let parse_cmdline () =
     [ S 'c'; L"connect" ],        Getopt.Set_string (s_"uri", libvirturi), s_"Set libvirt URI";
     [ S 'd'; L"domain" ],        Getopt.String (s_"domain", set_domain),      s_"Set libvirt guest name";
     [ L"format" ],  Getopt.Set_string (s_"format", format),      s_"Format of input disk";
-    [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable";
     [ S 'o'; L"output" ],        Getopt.Set_string (s_"directory", output),  s_"Output directory";
     [ L"unversioned-names" ], Getopt.Set unversioned,
                                             s_"Use unversioned names for files";
@@ -71,13 +69,13 @@ A short summary of the options is given below.  For detailed help please
 read the man page virt-get-kernel(1).
 ")
       prog in
-  let opthandle = create_standard_options argspec ~key_opts:true usage_msg in
+  let opthandle = create_standard_options argspec ~key_opts:true ~machine_readable:true usage_msg in
   Getopt.parse opthandle;
 
   (* Machine-readable mode?  Print out some facts about what
    * this binary supports.
    *)
-  if !machine_readable then (
+  if machine_readable () then (
     printf "virt-get-kernel\n";
     exit 0
   );
diff --git a/resize/resize.ml b/resize/resize.ml
index 174f1c699..9d2fdaf40 100644
--- a/resize/resize.ml
+++ b/resize/resize.ml
@@ -157,7 +157,7 @@ let main () =
   let infile, outfile, align_first, alignment, copy_boot_loader,
     deletes,
     dryrun, expand, expand_content, extra_partition, format, ignores,
-    lv_expands, machine_readable, ntfsresize_force, output_format,
+    lv_expands, ntfsresize_force, output_format,
     resizes, resizes_force, shrink, sparse, unknown_fs_mode =
 
     let add xs s = List.push_front s xs in
@@ -178,7 +178,6 @@ let main () =
     let format = ref "" in
     let ignores = ref [] in
     let lv_expands = ref [] in
-    let machine_readable = ref false in
     let ntfsresize_force = ref false in
     let output_format = ref "" in
     let resizes = ref [] in
@@ -204,7 +203,6 @@ let main () =
       [ L"format" ],  Getopt.Set_string (s_"format", format),     s_"Format of input disk";
       [ L"ignore" ],  Getopt.String (s_"part", add ignores),  s_"Ignore partition";
       [ L"lv-expand"; L"LV-expand"; L"lvexpand"; L"LVexpand" ], Getopt.String (s_"lv", add lv_expands), s_"Expand logical volume";
-      [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable";
       [ S 'n'; L"dry-run"; L"dryrun" ],        Getopt.Set dryrun,            s_"Don’t perform changes";
       [ L"ntfsresize-force" ], Getopt.Set ntfsresize_force, s_"Force ntfsresize";
       [ L"output-format" ], Getopt.Set_string (s_"format", output_format), s_"Format of output disk";
@@ -225,7 +223,7 @@ A short summary of the options is given below.  For detailed help please
 read the man page virt-resize(1).
 ")
         prog in
-    let opthandle = create_standard_options argspec ~anon_fun usage_msg in
+    let opthandle = create_standard_options argspec ~anon_fun ~machine_readable:true usage_msg in
     Getopt.parse opthandle;
 
     if verbose () then (
@@ -245,7 +243,6 @@ read the man page virt-resize(1).
     let format = match !format with "" -> None | str -> Some str in
     let ignores = List.rev !ignores in
     let lv_expands = List.rev !lv_expands in
-    let machine_readable = !machine_readable in
     let ntfsresize_force = !ntfsresize_force in
     let output_format = match !output_format with "" -> None | str -> Some str in
     let resizes = List.rev !resizes in
@@ -279,7 +276,7 @@ read the man page virt-resize(1).
      * things added since this option, or things which depend on features
      * of the appliance.
      *)
-    if !disks = [] && machine_readable then (
+    if !disks = [] && machine_readable () then (
       printf "virt-resize\n";
       printf "ntfsresize-force\n";
       printf "32bitok\n";
@@ -331,7 +328,7 @@ read the man page virt-resize(1).
     infile, outfile, align_first, alignment, copy_boot_loader,
     deletes,
     dryrun, expand, expand_content, extra_partition, format, ignores,
-    lv_expands, machine_readable, ntfsresize_force, output_format,
+    lv_expands, ntfsresize_force, output_format,
     resizes, resizes_force, shrink, sparse, unknown_fs_mode in
 
   (* Default to true, since NTFS/btrfs/XFS/f2fs support are usually available. *)
@@ -356,7 +353,7 @@ read the man page virt-resize(1).
     (* The output disk is being created, so use cache=unsafe here. *)
     add_drive_uri g ?format:output_format ~readonly:false ~cachemode:"unsafe"
       (snd outfile);
-    if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable g;
+    if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable:(machine_readable ()) g;
     g#launch ();
 
     (* Set the filter to /dev/sda, in case there are any rogue
@@ -1334,7 +1331,7 @@ read the man page virt-resize(1).
       (* The output disk is being created, so use cache=unsafe here. *)
       add_drive_uri g ?format:output_format ~readonly:false ~cachemode:"unsafe"
         (snd outfile);
-      if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable g;
+      if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable:(machine_readable ()) g;
       g#launch ();
 
       g (* Return new handle. *)
diff --git a/sparsify/cmdline.ml b/sparsify/cmdline.ml
index 3e21428c2..b0af053ac 100644
--- a/sparsify/cmdline.ml
+++ b/sparsify/cmdline.ml
@@ -31,7 +31,6 @@ type cmdline = {
   indisk : string;
   format : string option;
   ignores : string list;
-  machine_readable : bool;
   zeroes : string list;
   mode : mode_t;
 }
@@ -60,7 +59,6 @@ let parse_cmdline () =
   let format = ref "" in
   let ignores = ref [] in
   let in_place = ref false in
-  let machine_readable = ref false in
   let option = ref "" in
   let tmp = ref "" in
   let zeroes = ref [] in
@@ -72,7 +70,6 @@ let parse_cmdline () =
     [ L"format" ],  Getopt.Set_string (s_"format", format),     s_"Format of input disk";
     [ L"ignore" ],  Getopt.String (s_"fs", add ignores),  s_"Ignore filesystem";
     [ L"in-place"; L"inplace" ], Getopt.Set in_place,         s_"Modify the disk image in-place";
-    [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable";
     [ S 'o' ],        Getopt.Set_string (s_"option", option),     s_"Add qemu-img options";
     [ L"tmp" ],     Getopt.Set_string (s_"block|dir|prebuilt:file", tmp),        s_"Set temporary block device, directory or prebuilt file";
     [ L"zero" ],    Getopt.String (s_"fs", add zeroes),   s_"Zero filesystem";
@@ -91,7 +88,7 @@ A short summary of the options is given below.  For detailed help please
 read the man page virt-sparsify(1).
 ")
       prog in
-  let opthandle = create_standard_options argspec ~anon_fun ~key_opts:true usage_msg in
+  let opthandle = create_standard_options argspec ~anon_fun ~key_opts:true ~machine_readable:true usage_msg in
   Getopt.parse opthandle;
 
   (* Dereference the rest of the args. *)
@@ -102,7 +99,6 @@ read the man page virt-sparsify(1).
   let format = match !format with "" -> None | str -> Some str in
   let ignores = List.rev !ignores in
   let in_place = !in_place in
-  let machine_readable = !machine_readable in
   let option = match !option with "" -> None | str -> Some str in
   let tmp = match !tmp with "" -> None | str -> Some str in
   let zeroes = List.rev !zeroes in
@@ -110,7 +106,7 @@ read the man page virt-sparsify(1).
   (* No arguments and machine-readable mode?  Print out some facts
    * about what this binary supports.
    *)
-  if disks = [] && machine_readable then (
+  if disks = [] && machine_readable () then (
     printf "virt-sparsify\n";
     printf "linux-swap\n";
     printf "zero\n";
@@ -180,7 +176,6 @@ read the man page virt-sparsify(1).
   { indisk = indisk;
     format = format;
     ignores = ignores;
-    machine_readable = machine_readable;
     zeroes = zeroes;
     mode = mode;
   }
diff --git a/sparsify/cmdline.mli b/sparsify/cmdline.mli
index fabc669a6..89848df8b 100644
--- a/sparsify/cmdline.mli
+++ b/sparsify/cmdline.mli
@@ -22,7 +22,6 @@ type cmdline = {
   indisk : string;
   format : string option;
   ignores : string list;
-  machine_readable : bool;
   zeroes : string list;
   mode : mode_t;
 }
diff --git a/sparsify/copying.ml b/sparsify/copying.ml
index 8c8924241..a4bfcaa2a 100644
--- a/sparsify/copying.ml
+++ b/sparsify/copying.ml
@@ -37,7 +37,7 @@ type tmp_place =
 | Directory of string | Block_device of string | Prebuilt_file of string
 
 let run indisk outdisk check_tmpdir compress convert
-    format ignores machine_readable option tmp_param zeroes =
+    format ignores option tmp_param zeroes =
 
   (* Once we have got past argument parsing and start to create
    * temporary files (including the potentially massive overlay file), we
@@ -179,7 +179,7 @@ You can ignore this warning or change it to a hard failure using the
     (* Note that the temporary overlay disk is always qcow2 format. *)
     g#add_drive ~format:"qcow2" ~readonly:false ~cachemode:"unsafe" overlaydisk;
 
-    if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable g;
+    if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable:(machine_readable ()) g;
     g#launch ();
 
     g in
diff --git a/sparsify/copying.mli b/sparsify/copying.mli
index fae903d3e..50605cc71 100644
--- a/sparsify/copying.mli
+++ b/sparsify/copying.mli
@@ -22,4 +22,4 @@
 type tmp_place =
 | Directory of string | Block_device of string | Prebuilt_file of string
 
-val run : string -> string -> Cmdline.check_t -> bool -> string option -> string option -> string list -> bool -> string option -> string option -> string list -> unit
+val run : string -> string -> Cmdline.check_t -> bool -> string option -> string option -> string list -> string option -> string option -> string list -> unit
diff --git a/sparsify/in_place.ml b/sparsify/in_place.ml
index 5b7c950d0..7be8ee3e1 100644
--- a/sparsify/in_place.ml
+++ b/sparsify/in_place.ml
@@ -30,7 +30,7 @@ open Cmdline
 
 module G = Guestfs
 
-let run disk format ignores machine_readable zeroes =
+let run disk format ignores zeroes =
   (* Connect to libguestfs. *)
   let g = open_guestfs () in
 
@@ -49,7 +49,7 @@ let run disk format ignores machine_readable zeroes =
 
   g#add_drive ?format ~discard:"enable" disk;
 
-  if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable g;
+  if not (quiet ()) then Progress.set_up_progress_bar ~machine_readable:(machine_readable ()) g;
   g#launch ();
 
   (* If discard is not supported in the appliance, we must return exit
diff --git a/sparsify/in_place.mli b/sparsify/in_place.mli
index 42322858a..8f59ea1be 100644
--- a/sparsify/in_place.mli
+++ b/sparsify/in_place.mli
@@ -18,4 +18,4 @@
 
 (** This is the virt-sparsify --in-place mode. *)
 
-val run : string -> string option -> string list -> bool -> string list -> unit
+val run : string -> string option -> string list -> string list -> unit
diff --git a/sparsify/sparsify.ml b/sparsify/sparsify.ml
index eeda5bc98..9658b4175 100644
--- a/sparsify/sparsify.ml
+++ b/sparsify/sparsify.ml
@@ -35,11 +35,9 @@ let rec main () =
   (match cmdline.mode with
   | Mode_copying (outdisk, check_tmpdir, compress, convert, option, tmp) ->
     Copying.run cmdline.indisk outdisk check_tmpdir compress convert
-                cmdline.format cmdline.ignores cmdline.machine_readable
-                option tmp cmdline.zeroes
+                cmdline.format cmdline.ignores option tmp cmdline.zeroes
   | Mode_in_place ->
-    In_place.run cmdline.indisk cmdline.format cmdline.ignores
-                 cmdline.machine_readable cmdline.zeroes
+    In_place.run cmdline.indisk cmdline.format cmdline.ignores cmdline.zeroes
   )
 
 let () = run_main_and_handle_errors main
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
index 5b2df3555..10cbb90e6 100644
--- a/v2v/cmdline.ml
+++ b/v2v/cmdline.ml
@@ -48,7 +48,6 @@ let parse_cmdline () =
   let compressed = ref false in
   let debug_overlays = ref false in
   let do_copy = ref true in
-  let machine_readable = ref false in
   let print_source = ref false in
   let qemu_boot = ref false in
 
@@ -209,8 +208,6 @@ let parse_cmdline () =
                                     s_"Only tune the guest in the input VM";
     [ L"mac" ],      Getopt.String ("mac:network|bridge:out", add_mac),
                                     s_"Map NIC to network or bridge";
-    [ L"machine-readable" ], Getopt.Set machine_readable,
-                                    s_"Make output machine readable";
     [ S 'n'; L"network" ], Getopt.String ("in:out", add_network),
                                     s_"Map network ‘in’ to ‘out’";
     [ L"no-copy" ],  Getopt.Clear do_copy,
@@ -297,7 +294,7 @@ A short summary of the options is given below.  For detailed help please
 read the man page virt-v2v(1).
 ")
       prog in
-  let opthandle = create_standard_options argspec ~anon_fun ~key_opts:true usage_msg in
+  let opthandle = create_standard_options argspec ~anon_fun ~key_opts:true ~machine_readable:true usage_msg in
   Getopt.parse opthandle;
 
   (* Dereference the arguments. *)
@@ -318,7 +315,6 @@ read the man page virt-v2v(1).
     | Some transport ->
        error (f_"unknown input transport ‘-it %s’") transport in
   let in_place = !in_place in
-  let machine_readable = !machine_readable in
   let output_alloc =
     match !output_alloc with
     | `Not_set | `Sparse -> Sparse
@@ -337,7 +333,7 @@ read the man page virt-v2v(1).
   (* No arguments and machine-readable mode?  Print out some facts
    * about what this binary supports.
    *)
-  if args = [] && machine_readable then (
+  if args = [] && machine_readable () then (
     printf "virt-v2v\n";
     printf "libguestfs-rewrite\n";
     printf "vcenter-https\n";
-- 
2.17.1




More information about the Libguestfs mailing list