From 0bb00678b2ec6ac505de82328425a1e477770f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Wed, 23 Jun 2021 12:31:49 +0200 Subject: [PATCH] Support Fmt.cli and Logs.cli --- dune | 2 +- main.ml | 41 ++++++++++++++++++----------------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/dune b/dune index 725b0a20..118f2839 100644 --- a/dune +++ b/dune @@ -3,4 +3,4 @@ (name main) (package obuilder) (preprocess (pps ppx_deriving.show)) - (libraries lwt lwt.unix fmt tar-unix obuilder fmt.tty cmdliner logs.fmt)) + (libraries lwt lwt.unix fmt fmt.cli fmt.tty tar-unix obuilder cmdliner logs.fmt logs.cli)) diff --git a/main.ml b/main.ml index 9d89d680..71f5aad3 100644 --- a/main.ml +++ b/main.ml @@ -1,8 +1,5 @@ open Lwt.Infix -let () = - Logs.set_reporter (Logs_fmt.reporter ()) - let ( / ) = Filename.concat module Sandbox = Obuilder.Runc_sandbox @@ -29,8 +26,7 @@ let read_whole_file path = let len = in_channel_length ic in really_input_string ic len - -let build store spec conf src_dir secrets = +let build () store spec conf src_dir secrets = Lwt_main.run begin create_builder store conf >>= fun (Builder ((module Builder), builder)) -> let spec = @@ -53,9 +49,7 @@ let build store spec conf src_dir secrets = exit 1 end -let healthcheck verbose store conf = - if verbose then - Logs.Src.set_level Obuilder.log_src (Some Logs.Info); +let healthcheck () store conf = Lwt_main.run begin create_builder store conf >>= fun (Builder ((module Builder), builder)) -> Builder.healthcheck builder >|= function @@ -66,13 +60,13 @@ let healthcheck verbose store conf = Fmt.pr "Healthcheck passed@." end -let delete store conf id = +let delete () store conf id = Lwt_main.run begin create_builder store conf >>= fun (Builder ((module Builder), builder)) -> Builder.delete builder id ~log:(fun id -> Fmt.pr "Removing %s@." id) end -let dockerfile buildkit spec = +let dockerfile () buildkit spec = Sexplib.Sexp.load_sexp spec |> Obuilder_spec.t_of_sexp |> Obuilder_spec.Docker.dockerfile_of_spec ~buildkit @@ -80,6 +74,16 @@ let dockerfile buildkit spec = open Cmdliner +let setup_log style_renderer level = + Fmt_tty.setup_std_outputs ?style_renderer (); + Logs.set_level level; + Logs.Src.set_level Obuilder.log_src level; + Logs.set_reporter (Logs_fmt.reporter ()); + () + +let setup_log = + Term.(const setup_log $ Fmt_cli.style_renderer () $ Logs_cli.level ()) + let spec_file = Arg.required @@ Arg.opt Arg.(some file) None @@ @@ -125,12 +129,12 @@ let secrets = let build = let doc = "Build a spec file." in - Term.(const build $ store $ spec_file $ Sandbox.cmdliner $ src_dir $ secrets), + Term.(const build $ setup_log $ store $ spec_file $ Sandbox.cmdliner $ src_dir $ secrets), Term.info "build" ~doc let delete = let doc = "Recursively delete a cached build result." in - Term.(const delete $ store $ Sandbox.cmdliner $ id), + Term.(const delete $ setup_log $ store $ Sandbox.cmdliner $ id), Term.info "delete" ~doc let buildkit = @@ -142,19 +146,12 @@ let buildkit = let dockerfile = let doc = "Convert a spec to Dockerfile format" in - Term.(const dockerfile $ buildkit $ spec_file), + Term.(const dockerfile $ setup_log $ buildkit $ spec_file), Term.info "dockerfile" ~doc -let verbose = - Arg.value @@ - Arg.flag @@ - Arg.info - ~doc:"Enable verbose logging" - ["verbose"] - let healthcheck = let doc = "Perform a self-test" in - Term.(const healthcheck $ verbose $ store $ Sandbox.cmdliner), + Term.(const healthcheck $ setup_log $ store $ Sandbox.cmdliner), Term.info "healthcheck" ~doc let cmds = [build; delete; dockerfile; healthcheck] @@ -167,6 +164,4 @@ let default_cmd = let term_exit (x : unit Term.result) = Term.exit x let () = - (* Logs.(set_level (Some Info)); *) - Fmt_tty.setup_std_outputs (); term_exit @@ Term.eval_choice default_cmd cmds