From cb124a87d8ed1a309b00c9163cde2911ee029251 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 09:02:53 -0600 Subject: Add cmdliner --- README.md | 25 ++++++++++++++++++------- bin/dune | 1 + bin/main.ml | 39 +++++++++++++++++++++++++++++---------- dune-project | 2 +- fixgen.opam | 1 + 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 024395b..e41f406 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,35 @@ # (WIP) Fixgen - Language agnostic fixture generator +Usually wiring up fake test data is a bunch of work, especially when that data is relational. Fixgen helps to alleviate some of that pain by allowing you to very easily generate fixtures that can also reference other fixtures. It also has some nice characteristics: + - Zero dependencies - Language agnostic +- Dead simple and fast grammar to generate fixtures in minutes +- Supports relations +- Writing in OCaml -This tool allows you to very easily generate fixtures. +## Usage -You simply define a fixgen file, for example sample.fixgen +Simply define a fixgen file, for example sample.fixgen ``` -user (2): id uuidv4, name name -purchase (10): id uuidv4, buyer user.id, amount int<1,40> +user (2): id uuidv4, name name, email email +purchase (10): id uuidv4, buyer user.id, amount int(1,40) ``` And then run it -``` +```sh $ fixgen sample.fixgen -f csv -o fixtures ``` -And it generates two files: fixtures/purchase.csv, fixtures/user.csv. Bam! Simple as that. +Or just generate in-line: + +```sh +$ fixgen "user (2): id uuidv4, name name; purchase (10): id uuidv4, buyer user.id, amount int<1,40> +``` + +For it to generate two files with fake relational data: fixtures/purchase.csv, fixtures/user.csv. Bam! Simple as that. You can then import it into your SQL server with simple copy from commands @@ -27,7 +38,7 @@ COPY users FROM '/fixtures/user.csv' CSV HEADER; COPY purchases FROM '/fixtures/purchase.csv' CSV HEADER; ``` -Or maybe export as json instead to import into your NoSQL DB. +Or maybe export as JSON instead to import into your NoSQL DB. ## Development - Prerequisites diff --git a/bin/dune b/bin/dune index ce5ed03..4a29c86 100644 --- a/bin/dune +++ b/bin/dune @@ -2,5 +2,6 @@ (public_name fixgen) (name main) (libraries + cmdliner fixgen) ) diff --git a/bin/main.ml b/bin/main.ml index 6942382..c964c5e 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,4 +1,5 @@ open Fixgen +open Cmdliner let parse (s : string) = let lexbuf = Lexing.from_string s in @@ -6,13 +7,31 @@ let parse (s : string) = ast -let () = - let result = parse "user: id uuidv4, uh uuidv4\npurchases: id uuidv4, pid user.id" in - match result with - | Some ast -> - Ast_types.compile ast ~amount:4 - |> List.iter (fun (name, file) -> - let result = Fixture.csv_of_generated_fixtures file in - print_endline ("FILE NAME: " ^ name); - print_endline result) - | None -> print_endline "error" +let fixgen content output_type () = + let result = parse content in + if output_type != "csv" + then print_endline "Only csv output is supported" + else ( + match result with + | Some ast -> + Ast_types.compile ast ~amount:4 + |> List.iter (fun (name, file) -> + let result = Fixture.csv_of_generated_fixtures file in + print_endline ("FILE NAME: " ^ name); + print_endline result) + | None -> print_endline "error") + + +let file = + let doc = "Fixgen content" in + Arg.(required & pos 0 (some string) None & info [] ~docv:"PROGRAM" ~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 cmd = Cmd.v (Cmd.info "fixgen") fixgen_t +let () = exit (Cmd.eval cmd) diff --git a/dune-project b/dune-project index 55c7f11..5676cf0 100644 --- a/dune-project +++ b/dune-project @@ -21,7 +21,7 @@ (name fixgen) (synopsis "Generate fixtures for testing") (description "Generate fixtures for testing") - (depends ocaml dune ppx_inline_test ppx_deriving uuidm alcotest + (depends ocaml dune ppx_inline_test ppx_deriving uuidm alcotest cmdliner ) ) diff --git a/fixgen.opam b/fixgen.opam index a68e9b0..338965e 100644 --- a/fixgen.opam +++ b/fixgen.opam @@ -15,6 +15,7 @@ depends: [ "ppx_deriving" "uuidm" "alcotest" + "cmdliner" "odoc" {with-doc} ] build: [ -- cgit v1.2.3