diff options
author | Marc Coquand <marc@mccd.space> | 2023-12-26 11:15:33 -0600 |
---|---|---|
committer | Marc Coquand <marc@mccd.space> | 2023-12-26 11:15:33 -0600 |
commit | b826340d7536c2777fdcafb3bfcc6ebc8d8053ee (patch) | |
tree | 5eb2ba17a4c5edefb82bc32677c68bbf84968249 /bin | |
parent | fc077e16adf2860af3d32a60cabd67f57bab0803 (diff) | |
download | fixgen-b826340d7536c2777fdcafb3bfcc6ebc8d8053ee.tar.gz fixgen-b826340d7536c2777fdcafb3bfcc6ebc8d8053ee.tar.bz2 fixgen-b826340d7536c2777fdcafb3bfcc6ebc8d8053ee.zip |
Support output file
Diffstat (limited to '')
-rw-r--r-- | bin/main.ml | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/bin/main.ml b/bin/main.ml index d644133..43ce56b 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -7,7 +7,26 @@ let parse (s : string) = ast -let fixgen content output_type () = +let print_stdout = function + | [ (_, content) ] -> print_endline content + | list -> + list + |> List.iter (fun (name, content) -> + print_endline ("FILE NAME: " ^ name); + print_endline content) + + +let file_name name file_format = name ^ "." ^ file_format + +let save_files file_format output_folder list = + list + |> List.iter (fun (name, content) -> + let file = open_out (output_folder ^ "/" ^ file_name name file_format) in + output_string file content; + close_out file) + + +let fixgen content output_type output_folder = let result = parse content in let generate_output = match output_type with @@ -15,13 +34,18 @@ let fixgen content output_type () = | "json" -> Fixture.json_of_generated_fixtures | _ -> failwith "Unsupported output type, supported types are: json, csv" in + let save_output = + match output_folder with + | "STDOUT" -> print_stdout + | s -> save_files output_type s + in match result with | Some ast -> Ast_types.compile ast - |> List.iter (fun (name, file) -> + |> List.map (fun (name, file) -> let result = generate_output file in - print_endline ("FILE NAME: " ^ name); - print_endline result) + name, result) + |> save_output | None -> print_endline "error" @@ -31,12 +55,17 @@ let file = (* TODO: Support option to set output folder *) +let output_folder = + let doc = "Set output folder" in + Arg.( + value & opt string "STDOUT" & info [ "o"; "output-folder" ] ~docv:"OUTPUT FOLDER" ~doc) + let output_type = let doc = "Set output file type, supported=csv" in Arg.(value & opt string "csv" & info [ "f"; "file-type" ] ~docv:"FILE TYPE" ~doc) -let fixgen_t = Term.(const fixgen $ file $ output_type $ const ()) +let fixgen_t = Term.(const fixgen $ file $ output_type $ output_folder) let cmd = Cmd.v (Cmd.info "fixgen") fixgen_t let () = exit (Cmd.eval cmd) |