ligo/lib_client_base/client_aliases.ml

273 lines
8.1 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
2017-11-14 03:36:14 +04:00
(* Copyright (c) 2014 - 2017. *)
2016-09-08 21:13:10 +04:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
(* Tezos Command line interface - Local Storage for Configuration *)
open Lwt.Infix
2016-09-08 21:13:10 +04:00
open Cli_entries
module type Entity = sig
type t
val encoding : t Data_encoding.t
val of_source :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> t tzresult Lwt.t
val to_source :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
t -> string tzresult Lwt.t
2016-09-08 21:13:10 +04:00
val name : string
end
module type Alias = sig
type t
2017-11-07 17:23:01 +04:00
type fresh_param
val load :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
(string * t) list tzresult Lwt.t
2017-11-07 20:38:11 +04:00
val set :
#Client_commands.wallet ->
(string * t) list ->
unit tzresult Lwt.t
val find :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> t tzresult Lwt.t
val find_opt :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> t option tzresult Lwt.t
val rev_find :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
t -> string option tzresult Lwt.t
val name :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
t -> string tzresult Lwt.t
val mem :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> bool tzresult Lwt.t
val add :
2017-11-07 17:23:01 +04:00
force:bool ->
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> t -> unit tzresult Lwt.t
val del :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> unit tzresult Lwt.t
val update :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> t -> unit tzresult Lwt.t
val of_source :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
string -> t tzresult Lwt.t
val to_source :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
t -> string tzresult Lwt.t
2016-09-08 21:13:10 +04:00
val alias_param :
?name:string ->
2016-09-08 21:13:10 +04:00
?desc:string ->
2017-11-07 20:38:11 +04:00
('a, (#Client_commands.wallet as 'b), 'ret) Cli_entries.params ->
(string * t -> 'a, 'b, 'ret) Cli_entries.params
2016-09-08 21:13:10 +04:00
val fresh_alias_param :
?name:string ->
2016-09-08 21:13:10 +04:00
?desc:string ->
2017-11-07 20:38:11 +04:00
('a, (< .. > as 'obj), 'ret) Cli_entries.params ->
(fresh_param -> 'a, 'obj, 'ret) Cli_entries.params
2017-11-07 17:23:01 +04:00
val of_fresh :
2017-11-07 20:38:11 +04:00
#Client_commands.wallet ->
2017-11-07 17:23:01 +04:00
bool ->
fresh_param ->
string tzresult Lwt.t
2016-09-08 21:13:10 +04:00
val source_param :
?name:string ->
2016-09-08 21:13:10 +04:00
?desc:string ->
2017-11-07 20:38:11 +04:00
('a, (#Client_commands.wallet as 'obj), 'ret) Cli_entries.params ->
(t -> 'a, 'obj, 'ret) Cli_entries.params
2017-09-27 11:55:20 +04:00
val autocomplete:
2017-11-07 20:38:11 +04:00
#Client_commands.wallet -> string list tzresult Lwt.t
2016-09-08 21:13:10 +04:00
end
module Alias = functor (Entity : Entity) -> struct
open Client_commands
2017-11-07 20:38:11 +04:00
let wallet_encoding : (string * Entity.t) list Data_encoding.encoding =
2016-09-08 21:13:10 +04:00
let open Data_encoding in
list (obj2
(req "name" string)
(req "value" Entity.encoding))
2017-11-07 20:38:11 +04:00
let load (wallet : #wallet) =
wallet#load Entity.name ~default:[] wallet_encoding
2017-11-07 20:38:11 +04:00
let set (wallet : #wallet) entries =
wallet#write Entity.name entries wallet_encoding
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let autocomplete wallet =
load wallet >>= function
2017-09-27 11:55:20 +04:00
| Error _ -> return []
| Ok list -> return (List.map fst list)
2017-11-07 20:38:11 +04:00
let find_opt (wallet : #wallet) name =
load wallet >>=? fun list ->
try return (Some (List.assoc name list))
with Not_found -> return None
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let find (wallet : #wallet) name =
load wallet >>=? fun list ->
try return (List.assoc name list)
with Not_found ->
failwith "no %s alias named %s" Entity.name name
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let rev_find (wallet : #wallet) v =
load wallet >>=? fun list ->
try return (Some (List.find (fun (_, v') -> v = v') list |> fst))
with Not_found -> return None
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let mem (wallet : #wallet) name =
load wallet >>=? fun list ->
2016-09-08 21:13:10 +04:00
try
ignore (List.assoc name list) ;
return true
2016-09-08 21:13:10 +04:00
with
| Not_found -> return false
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let add ~force (wallet : #wallet) name value =
2016-09-08 21:13:10 +04:00
let keep = ref false in
2017-11-07 20:38:11 +04:00
load wallet >>=? fun list ->
begin
2017-11-07 17:23:01 +04:00
if force then
return ()
else
iter_s (fun (n, v) ->
if n = name && v = value then begin
keep := true ;
return ()
end else if n = name && v <> value then begin
failwith
"another %s is already aliased as %s, \
2017-11-07 20:38:11 +04:00
use -force to update"
Entity.name n
end else if n <> name && v = value then begin
failwith
"this %s is already aliased as %s, \
2017-11-07 20:38:11 +04:00
use -force to insert duplicate"
Entity.name n
end else begin
return ()
end)
list
end >>=? fun () ->
2016-09-08 21:13:10 +04:00
let list = List.filter (fun (n, _) -> n <> name) list in
let list = (name, value) :: list in
if !keep then
return ()
2016-09-08 21:13:10 +04:00
else
2017-11-07 20:38:11 +04:00
wallet#write Entity.name list wallet_encoding
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let del (wallet : #wallet) name =
load wallet >>=? fun list ->
2016-09-08 21:13:10 +04:00
let list = List.filter (fun (n, _) -> n <> name) list in
2017-11-07 20:38:11 +04:00
wallet#write Entity.name list wallet_encoding
2017-11-07 20:38:11 +04:00
let update (wallet : #wallet) name value =
load wallet >>=? fun list ->
let list =
List.map
(fun (n, v) -> (n, if n = name then value else v))
list in
2017-11-07 20:38:11 +04:00
wallet#write Entity.name list wallet_encoding
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let save wallet list =
wallet#write Entity.name wallet_encoding list
2016-09-08 21:13:10 +04:00
include Entity
let alias_param
?(name = "name") ?(desc = "existing " ^ Entity.name ^ " alias") next =
param ~name ~desc
2017-11-07 20:38:11 +04:00
(parameter (fun (cctxt : #Client_commands.wallet) s ->
2017-09-27 11:55:20 +04:00
find cctxt s >>=? fun v ->
return (s, v)))
next
2017-11-07 17:23:01 +04:00
type fresh_param = Fresh of string
2017-11-07 20:38:11 +04:00
let of_fresh (wallet : #wallet) force (Fresh s) =
load wallet >>=? fun list ->
2017-11-07 17:23:01 +04:00
begin if force then
return ()
else
iter_s
(fun (n, _v) ->
if n = s then
2017-11-07 20:38:11 +04:00
Entity.to_source wallet _v >>=? fun value ->
2017-11-07 17:23:01 +04:00
failwith
"@[<v 2>The %s alias %s already exists.@,\
The current value is %s.@,\
Use -force to update@]"
Entity.name n
value
else
return ())
list
end >>=? fun () ->
return s
let fresh_alias_param
?(name = "new") ?(desc = "new " ^ Entity.name ^ " alias") next =
param ~name ~desc
2017-11-07 20:38:11 +04:00
(parameter (fun (_ : < .. >) s -> return @@ Fresh s))
next
2016-12-05 16:18:12 +04:00
let source_param ?(name = "src") ?(desc = "source " ^ Entity.name) next =
let desc =
desc ^ "\n"
^ "can be an alias, file or literal (autodetected in this order)\n\
use 'file:path', 'text:literal' or 'alias:name' to force" in
param ~name ~desc
2017-09-27 11:55:20 +04:00
(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 cctxt content in
begin
match String.split ~limit:1 ':' s with
2017-09-27 11:55:20 +04:00
| [ "alias" ; alias ]->
find cctxt alias
| [ "text" ; text ] ->
of_source cctxt 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 cctxt 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))
next
2016-09-08 21:13:10 +04:00
2017-11-07 20:38:11 +04:00
let name (wallet : #wallet) d =
rev_find wallet d >>=? function
| None -> Entity.to_source wallet d
| Some name -> return name
2016-09-08 21:13:10 +04:00
end