[Libguestfs] [PATCH 1/3] builder: small refactor of the list output

Pino Toscano ptoscano at redhat.com
Thu Jan 16 14:04:30 UTC 2014


Switch from a boolean for the short/long list output to labels for the
actual format. Also, split the output of each list format to an own
function for easier maintaineance.
---
 builder/builder.ml       |  4 +--
 builder/cmdline.ml       |  9 +++---
 builder/list_entries.ml  | 82 +++++++++++++++++++++++++++---------------------
 builder/list_entries.mli |  2 +-
 4 files changed, 54 insertions(+), 43 deletions(-)

diff --git a/builder/builder.ml b/builder/builder.ml
index 861e029..bb0b108 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -37,7 +37,7 @@ let main () =
   (* Command line argument parsing - see cmdline.ml. *)
   let mode, arg,
     attach, cache, check_signature, curl, debug, delete, delete_on_failure,
-    edit, firstboot, run, format, gpg, hostname, install, list_long, links,
+    edit, firstboot, run, format, gpg, hostname, install, list_format, links,
     memsize, mkdirs,
     network, output, password_crypto, quiet, root_password, scrub,
     scrub_logfile, size, smp, sources, sync, timezone, update, upload,
@@ -148,7 +148,7 @@ let main () =
   let mode =
     match mode with
     | `List ->                          (* --list *)
-      List_entries.list_entries ~list_long ~sources index;
+      List_entries.list_entries ~list_format ~sources index;
       exit 0
 
     | `Print_cache ->                   (* --print-cache *)
diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index 67b142a..f199f03 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -130,7 +130,8 @@ let parse_cmdline () =
     links := (target, lns) :: !links
   in
 
-  let list_long = ref false in
+  let list_format = ref `Short in
+  let list_set_long () = list_format := `Long in
 
   let memsize = ref None in
   let set_memsize arg = memsize := Some arg in
@@ -255,7 +256,7 @@ let parse_cmdline () =
     "--link",    Arg.String add_link,       "target:link.." ^ " " ^ s_"Create symbolic links";
     "-l",        Arg.Unit list_mode,        " " ^ s_"List available templates";
     "--list",    Arg.Unit list_mode,        ditto;
-    "--long",    Arg.Set list_long,         ditto;
+    "--long",    Arg.Unit list_set_long,    " " ^ s_"List available templates, in long textual form";
     "--no-logfile", Arg.Set scrub_logfile,  " " ^ s_"Scrub build log file";
     "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options";
     "-m",        Arg.Int set_memsize,       "mb" ^ " " ^ s_"Set memory size";
@@ -329,7 +330,7 @@ read the man page virt-builder(1).
   let gpg = !gpg in
   let hostname = !hostname in
   let install = List.rev !install in
-  let list_long = !list_long in
+  let list_format = !list_format in
   let links = List.rev !links in
   let memsize = !memsize in
   let mkdirs = List.rev !mkdirs in
@@ -443,7 +444,7 @@ read the man page virt-builder(1).
 
   mode, arg,
   attach, cache, check_signature, curl, debug, delete, delete_on_failure,
-  edit, firstboot, run, format, gpg, hostname, install, list_long, links,
+  edit, firstboot, run, format, gpg, hostname, install, list_format, links,
   memsize, mkdirs,
   network, output, password_crypto, quiet, root_password, scrub,
   scrub_logfile, size, smp, sources, sync, timezone, update, upload,
diff --git a/builder/list_entries.ml b/builder/list_entries.ml
index 87001c0..97ab201 100644
--- a/builder/list_entries.ml
+++ b/builder/list_entries.ml
@@ -21,15 +21,35 @@ open Common_utils
 
 open Printf
 
-let list_entries ?(list_long = false) ~sources index =
-  if list_long then (
-    List.iter (
-      fun (source, fingerprint) ->
-        printf (f_"Source URI: %s\n") source;
-        printf (f_"Fingerprint: %s\n") fingerprint;
+let rec list_entries ~list_format ~sources index =
+  match list_format with
+  | `Short -> list_entries_short index
+  | `Long -> list_entries_long ~sources index
+
+and list_entries_short index =
+  List.iter (
+    fun (name, { Index_parser.printable_name = printable_name;
+                 size = size;
+                 compressed_size = compressed_size;
+                 notes = notes;
+                 hidden = hidden }) ->
+      if not hidden then (
+        printf "%-24s" name;
+        (match printable_name with
+        | None -> ()
+        | Some s -> printf " %s" s
+        );
         printf "\n"
-    ) sources
-  );
+      )
+  ) index
+
+and list_entries_long ~sources index =
+  List.iter (
+    fun (source, fingerprint) ->
+      printf (f_"Source URI: %s\n") source;
+      printf (f_"Fingerprint: %s\n") fingerprint;
+      printf "\n"
+  ) sources;
 
   List.iter (
     fun (name, { Index_parser.printable_name = printable_name;
@@ -38,33 +58,23 @@ let list_entries ?(list_long = false) ~sources index =
                  notes = notes;
                  hidden = hidden }) ->
       if not hidden then (
-        if not list_long then (         (* Short *)
-          printf "%-24s" name;
-          (match printable_name with
-          | None -> ()
-          | Some s -> printf " %s" s
-          );
-          printf "\n"
-        )
-        else (                          (* Long *)
-          printf "%-24s %s\n" "os-version:" name;
-          (match printable_name with
-          | None -> ()
-          | Some name -> printf "%-24s %s\n" (s_"Full name:") name;
-          );
-          printf "%-24s %s\n" (s_"Minimum/default size:") (human_size size);
-          (match compressed_size with
-          | None -> ()
-          | Some size ->
-            printf "%-24s %s\n" (s_"Download size:") (human_size size);
-          );
-          (match notes with
-          | None -> ()
-          | Some notes ->
-            printf "\n";
-            printf (f_"Notes:\n\n%s\n") notes
-          );
-          printf "\n"
-        )
+        printf "%-24s %s\n" "os-version:" name;
+        (match printable_name with
+        | None -> ()
+        | Some name -> printf "%-24s %s\n" (s_"Full name:") name;
+        );
+        printf "%-24s %s\n" (s_"Minimum/default size:") (human_size size);
+        (match compressed_size with
+        | None -> ()
+        | Some size ->
+          printf "%-24s %s\n" (s_"Download size:") (human_size size);
+        );
+        (match notes with
+        | None -> ()
+        | Some notes ->
+          printf "\n";
+          printf (f_"Notes:\n\n%s\n") notes
+        );
+        printf "\n"
       )
   ) index
diff --git a/builder/list_entries.mli b/builder/list_entries.mli
index d9486b0..41d0bff 100644
--- a/builder/list_entries.mli
+++ b/builder/list_entries.mli
@@ -16,4 +16,4 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)
 
-val list_entries : ?list_long:bool -> sources:(string * string) list -> Index_parser.index -> unit
+val list_entries : list_format:([ `Short | `Long ]) -> sources:(string * string) list -> Index_parser.index -> unit
-- 
1.8.3.1




More information about the Libguestfs mailing list