Browse Source

Add import subcommand

 + Allow to import form old-style, json-formatted file. They were used
 before 0.3.X
 + We use two flags instead of optional arguments due to an exception
 raised with two string-based optional arguments. These flags would not
 be used often, since they are clever defaults.
 + Old name of the rc file (in .json) added to Const module.
 + Code to deduplicate entries in rc file expects object, while
 structures were used in the past. Thus, the ability to remove
 duplicated entries in old rc file has been removed.
 + An help message was added, to inform the user that one may import
 using the import command.
Leo 8 years ago
parent
commit
d35ee64ca2
6 changed files with 54 additions and 3 deletions
  1. 23 1
      src/command_def.ml
  2. 10 0
      src/const.ml
  3. 1 0
      src/const.mli
  4. 1 1
      src/file_com.ml
  5. 17 1
      src/rc.ml
  6. 2 0
      src/rc.mli

+ 23 - 1
src/command_def.ml

@@ -279,6 +279,27 @@ let edit =
     )
 ;;
 
+(* Import from older version *)
+let import =
+  basic
+    ~summary:"Import an rc file of version <= 0.2.x"
+
+    Spec.(
+      empty
+      +> shared_params
+      +> flag "--from" (optional file) ~aliases:["-from"; "-f"]
+           ~doc:"file Import from given [FILE], json format (default: default-rc-file.json)"
+      +> flag "--to" (optional file) ~aliases:["-t"; "-to"]
+           ~doc:"file Write imported file to [FILE], sexp format (default: default-rc-file.scm)"
+    )
+    (fun _ from to_file () ->
+      let open Option in
+      let from = value ~default:Const.rc_file_old (from >>| Lazy.return) in
+      let to_file = value ~default:!Const.rc_file (to_file >>| Lazy.return) in
+      Rc.import ~from ~to_file
+    )
+;;
+
 (* To display informations about the licence *)
 let licence =
   basic
@@ -343,7 +364,8 @@ let run ~version ~build_info () =
       ~preserve_subcommand_order:()
       [ ("run", default) ; ("licence", licence) ; ("add", add) ; ("edit", edit)
       ; ("list", list) ; ("cleanup", clean) ; ("delete", delete)
-      ; ("state", state) ; ( "reset", reset) ; ( "reset-all", reset_all) ]
+      ; ("state", state) ; ( "reset", reset) ; ( "reset-all", reset_all)
+      ; ( "import", import ) ]
     |> run ~version ~build_info
   in
 

+ 10 - 0
src/const.ml

@@ -97,6 +97,16 @@ let no_color =
       )
 ;;
 
+(* Default place to read settings, before version 0.3.x *)
+let rc_file_old =
+  let internal_default : string lazy_t =
+    (* Default value, if no value is given (for instance as
+       command line argument), or no environnement variable is set *)
+    Lazy.(home >>| fun home -> home ^ "/" ^ ".oclaunch_rc.json")
+  in
+  get_var ~default:internal_default (lazy "OC_RC_OLD")
+;;
+
 (* Default place to read settings *)
 let rc_file_default =
   let internal_default : string lazy_t =

+ 1 - 0
src/const.mli

@@ -44,6 +44,7 @@ val ask : bool option ref
 val no_color : bool ref
 
 (* Files *)
+val rc_file_old : string Core.Std.Lazy.t
 val rc_file : string Core.Std.Lazy.t ref
 val tmp_file : string
 (* Conf *)

+ 1 - 1
src/file_com.ml

@@ -65,7 +65,7 @@ let write (rc_file:t) =
   let name = !Const.rc_file in
   (* Create string to be written, after removing duplicated commands (and
    * newlines) *)
-  let data = (Unify.prettify rc_file |> Settings_j.string_of_rc_file
+  let data = (rc_file |> Settings_j.string_of_rc_file
               |> Yojson.Basic.prettify ~std:true) in
   Out_channel.write_all (Lazy.force name) ~data
 ;;

+ 17 - 1
src/rc.ml

@@ -107,7 +107,10 @@ let welcome_msg =
 ;;
 
 let create_basic name =
-  sprintf "Initializing empty configuration file in %s." name
+  sprintf "Initializing empty configuration file in %s.\n\
+           You may import your rc file from an older version with\n\
+           `oclaunch import`\
+  " name
   |> Messages.warning;
   Messages.tips welcome_msg;
   Licencing.print ~cecill:false;
@@ -299,3 +302,16 @@ let change_setting () = failwith "Not implemented"
 let empty_entry () =
   new entry ~tags:[] ""
 ;;
+
+(* Import from older version *)
+let import ~from ~to_file =
+  let imported_rc : t = init ~rc:to_file () in
+  let to_import : File_com.t = File_com.init_rc ~rc:from () in
+  to_import.progs |> List.map ~f:(new entry)
+  |> imported_rc#change_entries
+  |> fun rc -> rc#write;
+  Messages.ok "Import successful!";
+  sprintf "from: %s; to: %s" (Lazy.force from) (Lazy.force to_file)
+  |> Messages.tips
+;;
+

+ 2 - 0
src/rc.mli

@@ -94,3 +94,5 @@ type t = <
 val init : ?rc:string lazy_t -> unit -> t
 
 val empty_entry : unit -> entry
+
+val import : from:(string Lazy.t) -> to_file:(string Lazy.t) -> unit