2018-01-16 07:10:20 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2018-01-16 07:10:20 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(* Tezos Command line interface - Main Program *)
|
|
|
|
|
2018-02-16 14:08:04 +04:00
|
|
|
open Client_context_unix
|
2018-02-14 18:20:03 +04:00
|
|
|
|
2018-02-14 18:33:47 +04:00
|
|
|
let builtin_commands =
|
2018-04-03 13:39:09 +04:00
|
|
|
let open Clic in
|
2018-02-14 18:33:47 +04:00
|
|
|
[
|
|
|
|
command
|
|
|
|
~desc: "List the protocol versions that this client understands."
|
|
|
|
no_options
|
|
|
|
(fixed [ "list" ; "understood" ; "protocols" ])
|
2018-02-16 21:10:18 +04:00
|
|
|
(fun () (cctxt : #Client_context.full) ->
|
2018-02-14 18:33:47 +04:00
|
|
|
Lwt_list.iter_s
|
|
|
|
(fun (ver, _) -> cctxt#message "%a" Protocol_hash.pp_short ver)
|
|
|
|
(Client_commands.get_versions ()) >>= fun () ->
|
|
|
|
return ()) ;
|
|
|
|
]
|
|
|
|
|
2018-02-28 21:02:56 +04:00
|
|
|
(* Duplicated from the node, here for now since the client still
|
|
|
|
embeds the baker. To be moved where appropriate when the baker is
|
|
|
|
definitively moved/factorized in its own binary. *)
|
|
|
|
let find_log_rules () =
|
|
|
|
match Option.try_with (fun () -> Sys.getenv "TEZOS_LOG"),
|
|
|
|
Option.try_with (fun () -> Sys.getenv "LWT_LOG")
|
|
|
|
with
|
|
|
|
| Some rules, None -> "environment variable TEZOS_LOG", Some rules
|
|
|
|
| None, Some rules -> "environment variable LWT_LOG", Some rules
|
|
|
|
| None, None -> "default rules", None
|
|
|
|
| Some rules, Some _ ->
|
|
|
|
Format.eprintf
|
|
|
|
"@[<v 2>@{<warning>@{<title>Warning@}@} \
|
|
|
|
Both environment variables TEZOS_LOG and LWT_LOG \
|
|
|
|
defined, using TEZOS_LOG.@]@\n@." ;
|
|
|
|
"environment varible TEZOS_LOG", Some rules
|
|
|
|
|
|
|
|
let init_logger () =
|
|
|
|
Lwt_log_core.add_rule "*" Lwt_log_core.Notice ;
|
|
|
|
let origin, rules = find_log_rules () in
|
|
|
|
Option.iter rules ~f:begin fun rules ->
|
|
|
|
try Lwt_log_core.load_rules rules ~fail_on_error:true
|
|
|
|
with _ ->
|
|
|
|
Pervasives.failwith
|
|
|
|
(Format.asprintf "Incorrect log rules defined in %s." origin)
|
|
|
|
end ;
|
|
|
|
Logging_unix.(init ~template:"$(message)" Stderr)
|
|
|
|
|
2018-01-16 07:10:20 +04:00
|
|
|
(* Main (lwt) entry *)
|
2018-02-14 14:01:23 +04:00
|
|
|
let main select_commands =
|
2018-02-13 20:56:47 +04:00
|
|
|
let executable_name = Filename.basename Sys.executable_name in
|
|
|
|
let global_options = Client_config.global_options () 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
|
2018-01-16 07:10:20 +04:00
|
|
|
Random.self_init () ;
|
2018-04-03 13:39:09 +04:00
|
|
|
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) ;
|
2018-02-28 21:02:56 +04:00
|
|
|
init_logger () >>= fun () ->
|
2018-02-13 20:56:47 +04:00
|
|
|
Lwt.catch begin fun () -> begin
|
2018-01-16 07:10:20 +04:00
|
|
|
Client_config.parse_config_args
|
2018-02-16 21:10:18 +04:00
|
|
|
(new unix_full
|
2018-02-16 14:08:04 +04:00
|
|
|
~block:Client_config.default_block
|
|
|
|
~base_dir:Client_config.default_base_dir
|
|
|
|
~rpc_config:RPC_client.default_config)
|
2018-01-16 07:10:20 +04:00
|
|
|
original_args
|
2018-01-12 04:10:12 +04:00
|
|
|
>>=? fun (parsed_config_file, parsed_args, config_commands, remaining) ->
|
2018-02-11 22:17:39 +04:00
|
|
|
let rpc_config : RPC_client.config = {
|
|
|
|
RPC_client.default_config with
|
2018-01-16 07:10:20 +04:00
|
|
|
host = parsed_config_file.node_addr ;
|
|
|
|
port = parsed_config_file.node_port ;
|
|
|
|
tls = parsed_config_file.tls ;
|
|
|
|
} in
|
2018-02-11 22:17:39 +04:00
|
|
|
let ctxt = new RPC_client.http_ctxt rpc_config Media_type.all_media_types in
|
2018-02-14 14:01:23 +04:00
|
|
|
select_commands ctxt parsed_args >>=? fun commands ->
|
2018-02-13 20:56:47 +04:00
|
|
|
let commands =
|
2018-04-03 13:39:09 +04:00
|
|
|
Clic.add_manual
|
2018-02-13 20:56:47 +04:00
|
|
|
~executable_name
|
|
|
|
~global_options
|
2018-04-03 13:39:09 +04:00
|
|
|
(if Unix.isatty Unix.stdout then Clic.Ansi else Clic.Plain)
|
2018-02-13 20:56:47 +04:00
|
|
|
Format.std_formatter
|
2018-02-14 18:33:47 +04:00
|
|
|
(config_commands @ builtin_commands @ commands) in
|
2018-01-16 07:10:20 +04:00
|
|
|
let rpc_config =
|
|
|
|
if parsed_args.print_timings then
|
|
|
|
{ rpc_config with
|
|
|
|
logger = RPC_client.timings_logger Format.err_formatter }
|
|
|
|
else if parsed_args.log_requests
|
|
|
|
then { rpc_config with logger = RPC_client.full_logger Format.err_formatter }
|
|
|
|
else rpc_config
|
|
|
|
in
|
|
|
|
let client_config =
|
2018-02-16 21:10:18 +04:00
|
|
|
new unix_full
|
2018-02-16 14:08:04 +04:00
|
|
|
~block:parsed_args.block
|
|
|
|
~base_dir:parsed_config_file.base_dir
|
|
|
|
~rpc_config:rpc_config in
|
2018-02-13 20:56:47 +04:00
|
|
|
begin match autocomplete with
|
|
|
|
| Some (prev_arg, cur_arg, script) ->
|
2018-04-03 13:39:09 +04:00
|
|
|
Clic.autocompletion
|
2018-02-13 20:56:47 +04:00
|
|
|
~script ~cur_arg ~prev_arg ~args:original_args ~global_options
|
|
|
|
commands client_config >>=? fun completions ->
|
|
|
|
List.iter print_endline completions ;
|
|
|
|
return ()
|
|
|
|
| None ->
|
2018-04-03 13:39:09 +04:00
|
|
|
Clic.dispatch commands client_config remaining
|
2018-02-13 20:56:47 +04:00
|
|
|
end
|
|
|
|
end >>= function
|
|
|
|
| Ok () ->
|
|
|
|
Lwt.return 0
|
2018-04-03 13:39:09 +04:00
|
|
|
| Error [ Clic.Help command ] ->
|
|
|
|
Clic.usage
|
2018-02-13 20:56:47 +04:00
|
|
|
Format.std_formatter
|
|
|
|
~executable_name
|
|
|
|
~global_options
|
|
|
|
(match command with None -> [] | Some c -> [ c ]) ;
|
|
|
|
Lwt.return 0
|
2018-01-16 07:10:20 +04:00
|
|
|
| Error errs ->
|
2018-04-03 13:39:09 +04:00
|
|
|
Clic.pp_cli_errors
|
2018-02-13 20:56:47 +04:00
|
|
|
Format.err_formatter
|
|
|
|
~executable_name
|
|
|
|
~global_options
|
|
|
|
~default:Error_monad.pp
|
|
|
|
errs ;
|
2018-01-16 07:10:20 +04:00
|
|
|
Lwt.return 1
|
|
|
|
end begin function
|
2018-02-13 20:56:47 +04:00
|
|
|
| Client_commands.Version_not_found ->
|
|
|
|
Format.eprintf "@{<error>@{<title>Fatal error@}@} unknown protocol version." ;
|
2018-01-16 07:10:20 +04:00
|
|
|
Lwt.return 1
|
|
|
|
| Failure message ->
|
2018-02-13 20:56:47 +04:00
|
|
|
Format.eprintf "@{<error>@{<title>Fatal error@}@} %s." message ;
|
2018-01-16 07:10:20 +04:00
|
|
|
Lwt.return 1
|
|
|
|
| exn ->
|
2018-02-13 20:56:47 +04:00
|
|
|
Format.printf "@{<error>@{<title>Fatal error@}@} %s." (Printexc.to_string exn) ;
|
2018-01-16 07:10:20 +04:00
|
|
|
Lwt.return 1
|
2018-02-13 20:56:47 +04:00
|
|
|
end >>= fun retcode ->
|
2018-01-18 19:18:55 +04:00
|
|
|
Format.pp_print_flush Format.err_formatter () ;
|
|
|
|
Format.pp_print_flush Format.std_formatter () ;
|
2018-02-28 21:02:56 +04:00
|
|
|
Logging_unix.close () >>= fun () ->
|
2018-02-13 20:56:47 +04:00
|
|
|
Lwt.return retcode
|
2018-02-14 14:01:23 +04:00
|
|
|
|
|
|
|
(* Where all the user friendliness starts *)
|
|
|
|
let run select_commands =
|
|
|
|
Pervasives.exit (Lwt_main.run (main select_commands))
|