mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
OCaml tools: simplify machine-readable handling
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.
This commit is contained in:
committed by
Richard W.M. Jones
parent
b8f0740b51
commit
2fa78e6dce
@@ -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";
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
@@ -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. *)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ type cmdline = {
|
||||
indisk : string;
|
||||
format : string option;
|
||||
ignores : string list;
|
||||
machine_readable : bool;
|
||||
zeroes : string list;
|
||||
mode : mode_t;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user