2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(* Tezos Command line interface - Main Program *)
|
|
|
|
|
2017-03-13 20:40:51 +04:00
|
|
|
open Lwt.Infix
|
2017-04-05 01:35:41 +04:00
|
|
|
open Client_commands
|
|
|
|
open Error_monad
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-05 01:35:41 +04:00
|
|
|
let cctxt config rpc_config =
|
2016-11-22 17:23:40 +04:00
|
|
|
let startup =
|
|
|
|
CalendarLib.Printer.Precise_Calendar.sprint
|
|
|
|
"%Y-%m-%dT%H:%M:%SZ"
|
|
|
|
(CalendarLib.Calendar.Precise.now ()) in
|
|
|
|
let log channel msg = match channel with
|
|
|
|
| "stdout" ->
|
|
|
|
print_endline msg ;
|
|
|
|
Lwt.return ()
|
|
|
|
| "stderr" ->
|
|
|
|
prerr_endline msg ;
|
|
|
|
Lwt.return ()
|
|
|
|
| log ->
|
2017-03-15 04:17:20 +04:00
|
|
|
let (//) = Filename.concat in
|
|
|
|
Lwt_utils.create_dir (config.base_dir // "logs" // log) >>= fun () ->
|
2016-11-22 17:23:40 +04:00
|
|
|
Lwt_io.with_file
|
|
|
|
~flags: Unix.[ O_APPEND ; O_CREAT ; O_WRONLY ]
|
|
|
|
~mode: Lwt_io.Output
|
2017-03-15 04:17:20 +04:00
|
|
|
Client_commands.(config.base_dir // "logs" // log // startup)
|
2016-11-22 17:23:40 +04:00
|
|
|
(fun chan -> Lwt_io.write chan msg) in
|
2017-04-05 01:35:41 +04:00
|
|
|
Client_commands.make_context ~config ~rpc_config log
|
2016-11-22 17:23:40 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Main (lwt) entry *)
|
|
|
|
let main () =
|
|
|
|
Random.self_init () ;
|
|
|
|
Sodium.Random.stir () ;
|
2017-03-13 20:40:51 +04:00
|
|
|
Lwt.catch begin fun () ->
|
2017-03-31 02:42:13 +04:00
|
|
|
let parsed_config_file, block = Client_config.preparse_args Sys.argv in
|
2017-04-05 01:35:41 +04:00
|
|
|
let rpc_config : Client_rpcs.config = {
|
|
|
|
Client_rpcs.default_config with
|
|
|
|
host = parsed_config_file.node_addr ;
|
|
|
|
port = parsed_config_file.node_port ;
|
|
|
|
tls = parsed_config_file.tls ;
|
|
|
|
} in
|
|
|
|
begin
|
|
|
|
Client_node_rpcs.Blocks.protocol rpc_config block >>= function
|
|
|
|
| Ok version ->
|
|
|
|
Lwt.return (Some version, Client_commands.commands_for_version version)
|
|
|
|
| Error err ->
|
|
|
|
Format.eprintf
|
|
|
|
"Failed to acquire the protocol version from the node: %a.@."
|
|
|
|
pp_print_error err ;
|
|
|
|
Lwt.return (None, [])
|
2017-03-15 04:17:20 +04:00
|
|
|
end >>= fun (_version, commands_for_version) ->
|
2017-03-13 20:40:51 +04:00
|
|
|
let commands =
|
|
|
|
Client_generic_rpcs.commands @
|
|
|
|
Client_network.commands () @
|
|
|
|
Client_keys.commands () @
|
|
|
|
Client_protocols.commands () @
|
|
|
|
Client_helpers.commands () @
|
|
|
|
commands_for_version in
|
2017-03-31 02:42:13 +04:00
|
|
|
let (command, parsed_args) =
|
|
|
|
Client_config.parse_args
|
|
|
|
(Cli_entries.usage ~commands)
|
|
|
|
(Cli_entries.inline_dispatch commands)
|
|
|
|
Sys.argv in
|
|
|
|
let config : Client_commands.cfg = {
|
|
|
|
base_dir = parsed_config_file.base_dir ;
|
|
|
|
force = parsed_args.force ;
|
|
|
|
block ;
|
|
|
|
web_port = Client_commands.default_cfg.web_port ;
|
|
|
|
} in
|
2017-04-05 01:35:41 +04:00
|
|
|
let rpc_config =
|
|
|
|
if parsed_args.print_timings then
|
|
|
|
{ rpc_config with
|
|
|
|
logger = Client_rpcs.timings_logger Format.err_formatter }
|
|
|
|
else
|
|
|
|
rpc_config
|
|
|
|
in
|
|
|
|
command (cctxt config rpc_config) >>= function
|
|
|
|
| Ok () ->
|
|
|
|
Lwt.return 0
|
2017-04-05 12:22:41 +04:00
|
|
|
| Error [Cli_entries.Command_not_found] ->
|
|
|
|
Format.eprintf "Unknown command, try `-help`.@." ;
|
|
|
|
Lwt.return 1
|
|
|
|
| Error [Cli_entries.Bad_argument (idx, _n, v)] ->
|
|
|
|
Format.eprintf "There's a problem with argument %d, %s.@." idx v ;
|
|
|
|
Lwt.return 1
|
|
|
|
| Error [Cli_entries.Command_failed message] ->
|
|
|
|
Format.eprintf "Command failed, %s.@." message ;
|
|
|
|
Lwt.return 1
|
2017-04-05 01:35:41 +04:00
|
|
|
| Error err ->
|
|
|
|
Format.eprintf "Error: %a@." pp_print_error err ;
|
|
|
|
Lwt.return 1
|
2017-03-13 20:40:51 +04:00
|
|
|
end begin function
|
|
|
|
| Arg.Help help ->
|
|
|
|
Format.printf "%s%!" help ;
|
|
|
|
Lwt.return 0
|
|
|
|
| Arg.Bad help ->
|
|
|
|
Format.eprintf "%s%!" help ;
|
|
|
|
Lwt.return 1
|
2017-04-05 12:22:41 +04:00
|
|
|
| Client_commands.Version_not_found ->
|
2017-04-05 01:35:41 +04:00
|
|
|
Format.eprintf "Unknown protocol version, try `list versions`.@." ;
|
2017-03-13 20:40:51 +04:00
|
|
|
Lwt.return 1
|
|
|
|
| Failure message ->
|
2017-04-05 01:35:41 +04:00
|
|
|
Format.eprintf "Fatal error: %s@." message ;
|
2017-03-13 20:40:51 +04:00
|
|
|
Lwt.return 1
|
|
|
|
| exn ->
|
2017-04-05 01:35:41 +04:00
|
|
|
Format.printf "Fatal internal error: %s@."
|
2017-03-13 20:40:51 +04:00
|
|
|
(Printexc.to_string exn) ;
|
|
|
|
Lwt.return 1
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(* Where all the user friendliness starts *)
|
2016-12-02 02:20:23 +04:00
|
|
|
let () = Pervasives.exit (Lwt_main.run (main ()))
|