From 69261aa54286a8c9144437044a9fc9395f9af251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sun, 16 Oct 2016 21:57:56 +0200 Subject: [PATCH] Client: add command "complete". --- src/Makefile | 2 ++ src/client/client_helpers.ml | 35 ++++++++++++++++++++++++++++ src/client/client_helpers.mli | 10 ++++++++ src/client/client_node_rpcs.ml | 2 ++ src/client/client_node_rpcs.mli | 2 ++ src/client_main.ml | 1 + src/node/shell/node_rpc.ml | 5 ++++ src/node/shell/node_rpc_services.ml | 13 +++++++++++ src/node/shell/node_rpc_services.mli | 2 ++ 9 files changed, 72 insertions(+) create mode 100644 src/client/client_helpers.ml create mode 100644 src/client/client_helpers.mli diff --git a/src/Makefile b/src/Makefile index c381fdc1c..748caf687 100644 --- a/src/Makefile +++ b/src/Makefile @@ -345,6 +345,7 @@ CLIENT_LIB_INTFS := \ client/client_version.mli \ client/client_node_rpcs.mli \ client/client_generic_rpcs.mli \ + client/client_helpers.mli \ client/client_aliases.mli \ client/client_keys.mli \ client/client_protocols.mli \ @@ -354,6 +355,7 @@ CLIENT_LIB_IMPLS := \ client/client_config.ml \ client/client_node_rpcs.ml \ client/client_generic_rpcs.ml \ + client/client_helpers.ml \ client/client_aliases.ml \ client/client_keys.ml \ client/client_protocols.ml \ diff --git a/src/client/client_helpers.ml b/src/client/client_helpers.ml new file mode 100644 index 000000000..e5fa85b8d --- /dev/null +++ b/src/client/client_helpers.ml @@ -0,0 +1,35 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2016. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +let () = + let open Cli_entries in + register_group "helpers" "Various helpers" + +let unique = ref false +let unique_arg = + "-unique", + Arg.Set unique, + "Fail when there is more than one possible completion." + +let commands () = Cli_entries.[ + command + ~desc: "Lookup for the possible completion of a \ + given prefix of Base48Check-encoded hash. This actually \ + works only for blocks and operations." + ~args: [unique_arg] + (prefixes [ "complete" ] @@ string "prefix" "the prefix of the Base48Check-encoded hash to be completed" @@ stop) + (fun prefix () -> + Client_node_rpcs.complete prefix >>= fun completions -> + match completions with + | [] -> Pervasives.exit 3 + | _ :: _ :: _ when !unique -> Pervasives.exit 3 + | completions -> + List.iter print_endline completions ; + Lwt.return_unit) +] diff --git a/src/client/client_helpers.mli b/src/client/client_helpers.mli new file mode 100644 index 000000000..2ae45c301 --- /dev/null +++ b/src/client/client_helpers.mli @@ -0,0 +1,10 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2016. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +val commands: unit -> Cli_entries.command list diff --git a/src/client/client_node_rpcs.ml b/src/client/client_node_rpcs.ml index 889bc8f31..f897ebfd4 100644 --- a/src/client/client_node_rpcs.ml +++ b/src/client/client_node_rpcs.ml @@ -150,6 +150,8 @@ let inject_operation ?(wait = true) ?force operation = call_service0 Services.inject_operation (operation, wait, force) let inject_protocol ?(wait = true) ?force protocol = call_service0 Services.inject_protocol (protocol, wait, force) +let complete prefix = + call_service1 Services.complete prefix () let describe ?recurse path = let prefix, arg = RPC.forge_request Services.describe () recurse in get_json (prefix @ path) arg >>= diff --git a/src/client/client_node_rpcs.mli b/src/client/client_node_rpcs.mli index 1a1e9605f..3fe7e5fd6 100644 --- a/src/client/client_node_rpcs.mli +++ b/src/client/client_node_rpcs.mli @@ -104,6 +104,8 @@ module Protocols : sig (Protocol_hash.t * Store.protocol option) list Lwt.t end +val complete: string -> string list Lwt.t + val describe: ?recurse:bool -> string list -> RPC.Description.directory_descr Lwt.t (** Low-level *) diff --git a/src/client_main.ml b/src/client_main.ml index 416f10abd..e57cdd9cf 100644 --- a/src/client_main.ml +++ b/src/client_main.ml @@ -31,6 +31,7 @@ let main () = Client_generic_rpcs.commands @ Client_keys.commands () @ Client_protocols.commands () @ + Client_helpers.commands () @ Client_version.commands_for_version version in Client_config.parse_args ~version (Cli_entries.usage commands) diff --git a/src/node/shell/node_rpc.ml b/src/node/shell/node_rpc.ml index a938ec44c..87ed0191a 100644 --- a/src/node/shell/node_rpc.ml +++ b/src/node/shell/node_rpc.ml @@ -434,6 +434,11 @@ let build_rpc_directory node = let implementation () = RPC.Answer.return Data_encoding.Json.(schema (Error_monad.error_encoding ())) in RPC.register0 dir RPC.Error.service implementation in + let dir = + RPC.register1 dir Services.complete + (fun s () -> + Base48.decode_partial s >>= fun l -> + RPC.Answer.return (List.map Base48.encode l)) in let dir = RPC.register_describe_directory_service dir Services.describe in dir diff --git a/src/node/shell/node_rpc_services.ml b/src/node/shell/node_rpc_services.ml index b4d3332e4..c85c28283 100644 --- a/src/node/shell/node_rpc_services.ml +++ b/src/node/shell/node_rpc_services.ml @@ -583,6 +583,19 @@ let inject_protocol = (obj1 (req "injectedProtocol" Protocol_hash.encoding))) RPC.Path.(root / "inject_protocol") +let complete = + let prefix_arg = + let destruct s = Ok s + and construct s = s in + RPC.Arg.make ~name:"prefix" ~destruct ~construct () in + RPC.service + ~description: "Try to complete a prefix of a Base48Check-encoded data. \ + This RPC is actually able to complete hashes of \ + black and hashes of operations." + ~input: empty + ~output: (list string) + RPC.Path.(root / "complete" /: prefix_arg ) + let describe = RPC.Description.service ~description: "RPCs documentation and input/output schema" diff --git a/src/node/shell/node_rpc_services.mli b/src/node/shell/node_rpc_services.mli index 055de2ec6..ca0edb4e2 100644 --- a/src/node/shell/node_rpc_services.mli +++ b/src/node/shell/node_rpc_services.mli @@ -132,5 +132,7 @@ val inject_protocol: (unit, unit, (Store.protocol * bool * bool option), Protocol_hash.t tzresult) RPC.service +val complete: (unit, unit * string, unit, string list) RPC.service + val describe: (unit, unit, bool option, RPC.Description.directory_descr) RPC.service