Browse Source

Launch a program and log it

 - Problems
  - Launch only the first command
  - Don't increment "num" value in tmp file
Leo 11 years ago
parent
commit
32ff651a2c
6 changed files with 34 additions and 46 deletions
  1. 25 5
      exec_cmd.ml
  2. 3 12
      oclaunch.ml
  3. 1 1
      rc_test.json
  4. 1 0
      tmp_file.json
  5. 3 21
      tmp_file.ml
  6. 1 7
      tmp_test.json

+ 25 - 5
exec_cmd.ml

@@ -36,12 +36,32 @@
 
 open Core.Std;;
 
+(* Function to determinate what is the next command to
+ * execute *)
+let what_next ~tmp ~cmd_list =
+  let tmp_json = Yojson.Basic.from_file tmp in
+  let open Yojson.Basic.Util in
+  let num_next = tmp_json |> member "num" |> to_int in (* Number of the next cmd to run *)
+  let cmd_to_exec = List.nth cmd_list num_next in
+    match cmd_to_exec with
+      | None -> ""
+      | Some x -> x
+;;
+
+(* Log when a program has been launched in a file in /tmp
+   ~func is the function applied to the value *)
+let log ?(func= (+) 1 ) ~file_name =
+  let file = Yojson.Basic.from_file file_name in
+  match file with
+    | `Assoc [( a, `List b ); ("num", `Int c)] -> let new_value = `Assoc [( a, `List b ); ("num", `Int (c |> func))] in Yojson.Basic.to_file file_name new_value
+    | _ -> failwith "Incorrect format"
+;;
+
+
 (* Execute some command and log it *)
-let execute ?(display=false) cmd =
-    (*Tmp_file.log cmd*)
+let execute ?(display=true) ~tmp cmd =
+    log ~func:((+) 1) ~file_name:tmp;
     if display then
         print_endline cmd;
-    Sys.command cmd
-    |> print_int;
+    Sys.command cmd;
 ;;
-

+ 3 - 12
oclaunch.ml

@@ -41,17 +41,8 @@ let rc_content = File_com.init_rc ~rc:Const.rc_file;;
 
 (* Obtain data from tmp file *)
 let tmp_content = Tmp_file.init ~tmp:Const.tmp_file;;
-(* Generate structure of tmp file *)
-Tmp_file.create_struct ~tmp_file:tmp_content;;
-
-(*
-(* Function to determinate what is the next command to
- * execute *)
-let what_next =
-
 
+(*List.iter rc_content.progs ~f:print_endline*)
 (* Execute each item (one by one)in config file *)
-Exec_cmd.excute
-
-List.map ~f:Exec_cmd.execute rc_content.progs;;
-*)
+let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.progs ~tmp:tmp_content in
+Exec_cmd.execute ~tmp:tmp_content cmd_to_exec;; (* TODO Use display option in rc file *)

+ 1 - 1
rc_test.json

@@ -1,7 +1,7 @@
 {
     "progs" : [
-        "echo test",
         "task",
+        "echo test",
         "task +rdv",
         "vim"
     ],

+ 1 - 0
tmp_file.json

@@ -0,0 +1 @@
+{"cmd":[],"num":0}

+ 3 - 21
tmp_file.ml

@@ -52,7 +52,9 @@ let rec init ~tmp =
           Core_extended.Shell.rm tmp;
           init ~tmp:tmp
         end
-      | `Yes -> Yojson.Basic.pretty_to_channel (Out_channel.create tmp) Const.tmp_file_template
+      | `Yes -> Yojson.Basic.pretty_to_channel (Out_channel.create tmp)
+                  Const.tmp_file_template;
+                tmp
 ;;
 
 (* Verify that the value exist *)
@@ -63,13 +65,6 @@ let verify_key_exist ~key entry =
         false
 ;;
 
-(* Stock a value a file in /tmp
-   ~target is the target file *)
-let stock_tmp ~key ~value ~target =
-  let num_value = List.find target ~f:(verify_key_exist ~key:key) in
-    num_value
-;;
-
 (* Return true if a program is in the rc file *)
 let rec is_prog_in_rc list_from_rc_file program = (* TODO restaure ?(list_from_rc_file=rc_content.progs) *)
     match list_from_rc_file with
@@ -77,16 +72,3 @@ let rec is_prog_in_rc list_from_rc_file program = (* TODO restaure ?(list_from_r
     | [] -> false
     | hd :: tl -> if hd = program then true else is_prog_in_rc tl program
 ;;
-
-(*
-(* Log when a program has been launched *)
-let log program =
-    (* Verify the program exist in rc file *)
-    let prog_rc = (is_prog_in_rc program) in
-    match prog_rc with
-    | false -> (* failwith *) "Not in configuration file"
-    | true -> "Tmp value" (* TODO delete this *)
-    (* let open Tmp_log_t in
-    File_com.stock_tmp ~target:tmp_content.cmd ~key:program ~value:1 *)
-;;
-*)

+ 1 - 7
tmp_test.json

@@ -1,7 +1 @@
-{
-    "cmd": [
-        { "cmd1": 2 },
-        { "cmd2": 0.1 },
-        { "cmd3": 5 }
-    ]
-}
+{"cmd":[],"num":1}