(** Batteries-included CLI runner for Eio applications *) (** {1 Running Applications} *) (* Batteries-included run with XDG and optional keyeio *) let run ?(use_keyeio=true) ~info ~app_name ~service main_term = let open Cmdliner in let run_main main profile_name_opt key_file_opt = (* Initialize RNG with default entropy source *) let () = Mirage_crypto_rng_unix.use_default () in Eio_main.run @@ fun env -> (* Create xdg context *) let xdg = Xdge.create env#fs app_name in (* Load keyeio profile if requested *) let profile = if not use_keyeio then (* Create empty profile when keyeio is not needed *) Keyeio.Profile.empty else (* Load from keyeio *) let keyeio_ctx = Keyeio.create xdg in let profile_name = Option.value profile_name_opt ~default:"default" in match key_file_opt with | Some _path -> (* TODO: Load from specified file *) failwith "Direct file loading not yet supported in eiocmd (use --profile instead)" | None -> (* Load from XDG directory *) match Keyeio.load_service keyeio_ctx ~service with | Ok svc -> (match Keyeio.Service.get_profile svc profile_name with | Some prof -> prof | None -> failwith (Printf.sprintf "Profile '%s' not found in service '%s'" profile_name service)) | Error (`Msg msg) -> failwith msg in main env xdg profile in (* Add profile and key-file flags only if using keyeio *) let profile_flag = if use_keyeio then let doc = Printf.sprintf "Profile name to use for %s service" service in Arg.(value & opt (some string) None & info [ "profile" ] ~docv:"NAME" ~doc) else Term.const None in let key_file_flag = if use_keyeio then let doc = Printf.sprintf "Override with direct path to %s key file" service in Arg.(value & opt (some file) None & info [ "key-file" ] ~docv:"FILE" ~doc) else Term.const None in (* Compose with main term and add logging setup *) let term = let open Term.Syntax in let+ main = main_term and+ log_level = Logs_cli.level () and+ profile_name_opt = profile_flag and+ key_file_opt = key_file_flag in Logs.set_reporter (Logs_fmt.reporter ()); Logs.set_level log_level; run_main main profile_name_opt key_file_opt in Cmd.v info term