From e8fb2bf5154065818cc995b4855890992fab783d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sat, 26 May 2018 12:50:38 +0200 Subject: [PATCH] Signer: do not depend on `Client_main_run` --- src/bin_signer/main_signer.ml | 106 +++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 7 deletions(-) diff --git a/src/bin_signer/main_signer.ml b/src/bin_signer/main_signer.ml index c9e28428a..f61578260 100644 --- a/src/bin_signer/main_signer.ml +++ b/src/bin_signer/main_signer.ml @@ -109,12 +109,104 @@ let commands = Https_daemon.run cctxt ~host ~port ~cert ~key) ; ] +let home = try Sys.getenv "HOME" with Not_found -> "/root" + +let default_base_dir = + Filename.concat home ".tezos-signer" + +let (//) = Filename.concat + +let string_parameter () : (string, _) parameter = + parameter (fun _ x -> return x) + +let base_dir_arg () = + arg + ~long:"base-dir" + ~short:'d' + ~placeholder:"path" + ~doc:("signer data directory\n\ + The directory where the Tezos client will store all its data.\n\ + By default: '" ^ default_base_dir ^"'.") + (string_parameter ()) + +let global_options () = + args1 + (base_dir_arg ()) + +(* Main (lwt) entry *) +let main () = + let executable_name = Filename.basename Sys.executable_name in + let original_args, autocomplete = + (* for shell aliases *) + let rec move_autocomplete_token_upfront acc = function + | "bash_autocomplete" :: prev_arg :: cur_arg :: script :: args -> + let args = List.rev acc @ args in + args, Some (prev_arg, cur_arg, script) + | x :: rest -> move_autocomplete_token_upfront (x :: acc) rest + | [] -> List.rev acc, None in + match Array.to_list Sys.argv with + | _ :: args -> move_autocomplete_token_upfront [] args + | [] -> [], None in + Random.self_init () ; + ignore Clic.(setup_formatter Format.std_formatter + (if Unix.isatty Unix.stdout then Ansi else Plain) Short) ; + ignore Clic.(setup_formatter Format.err_formatter + (if Unix.isatty Unix.stderr then Ansi else Plain) Short) ; + begin + begin + parse_global_options + (global_options ()) () original_args >>=? fun (base_dir, remaining) -> + let base_dir = Option.unopt ~default:default_base_dir base_dir in + let cctxt = object + inherit Client_context_unix.unix_logger ~base_dir + inherit Client_context_unix.unix_prompter + inherit Client_context_unix.unix_wallet ~base_dir + end in + Client_keys.register_signer + (module Tezos_signer_backends.Encrypted) ; + Client_keys.register_signer + (module Tezos_signer_backends.Unencrypted) ; + let commands = + Clic.add_manual + ~executable_name + ~global_options:(global_options ()) + (if Unix.isatty Unix.stdout then Clic.Ansi else Clic.Plain) + Format.std_formatter + commands in + begin match autocomplete with + | Some (prev_arg, cur_arg, script) -> + Clic.autocompletion + ~script ~cur_arg ~prev_arg ~args:original_args + ~global_options:(global_options ()) + commands cctxt >>=? fun completions -> + List.iter print_endline completions ; + return () + | None -> + Clic.dispatch commands cctxt remaining + end + end >>= function + | Ok () -> + Lwt.return 0 + | Error [ Clic.Help command ] -> + Clic.usage + Format.std_formatter + ~executable_name + ~global_options:(global_options ()) + (match command with None -> [] | Some c -> [ c ]) ; + Lwt.return 0 + | Error errs -> + Clic.pp_cli_errors + Format.err_formatter + ~executable_name + ~global_options:(global_options ()) + ~default:Error_monad.pp + errs ; + Lwt.return 1 + end >>= fun retcode -> + Format.pp_print_flush Format.err_formatter () ; + Format.pp_print_flush Format.std_formatter () ; + Logging_unix.close () >>= fun () -> + Lwt.return retcode let () = - Client_main_run.run - (fun _ _ -> - return @@ - List.map - (Clic.map_command - (fun (o : Client_context.full) -> (o :> Client_context.io_wallet))) - commands) + Pervasives.exit (Lwt_main.run (main ()))