Client: remove tags

This commit is contained in:
Vincent Bernardoff 2018-06-20 10:50:01 +02:00
parent b022a605b4
commit 4e4d96dae1
5 changed files with 0 additions and 177 deletions

View File

@ -1,84 +0,0 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2018. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
module Tag = struct
type t = string list
let add tags t =
t :: tags
let remove tags t =
let rec aux acc = function
| [] -> raise Not_found
| x :: ts when x = t -> (List.rev acc) @ ts
| x :: ts -> aux (x :: acc) ts
in
aux [] tags
let encoding =
Data_encoding.(list string)
end
module type Entity = sig
val name : string
end
module Tags (Entity : Entity) = struct
include Client_aliases.Alias (struct
type t = Tag.t
let encoding = Tag.encoding
(* Split a string of tags separated by commas, and possibly spaces *)
let of_source tags_str =
let rec aux tags s =
try
let idx = String.index s ',' in
let tag = String.(trim (sub s 0 idx)) in
let tail = String.(sub s (idx + 1) (length s - (idx + 1))) in
aux (tag :: tags) tail
with
| Not_found ->
String.(trim s) :: tags
in
return (aux [] tags_str)
let to_source tags =
return (String.concat ", " tags)
let name = Entity.name ^ " tag"
end)
let tag_param ?(name = "tag") ?(desc = "list of tags") next =
let desc =
desc ^ "\n"
^ "can be one or multiple tags separated by commas" in
Clic.(
param ~name ~desc
(parameter (fun _ s -> of_source s))
next)
let rev_find_by_tag cctxt tag =
load cctxt >>=? fun tags ->
try return (Some (List.find (fun (_, v) -> List.mem tag v) tags |> fst))
with Not_found -> return None
let filter cctxt pred =
load cctxt >>=? fun tags ->
return (List.filter pred tags)
let filter_by_tag cctxt tag =
filter cctxt (fun (_, v) -> List.mem tag v)
end

View File

@ -1,49 +0,0 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2018. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
module Tag : sig
type t = string list
val add: t -> string -> t
val remove: t -> string -> t
val encoding: t Data_encoding.t
end
module type Entity = sig
val name : string
end
module Tags (Entity : Entity) : sig
include Client_aliases.Alias with type t = Tag.t
val tag_param:
?name:string ->
?desc:string ->
('a, 'ctx) Clic.params ->
(Tag.t -> 'a, 'ctx) Clic.params
val rev_find_by_tag:
#Client_context.full ->
string ->
string option tzresult Lwt.t
val filter:
#Client_context.full ->
(string * t -> bool) ->
(string * t) list tzresult Lwt.t
val filter_by_tag:
#Client_context.full ->
string ->
(string * t) list tzresult Lwt.t
end

View File

@ -110,10 +110,6 @@ module ContractAlias = struct
end
module Contract_tags = Client_tags.Tags (struct
let name = "contract"
end)
let list_contracts cctxt =
RawContractAlias.load cctxt >>=? fun raw_contracts ->
Lwt_list.map_s

View File

@ -54,7 +54,3 @@ val get_delegate:
block:Shell_services.block ->
Contract.t ->
public_key_hash option tzresult Lwt.t
module Contract_tags : module type of Client_tags.Tags (struct
let name = "contract"
end)

View File

@ -66,40 +66,4 @@ let commands () =
cctxt#message "%a\n%!" Contract.pp contract >>= fun () ->
return ()) ;
command ~group ~desc: "Tag a contract in the wallet."
no_options
(prefixes [ "tag" ; "contract" ]
@@ RawContractAlias.alias_param
@@ prefixes [ "with" ]
@@ Contract_tags.tag_param
@@ stop)
(fun () (alias, _contract) new_tags cctxt ->
Contract_tags.find_opt cctxt alias >>=? fun tags ->
let new_tags =
match tags with
| None -> new_tags
| Some tags -> List.merge2 tags new_tags in
Contract_tags.update cctxt alias new_tags) ;
command ~group ~desc: "Remove tag(s) from a contract in the wallet."
no_options
(prefixes [ "untag" ; "contract" ]
@@ RawContractAlias.alias_param
@@ prefixes [ "with" ]
@@ Contract_tags.tag_param
@@ stop)
(fun () (alias, _contract) new_tags cctxt ->
Contract_tags.find_opt cctxt alias >>=? fun tags ->
let new_tags =
match tags with
| None -> []
| Some tags ->
List.merge_filter2
~f:(fun x1 x2 -> match x1, x2 with
| None, None -> assert false
| None, Some _ -> None
| Some t1, Some t2 when t1 = t2 -> None
| Some t1, _ -> Some t1) tags new_tags in
Contract_tags.update cctxt alias new_tags) ;
]