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 *)
|
|
|
|
|
|
|
|
let cctxt ~base_dir ~block rpc_config =
|
2018-02-14 03:54:33 +04:00
|
|
|
Client_context_unix.make_context ~base_dir ~block ~rpc_config (Client_context_unix.default_log ~base_dir)
|
2018-01-16 07:10:20 +04:00
|
|
|
|
|
|
|
let get_commands_for_version ctxt block protocol =
|
2018-02-08 13:51:02 +04:00
|
|
|
Block_services.protocol ctxt block >>= function
|
2018-01-16 07:10:20 +04:00
|
|
|
| Ok version -> begin
|
|
|
|
match protocol with
|
|
|
|
| None ->
|
|
|
|
return (Some version, Client_commands.commands_for_version version)
|
|
|
|
| Some given_version -> begin
|
|
|
|
if not (Protocol_hash.equal version given_version) then
|
|
|
|
Format.eprintf
|
|
|
|
"@[<v 2>Warning:@,\
|
|
|
|
The protocol provided via `-protocol` (%a)@,\
|
2018-02-13 20:56:47 +04:00
|
|
|
is not the one retrieved from the node (%a).@]@\n@."
|
2018-01-16 07:10:20 +04:00
|
|
|
Protocol_hash.pp_short given_version
|
|
|
|
Protocol_hash.pp_short version ;
|
|
|
|
return (Some version, Client_commands.commands_for_version given_version)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
| Error errs -> begin
|
|
|
|
match protocol with
|
|
|
|
| None -> begin
|
|
|
|
Format.eprintf
|
2018-02-13 20:56:47 +04:00
|
|
|
"@[<v 2>@{<warning>@{<title>Warning@}@}@,\
|
|
|
|
Failed to acquire the protocol version from the node@,%a@]@\n@."
|
2018-01-16 07:10:20 +04:00
|
|
|
(Format.pp_print_list pp) errs ;
|
|
|
|
return (None, [])
|
|
|
|
end
|
|
|
|
| Some version ->
|
|
|
|
return (Some version, Client_commands.commands_for_version version)
|
|
|
|
end
|
|
|
|
|
|
|
|
(* Main (lwt) entry *)
|
|
|
|
let main ?only_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-02-13 20:56:47 +04:00
|
|
|
ignore Cli_entries.(setup_formatter Format.std_formatter
|
|
|
|
(if Unix.isatty Unix.stdout then Ansi else Plain) Short) ;
|
|
|
|
ignore Cli_entries.(setup_formatter Format.err_formatter
|
|
|
|
(if Unix.isatty Unix.stderr then Ansi else Plain) Short) ;
|
|
|
|
Lwt.catch begin fun () -> begin
|
2018-01-16 07:10:20 +04:00
|
|
|
Client_config.parse_config_args
|
2018-02-14 03:54:33 +04:00
|
|
|
(cctxt ~base_dir:Client_context_unix.default_base_dir
|
|
|
|
~block:Client_context_unix.default_block
|
2018-02-11 22:17:39 +04:00
|
|
|
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-01-16 07:10:20 +04:00
|
|
|
begin match only_commands with
|
|
|
|
| None ->
|
|
|
|
get_commands_for_version ctxt
|
|
|
|
parsed_args.block
|
|
|
|
parsed_args.protocol >>|? fun (_version, commands_for_version) ->
|
|
|
|
Client_generic_rpcs.commands @
|
|
|
|
Client_network.commands () @
|
|
|
|
Client_keys.commands () @
|
|
|
|
Client_protocols.commands () @
|
|
|
|
Client_helpers.commands () @
|
2018-01-12 04:10:12 +04:00
|
|
|
config_commands @
|
2018-01-16 07:10:20 +04:00
|
|
|
commands_for_version
|
2018-01-12 04:10:12 +04:00
|
|
|
| Some commands ->
|
|
|
|
return (config_commands @ commands)
|
|
|
|
end >>=? fun commands ->
|
2018-02-13 20:56:47 +04:00
|
|
|
let commands =
|
|
|
|
Cli_entries.add_manual
|
|
|
|
~executable_name
|
|
|
|
~global_options
|
|
|
|
(if Unix.isatty Unix.stdout then Cli_entries.Ansi else Cli_entries.Plain)
|
|
|
|
Format.std_formatter
|
|
|
|
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 =
|
|
|
|
cctxt ~block:parsed_args.block ~base_dir:parsed_config_file.base_dir rpc_config in
|
2018-02-13 20:56:47 +04:00
|
|
|
begin match autocomplete with
|
|
|
|
| Some (prev_arg, cur_arg, script) ->
|
|
|
|
Cli_entries.autocompletion
|
|
|
|
~script ~cur_arg ~prev_arg ~args:original_args ~global_options
|
|
|
|
commands client_config >>=? fun completions ->
|
|
|
|
List.iter print_endline completions ;
|
|
|
|
return ()
|
|
|
|
| None ->
|
|
|
|
Cli_entries.dispatch commands client_config remaining
|
|
|
|
end
|
|
|
|
end >>= function
|
|
|
|
| Ok () ->
|
|
|
|
Lwt.return 0
|
|
|
|
| Error [ Cli_entries.Help command ] ->
|
|
|
|
Cli_entries.usage
|
|
|
|
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-02-13 20:56:47 +04:00
|
|
|
Cli_entries.pp_cli_errors
|
|
|
|
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 ->
|
|
|
|
Format.fprintf Format.std_formatter "@." ;
|
|
|
|
Format.fprintf Format.err_formatter "@." ;
|
|
|
|
Lwt.return retcode
|