From 3765c394771dfecb607ac6ec2f183c1609de3c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 10 Apr 2018 14:55:32 +0200 Subject: [PATCH] Client: fix for issue #161. Use proper parsing for `--delegate` argument. --- src/lib_client_base/client_aliases.ml | 93 ++++++++++++------- src/lib_client_base/client_aliases.mli | 5 + .../lib_client/client_proto_args.ml | 4 +- .../lib_client/client_proto_args.mli | 2 +- .../client_proto_context_commands.ml | 6 -- 5 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/lib_client_base/client_aliases.ml b/src/lib_client_base/client_aliases.ml index ed5264104..e019c760c 100644 --- a/src/lib_client_base/client_aliases.ml +++ b/src/lib_client_base/client_aliases.ml @@ -79,6 +79,11 @@ module type Alias = sig ?desc:string -> ('a, (#Client_context.wallet as 'obj)) Clic.params -> (t -> 'a, 'obj) Clic.params + val source_arg : + ?long:string -> + ?placeholder:string -> + ?doc:string -> + unit -> (t option, (#Client_context.wallet as 'obj)) Clic.arg val autocomplete: #Client_context.wallet -> string list tzresult Lwt.t end @@ -218,6 +223,40 @@ module Alias = functor (Entity : Entity) -> struct (parameter (fun (_ : < .. >) s -> return @@ Fresh s)) next + let parse_source_string cctxt s = + let read path = + Lwt.catch + (fun () -> + Lwt_io.(with_file ~mode:Input path read) >>= fun content -> + return content) + (fun exn -> + failwith + "cannot read file (%s)" (Printexc.to_string exn)) + >>=? fun content -> + of_source content in + begin + match String.split ~limit:1 ':' s with + | [ "alias" ; alias ]-> + find cctxt alias + | [ "text" ; text ] -> + of_source text + | [ "file" ; path ] -> + read path + | _ -> + find cctxt s >>= function + | Ok v -> return v + | Error a_errs -> + read s >>= function + | Ok v -> return v + | Error r_errs -> + of_source s >>= function + | Ok v -> return v + | Error s_errs -> + let all_errs = + List.flatten [ a_errs ; r_errs ; s_errs ] in + Lwt.return (Error all_errs) + end + let source_param ?(name = "src") ?(desc = "source " ^ Entity.name) next = let desc = Format.asprintf @@ -230,41 +269,29 @@ module Alias = functor (Entity : Entity) -> struct autodetect." desc Entity.name Entity.name Entity.name Entity.name Entity.name in param ~name ~desc - (parameter (fun cctxt s -> - let read path = - Lwt.catch - (fun () -> - Lwt_io.(with_file ~mode:Input path read) >>= fun content -> - return content) - (fun exn -> - failwith - "cannot read file (%s)" (Printexc.to_string exn)) - >>=? fun content -> - of_source content in - begin - match String.split ~limit:1 ':' s with - | [ "alias" ; alias ]-> - find cctxt alias - | [ "text" ; text ] -> - of_source text - | [ "file" ; path ] -> - read path - | _ -> - find cctxt s >>= function - | Ok v -> return v - | Error a_errs -> - read s >>= function - | Ok v -> return v - | Error r_errs -> - of_source s >>= function - | Ok v -> return v - | Error s_errs -> - let all_errs = - List.flatten [ a_errs ; r_errs ; s_errs ] in - Lwt.return (Error all_errs) - end)) + (parameter parse_source_string) next + let source_arg + ?(long = "source " ^ Entity.name) + ?(placeholder = "src") + ?(doc = "") () = + let doc = + Format.asprintf + "%s\n\ + Can be a %s name, a file or a raw %s literal. If the \ + parameter is not the name of an existing %s, the client will \ + look for a file containing a %s, and if it does not exist, \ + the argument will be read as a raw %s.\n\ + Use 'alias:name', 'file:path' or 'text:literal' to disable \ + autodetect." + doc Entity.name Entity.name Entity.name Entity.name Entity.name in + arg + ~long + ~placeholder + ~doc + (parameter parse_source_string) + let force_switch () = Clic.switch ~long:"force" ~short:'f' diff --git a/src/lib_client_base/client_aliases.mli b/src/lib_client_base/client_aliases.mli index 1400cfcbb..319efb758 100644 --- a/src/lib_client_base/client_aliases.mli +++ b/src/lib_client_base/client_aliases.mli @@ -75,6 +75,11 @@ module type Alias = sig ?desc:string -> ('a, (#Client_context.wallet as 'obj)) Clic.params -> (t -> 'a, 'obj) Clic.params + val source_arg : + ?long:string -> + ?placeholder:string -> + ?doc:string -> + unit -> (t option, (#Client_context.wallet as 'obj)) Clic.arg val autocomplete: #Client_context.wallet -> string list tzresult Lwt.t end diff --git a/src/proto_alpha/lib_client/client_proto_args.ml b/src/proto_alpha/lib_client/client_proto_args.ml index 57794e017..29e400e37 100644 --- a/src/proto_alpha/lib_client/client_proto_args.ml +++ b/src/proto_alpha/lib_client/client_proto_args.ml @@ -75,12 +75,12 @@ let arg_arg = string_parameter let delegate_arg = - arg + Client_keys.Public_key_hash.source_arg ~long:"delegate" ~placeholder:"identity" ~doc:"delegate of the contract\n\ Must be a known identity." - string_parameter + () let source_arg = arg diff --git a/src/proto_alpha/lib_client/client_proto_args.mli b/src/proto_alpha/lib_client/client_proto_args.mli index 23d2e00ed..7c514c81e 100644 --- a/src/proto_alpha/lib_client/client_proto_args.mli +++ b/src/proto_alpha/lib_client/client_proto_args.mli @@ -17,7 +17,7 @@ val fee_arg: (Tez.t, Proto_alpha.full) Clic.arg val arg_arg: (string, Proto_alpha.full) Clic.arg val source_arg: (string option, Proto_alpha.full) Clic.arg -val delegate_arg: (string option, Proto_alpha.full) Clic.arg +val delegate_arg: (Ed25519.Public_key_hash.t option, Proto_alpha.full) Clic.arg val delegatable_switch: (bool, Proto_alpha.full) Clic.arg val spendable_switch: (bool, Proto_alpha.full) Clic.arg val max_priority_arg: (int option, Proto_alpha.full) Clic.arg diff --git a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml index 953426732..6111ece7d 100644 --- a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml +++ b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml @@ -16,10 +16,6 @@ open Client_proto_programs open Client_keys open Client_proto_args -let get_pkh cctxt = function - | None -> return None - | Some x -> Public_key_hash.find_opt cctxt x - let report_michelson_errors ?(no_print_source=false) ~msg (cctxt : #Client_context.printer) = function | Error errs -> cctxt#warning "%a" @@ -204,7 +200,6 @@ let commands () = new_contract (_, manager_pkh) balance (_, source) (cctxt : Proto_alpha.full) -> RawContractAlias.of_fresh cctxt force new_contract >>=? fun alias_name -> source_to_keys cctxt cctxt#block source >>=? fun (src_pk, src_sk) -> - get_pkh cctxt delegate >>=? fun delegate -> originate_account ~fee ?delegate @@ -247,7 +242,6 @@ let commands () = RawContractAlias.of_fresh cctxt force alias_name >>=? fun alias_name -> Lwt.return (Micheline_parser.no_parsing_error program) >>=? fun { expanded = code } -> source_to_keys cctxt cctxt#block source >>=? fun (src_pk, src_sk) -> - get_pkh cctxt delegate >>=? fun delegate -> originate_contract ~fee ~delegate ~delegatable ~spendable ~initial_storage ~manager ~balance ~source ~src_pk ~src_sk ~code cctxt >>= fun errors -> report_michelson_errors ~no_print_source ~msg:"origination simulation failed" cctxt errors >>= function