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
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let cctxt =
|
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 ->
|
2016-12-01 21:27:53 +04:00
|
|
|
Lwt_utils.create_dir Client_config.(base_dir#get // "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
|
|
|
|
Client_config.(base_dir#get // "logs" // log // startup)
|
|
|
|
(fun chan -> Lwt_io.write chan msg) in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_commands.make_context 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 () ->
|
|
|
|
Client_config.preparse_args Sys.argv cctxt >>= fun block ->
|
|
|
|
Lwt.catch begin fun () ->
|
|
|
|
Client_node_rpcs.Blocks.protocol cctxt block >>= fun version ->
|
|
|
|
Lwt.return (Some version, Client_commands.commands_for_version version)
|
|
|
|
end begin fun exn ->
|
|
|
|
cctxt.warning
|
|
|
|
"Failed to acquire the protocol version from the node: %s."
|
|
|
|
(match exn with
|
|
|
|
| Failure msg -> msg
|
|
|
|
| exn -> Printexc.to_string exn) >>= fun () ->
|
|
|
|
Lwt.return (None, [])
|
|
|
|
end >>= fun (version, commands_for_version) ->
|
|
|
|
let commands =
|
|
|
|
Client_generic_rpcs.commands @
|
|
|
|
Client_network.commands () @
|
|
|
|
Client_keys.commands () @
|
|
|
|
Client_protocols.commands () @
|
|
|
|
Client_helpers.commands () @
|
|
|
|
commands_for_version in
|
|
|
|
Client_config.parse_args ?version
|
|
|
|
(Cli_entries.usage ~commands)
|
|
|
|
(Cli_entries.inline_dispatch commands)
|
|
|
|
Sys.argv cctxt >>= fun command ->
|
|
|
|
command cctxt >>= fun () ->
|
|
|
|
Lwt.return 0
|
|
|
|
end begin function
|
|
|
|
| Arg.Help help ->
|
|
|
|
Format.printf "%s%!" help ;
|
|
|
|
Lwt.return 0
|
|
|
|
| Arg.Bad help ->
|
|
|
|
Format.eprintf "%s%!" help ;
|
|
|
|
Lwt.return 1
|
|
|
|
| Cli_entries.Command_not_found ->
|
2017-03-14 14:56:24 +04:00
|
|
|
Format.eprintf "Unknown command, try `-help`.\n%!" ;
|
2017-03-13 20:40:51 +04:00
|
|
|
Lwt.return 1
|
|
|
|
| Client_commands.Version_not_found ->
|
2017-03-14 14:56:24 +04:00
|
|
|
Format.eprintf "Unknown protocol version, try `list versions`.\n%!" ;
|
2017-03-13 20:40:51 +04:00
|
|
|
Lwt.return 1
|
|
|
|
| Cli_entries.Bad_argument (idx, _n, v) ->
|
|
|
|
Format.eprintf "There's a problem with argument %d, %s.\n%!" idx v ;
|
|
|
|
Lwt.return 1
|
|
|
|
| Cli_entries.Command_failed message ->
|
|
|
|
Format.eprintf "Command failed, %s.\n%!" message ;
|
|
|
|
Lwt.return 1
|
|
|
|
| Failure message ->
|
|
|
|
Format.eprintf "Fatal error: %s\n%!" message ;
|
|
|
|
Lwt.return 1
|
|
|
|
| exn ->
|
|
|
|
Format.printf "Fatal internal error: %s\n%!"
|
|
|
|
(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 ()))
|