Refactor common code dealing with [host:]name.
[mclu.git] / mclu_list.ml
index 3534066..f685f54 100644 (file)
@@ -74,6 +74,31 @@ let active_guests ?(verbose = false) ?(nodes = Mclu_conf.nodes ()) () =
     ) nodes in
   List.map (fun s -> Marshal.from_bytes s 0) active_guests
 
+let find_guest ?verbose ?(nodes = Mclu_conf.nodes ()) name =
+  let host, name = name_parse name in
+  let node =
+    match host with
+    | Some host ->
+      (try List.find (fun n -> host = n.Mclu_conf.hostname) nodes
+       with Not_found ->
+         eprintf "mclu: host '%s' not found\n" host;
+         exit 1)
+    | None ->
+      (* No 'host:' prefix given, so we need to find the host. *)
+      let guests = active_guests ?verbose ~nodes () in
+      let node, _ =
+        try
+          List.find (
+            fun (node, doms) ->
+              List.exists (fun dom -> name = dom.dom_name) doms
+          ) guests
+        with
+          Not_found ->
+            eprintf "mclu: guest '%s' not found\n" name;
+            exit 1 in
+      node in
+  node, name
+
 let list ~verbose () =
   let list_what = !list_what in