Implement 'whenjobs --job-names' which lists all loaded jobs.
[whenjobs.git] / tools / whenjobs.ml
index ff166a6..582f259 100644 (file)
@@ -71,10 +71,23 @@ let jobsdir =
 let rec main () =
   (* Parse the command line arguments. *)
   let mode = ref None in
 let rec main () =
   (* Parse the command line arguments. *)
   let mode = ref None in
-  let typ = ref "string" in
+  let typ = ref `String in
 
   let set_mode m () = mode := Some m in
 
 
   let set_mode m () = mode := Some m in
 
+  let set_type t =
+    typ :=
+      match t with
+      | "bool"|"boolean" -> `Bool
+      | "string" -> `String
+      | "int" -> `Int
+      | "float"|"double" -> `Float
+      | "unit" -> `Unit
+      | _ ->
+        eprintf "whenjobs: --type: unknown type (%s)\n" t;
+        exit 1
+  in
+
   let display_version () =
     printf "%s %s\n" Config.package_name Config.package_version;
     exit 0
   let display_version () =
     printf "%s %s\n" Config.package_name Config.package_version;
     exit 0
@@ -89,6 +102,7 @@ let rec main () =
     "-e", Arg.Unit (set_mode `Edit), " Edit and upload the script";
     "--edit", Arg.Unit (set_mode `Edit), " Edit and upload the script";
     "--get", Arg.Unit (set_mode `Get), "var Display the variable";
     "-e", Arg.Unit (set_mode `Edit), " Edit and upload the script";
     "--edit", Arg.Unit (set_mode `Edit), " Edit and upload the script";
     "--get", Arg.Unit (set_mode `Get), "var Display the variable";
+    "--job-names", Arg.Unit (set_mode `JobNames), " List names of loaded jobs";
     "--jobs", Arg.Unit (set_mode `Jobs), " List running jobs";
     "-l", Arg.Unit (set_mode `List), " List the script";
     "--list", Arg.Unit (set_mode `List), " List the script";
     "--jobs", Arg.Unit (set_mode `Jobs), " List running jobs";
     "-l", Arg.Unit (set_mode `List), " List the script";
     "--list", Arg.Unit (set_mode `List), " List the script";
@@ -96,15 +110,20 @@ let rec main () =
     "--set", Arg.Unit (set_mode `Set), " Set the variable";
     "--start", Arg.Unit (set_mode `Start), "name Start a job manually";
     "--tail", Arg.Unit (set_mode `Tail), "serial Tail job output";
     "--set", Arg.Unit (set_mode `Set), " Set the variable";
     "--start", Arg.Unit (set_mode `Start), "name Start a job manually";
     "--tail", Arg.Unit (set_mode `Tail), "serial Tail job output";
-    "--type", Arg.Set_string typ, "bool|int|float|string|unit Set the variable type";
+    "--type", Arg.String set_type, "bool|int|float|string|.. Set the variable type";
     "--upload", Arg.Unit (set_mode `Upload), " Upload the script";
     "--variables", Arg.Unit (set_mode `Variables), " Display all variables and values";
     "-V", Arg.Unit display_version, " Display version number and exit";
     "--version", Arg.Unit display_version, " Display version number and exit";
   ] in
 
     "--upload", Arg.Unit (set_mode `Upload), " Upload the script";
     "--variables", Arg.Unit (set_mode `Variables), " Display all variables and values";
     "-V", Arg.Unit display_version, " Display version number and exit";
     "--version", Arg.Unit display_version, " Display version number and exit";
   ] in
 
-  let args = ref [] in
-  let anon_fun str = args := str :: !args in
+  (* anon_fun normally just collects up the anonymous arguments as
+   * strings, and most modes just use 'args' as a list of strings.
+   * However for `Set mode we need to record the type of each argument
+   * as well, so we keep that in a separate list (argtypes).
+   *)
+  let argtypes = ref [] in
+  let anon_fun str = argtypes := (str, !typ) :: !argtypes in
 
   let usage_msg = "\
 Whenjobs is a powerful but simple cron replacement.
 
   let usage_msg = "\
 Whenjobs is a powerful but simple cron replacement.
@@ -118,7 +137,7 @@ Editing the script:
 Get and set variables:
 
   whenjobs --get variable
 Get and set variables:
 
   whenjobs --get variable
-  whenjobs --set variable value
+  whenjobs --set variable=value
 
 Start and stop the per-user daemon:
 
 
 Start and stop the per-user daemon:
 
@@ -132,17 +151,8 @@ Options:
   Arg.parse argspec anon_fun usage_msg;
 
   let mode = !mode in
   Arg.parse argspec anon_fun usage_msg;
 
   let mode = !mode in
-  let args = List.rev !args in
-
-  let typ = match !typ with
-    | "bool"|"boolean" -> `Bool
-    | "string" -> `String
-    | "int" -> `Int
-    | "float"|"double" -> `Float
-    | "unit" -> `Unit
-    | t ->
-      eprintf "whenjobs: --type: unknown type (%s)\n" t;
-      exit 1 in
+  let argtypes = List.rev !argtypes in
+  let args = List.map fst argtypes in
   let nr_args = List.length args in
   let arg1 = match args with [] -> "" | a::_ -> a in
 
   let nr_args = List.length args in
   let arg1 = match args with [] -> "" | a::_ -> a in
 
@@ -166,13 +176,18 @@ Options:
     upload_file ()
 
   | Some `Set ->
     upload_file ()
 
   | Some `Set ->
-    if List.length args != 2 then (
-      eprintf "whenjobs --set variable value\n";
-      eprintf "If 'value' contains spaces, you may need to quote it.\n";
+    if nr_args = 2 && not (String.contains arg1 '=') then (
+      eprintf "'whenjobs --set variable value' is the old whenjobs <= 0.5 syntax!\n";
+      eprintf "You need to change this to:\n";
+      eprintf "  whenjobs --set variable=value\n";
       suggest_help ();
       exit 1
     );
       suggest_help ();
       exit 1
     );
-    set_variable (List.hd args) (List.hd (List.tl args)) typ
+    (* Just ignore the case where no variables are defined, to make
+     * it easier to write shell scripts.
+     *)
+    if nr_args > 0 then
+      set_variables argtypes
 
   | Some `Get ->
     if nr_args != 1 then (
 
   | Some `Get ->
     if nr_args != 1 then (
@@ -231,6 +246,10 @@ Options:
     );
     tail arg1
 
     );
     tail arg1
 
+  | Some `JobNames ->
+    unused_error args "--job-names";
+    job_names ()
+
 and edit_file () =
   (* If there is no initial file, create an empty one containing the
    * tutorial.
 and edit_file () =
   (* If there is no initial file, create an empty one containing the
    * tutorial.
@@ -311,39 +330,27 @@ and upload_file () =
   );
   stop_client client
 
   );
   stop_client client
 
-and set_variable name value typ =
-  let value = match typ with
-    | `Bool ->
-      (match value with
-      | "true"|"t"|"yes"|"y"|"on"|"1" -> `bool_t true
-      | "false"|"f"|"no"|"n"|"off"|"0" -> `bool_t false
-      | _ ->
-        eprintf "whenjobs: variable does not have a boolean value\n";
-        exit 1
-      )
-    | `String -> `string_t value
-    | `Int ->
-      (try ignore (big_int_of_string value)
-       with Failure _ ->
-         eprintf "whenjobs: variable is not an integer\n";
-         exit 1
-      );
-      `int_t value (* the string is what we pass over the wire *)
-    | `Float ->
-      (try `float_t (float_of_string value)
-       with Failure _ ->
-         eprintf "whenjobs: variable is not a floating point number\n";
-         exit 1
-      )
-    | `Unit ->
-      if value <> "" then (
-        eprintf "whenjobs: unit variables must be empty strings\n";
-        exit 1
-      );
-      `unit_t in
+and set_variables argtypes =
+  let vars = List.map (
+    fun (def, typ) ->
+      (* 'def' should have the form "name=value".  The value part may
+       * be missing, but the equals sign is required.
+       *)
+      let i =
+        try String.index def '='
+        with Not_found ->
+          eprintf "whenjobs: set: missing = sign in variable definition\n";
+          suggest_help ();
+          exit 1 in
+      let name = String.sub def 0 i in
+      let value = String.sub def (i+1) (String.length def - (i+1)) in
+      let value = value_of_string value typ in
+      { Whenproto_aux.sv_name = name; sv_value = value }
+  ) argtypes in
+  let vars = Array.of_list vars in
 
   let client = start_client () in
 
   let client = start_client () in
-  (match Whenproto_clnt.When.V1.set_variable client (name, value) with
+  (match Whenproto_clnt.When.V1.set_variables client vars with
   | `ok -> ()
   | `error msg ->
     eprintf "whenjobs: set: %s\n" msg;
   | `ok -> ()
   | `error msg ->
     eprintf "whenjobs: set: %s\n" msg;
@@ -445,6 +452,12 @@ and tail serial =
       (Filename.quote job.Whenproto_aux.job_tmpdir) in
   exit (Sys.command cmd)
 
       (Filename.quote job.Whenproto_aux.job_tmpdir) in
   exit (Sys.command cmd)
 
+and job_names () =
+  let client = start_client () in
+  let names = Whenproto_clnt.When.V1.get_job_names client () in
+  stop_client client;
+  Array.iter print_endline names
+
 and unused_error args op =
   if args <> [] then (
     eprintf "whenjobs %s: unused parameters on the command line.\n" op;
 and unused_error args op =
   if args <> [] then (
     eprintf "whenjobs %s: unused parameters on the command line.\n" op;
@@ -494,6 +507,36 @@ and string_of_variable = function
   | `int_t i -> i (* passed on the wire as a string *)
   | `float_t f -> string_of_float f
 
   | `int_t i -> i (* passed on the wire as a string *)
   | `float_t f -> string_of_float f
 
+and value_of_string value = function
+  | `Bool ->
+    (match value with
+    | "true"|"t"|"yes"|"y"|"on"|"1" -> `bool_t true
+    | "false"|"f"|"no"|"n"|"off"|"0" -> `bool_t false
+    | _ ->
+      eprintf "whenjobs: variable does not have a boolean value\n";
+      exit 1
+    )
+  | `String -> `string_t value
+  | `Int ->
+    (try ignore (big_int_of_string value)
+     with Failure _ ->
+       eprintf "whenjobs: variable is not an integer\n";
+       exit 1
+    );
+    `int_t value (* the string is what we pass over the wire *)
+  | `Float ->
+    (try `float_t (float_of_string value)
+     with Failure _ ->
+       eprintf "whenjobs: variable is not a floating point number\n";
+       exit 1
+    )
+  | `Unit ->
+    if value <> "" then (
+      eprintf "whenjobs: unit variables must be empty strings\n";
+      exit 1
+    );
+    `unit_t
+
 let () =
   try main ()
   with
 let () =
   try main ()
   with