Parcourir la source

Put configuration file in HOME

 - Read configuration from a file in home
 - If don't exists, then create it
 - Know problem
    - The created file is ugly
Leo il y a 10 ans
Parent
commit
e227ae9286
3 fichiers modifiés avec 36 ajouts et 9 suppressions
  1. 5 4
      Makefile
  2. 8 2
      const.ml
  3. 23 3
      file_com.ml

+ 5 - 4
Makefile

@@ -1,8 +1,9 @@
-all: atdgen-tmp code
+all: atdgen-rc code
 
-atdgen-tmp:
-	atdgen -t settings.atd
-	atdgen -j settings.atd
+atdgen-rc:
+	atdgen -t settings.atd # Types
+	atdgen -v settings.atd # Validator
+	atdgen -j -j-defaults -j-strict-fields settings.atd # Useful function
 
 code:
 	corebuild -pkg yojson,atdgen,core_extended oclaunch.byte

+ 8 - 2
const.ml

@@ -38,11 +38,17 @@
 
 open Core.Std;;
 
+(* Get current home *)
+let home = match (Sys.getenv "HOME") with
+  | Some x -> x
+  | None -> failwith "Wrong value for home"
+;;
+
 (* Some settings variales *)
-let rc_file = "rc_test.json";; (* TODO Dev value, change this *)
+let rc_file = home ^ "/" ^ ".oclaunch_rc.json";;
 (* Set tmp file, in witch stock launches *)
 let tmp_file = "/tmp/tmp_test.json";; (* TODO Dev value, change this *)
 (* Template for the tmp file *)
 let (tmp_file_template:Yojson.Basic.json) = `Assoc
                                               [ "cmd", `List [];
-                                              "num", `Int 0 ]
+                                              "num", `Int 0 ];;

+ 23 - 3
file_com.ml

@@ -46,8 +46,28 @@ let content = In_channel.input_all tmp_buffer in
 In_channel.close tmp_buffer; content
 ;;
 
+(* Return the configuration file template *)
+let rc_template () =
+  Settings_v.create_rc_file ~progs:[] ~settings:[]
+;;
+
+(* Function to create configuration file if it does not
+ * exists *)
+let create_rc_file ~name =
+  (* Notify that we initialise config file *)
+  printf "Initializing configuration file in %s\n" name;
+  let compact_rc_file = Settings_j.string_of_rc_file (rc_template () ()) in
+  let readable_rc_file = Yojson.Basic.prettify compact_rc_file in (* Create human readable string for rc file *)
+  let out_file = Out_channel.create name in
+    Out_channel.output_string out_file readable_rc_file;
+    Out_channel.close out_file
+;;
+
 (* Function to read the rc file *)
-let init_rc ~rc:rc_file =
-    string_f_file rc_file
-    |> Settings_j.rc_file_of_string
+let rec init_rc ~rc:rc_file =
+  (* Verify that file exist *)
+  match (Sys.file_exists rc_file) with
+    | `No -> create_rc_file ~name:rc_file; init_rc ~rc:rc_file;
+    | `Unknown -> failwith "Error reading configuration file";
+    | `Yes -> string_f_file rc_file |> Settings_j.rc_file_of_string
 ;;