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 - Local Storage for Configuration *)
|
|
|
|
|
2017-04-05 01:35:41 +04:00
|
|
|
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
|
2016-12-03 16:05:02 +04:00
|
|
|
val of_source :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> t tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val to_source :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
t -> string tzresult Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
val name : string
|
|
|
|
end
|
|
|
|
|
|
|
|
module type Alias = sig
|
|
|
|
type t
|
2016-12-03 16:05:02 +04:00
|
|
|
val load :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
(string * t) list tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val find :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> t tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val find_opt :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> t option tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val rev_find :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
t -> string option tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val name :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
t -> string tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val mem :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> bool tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val add :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> t -> unit tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val del :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> unit tzresult Lwt.t
|
2017-03-15 04:27:34 +04:00
|
|
|
val update :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> t -> unit tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val save :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
(string * t) list -> unit tzresult Lwt.t
|
2017-03-15 04:27:34 +04:00
|
|
|
val of_source :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
string -> t tzresult Lwt.t
|
2016-12-03 16:05:02 +04:00
|
|
|
val to_source :
|
|
|
|
Client_commands.context ->
|
2017-04-05 03:02:10 +04:00
|
|
|
t -> string tzresult Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
val alias_param :
|
2016-11-22 17:23:40 +04:00
|
|
|
?name:string ->
|
2016-09-08 21:13:10 +04:00
|
|
|
?desc:string ->
|
2016-12-03 16:05:02 +04:00
|
|
|
('a, Client_commands.context, 'ret) Cli_entries.params ->
|
|
|
|
(string * t -> 'a, Client_commands.context, 'ret) Cli_entries.params
|
2016-09-08 21:13:10 +04:00
|
|
|
val fresh_alias_param :
|
2016-11-22 17:23:40 +04:00
|
|
|
?name:string ->
|
2016-09-08 21:13:10 +04:00
|
|
|
?desc:string ->
|
2016-12-03 16:05:02 +04:00
|
|
|
('a, Client_commands.context, 'ret) Cli_entries.params ->
|
|
|
|
(string -> 'a, Client_commands.context, 'ret) Cli_entries.params
|
2016-09-08 21:13:10 +04:00
|
|
|
val source_param :
|
2016-11-22 17:23:40 +04:00
|
|
|
?name:string ->
|
2016-09-08 21:13:10 +04:00
|
|
|
?desc:string ->
|
2016-12-03 16:05:02 +04:00
|
|
|
('a, Client_commands.context, 'ret) Cli_entries.params ->
|
|
|
|
(t -> 'a, Client_commands.context, 'ret) Cli_entries.params
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Alias = functor (Entity : Entity) -> struct
|
|
|
|
|
2017-03-15 04:17:20 +04:00
|
|
|
open Client_commands
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
list (obj2
|
|
|
|
(req "name" string)
|
|
|
|
(req "value" Entity.encoding))
|
|
|
|
|
2017-03-15 04:17:20 +04:00
|
|
|
let dirname cctxt =
|
|
|
|
cctxt.config.base_dir
|
|
|
|
|
|
|
|
let filename cctxt =
|
|
|
|
Filename.concat (dirname cctxt) (Entity.name ^ "s")
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let load cctxt =
|
2017-03-15 04:17:20 +04:00
|
|
|
let filename = filename cctxt in
|
2017-04-05 03:02:10 +04:00
|
|
|
if not (Sys.file_exists filename) then
|
|
|
|
return []
|
|
|
|
else
|
|
|
|
Data_encoding_ezjsonm.read_file filename
|
|
|
|
|> generic_trace
|
|
|
|
"couldn't to read the %s alias file" Entity.name >>=? fun json ->
|
|
|
|
match Data_encoding.Json.destruct encoding json with
|
|
|
|
| exception _ -> (* TODO print_error *)
|
|
|
|
failwith "didn't understand the %s alias file" Entity.name
|
|
|
|
| list ->
|
|
|
|
return list
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let find_opt cctxt name =
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? fun list ->
|
|
|
|
try return (Some (List.assoc name list))
|
|
|
|
with Not_found -> return None
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let find cctxt name =
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? fun list ->
|
|
|
|
try return (List.assoc name list)
|
2016-12-03 16:05:02 +04:00
|
|
|
with Not_found ->
|
2017-04-05 03:02:10 +04:00
|
|
|
failwith "no %s alias named %s" Entity.name name
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let rev_find cctxt v =
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? 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
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let mem cctxt name =
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? fun list ->
|
2016-09-08 21:13:10 +04:00
|
|
|
try
|
|
|
|
ignore (List.assoc name list) ;
|
2017-04-05 03:02:10 +04:00
|
|
|
return true
|
2016-09-08 21:13:10 +04:00
|
|
|
with
|
2017-04-05 03:02:10 +04:00
|
|
|
| Not_found -> return false
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let save cctxt list =
|
2017-04-05 01:35:41 +04:00
|
|
|
Lwt.catch
|
2016-09-08 21:13:10 +04:00
|
|
|
(fun () ->
|
2017-03-15 04:17:20 +04:00
|
|
|
let dirname = dirname cctxt in
|
2017-04-05 03:02:10 +04:00
|
|
|
Lwt_utils.create_dir dirname >>= fun () ->
|
2017-03-15 04:17:20 +04:00
|
|
|
let filename = filename cctxt in
|
2016-09-08 21:13:10 +04:00
|
|
|
let json = Data_encoding.Json.construct encoding list in
|
2017-04-05 03:02:10 +04:00
|
|
|
Data_encoding_ezjsonm.write_file filename json)
|
|
|
|
(fun exn -> Lwt.return (error_exn exn))
|
|
|
|
|> generic_trace "could not write the %s alias file." Entity.name
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let add cctxt name value =
|
2016-09-08 21:13:10 +04:00
|
|
|
let keep = ref false in
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? fun list ->
|
|
|
|
begin
|
|
|
|
if cctxt.config.force then
|
|
|
|
return ()
|
|
|
|
else
|
|
|
|
iter_s (fun (n, v) ->
|
|
|
|
if n = name && v = value then begin
|
|
|
|
keep := true ;
|
2017-04-05 01:35:41 +04:00
|
|
|
cctxt.message
|
2017-04-05 03:02:10 +04:00
|
|
|
"The %s alias %s already exists with the same value."
|
|
|
|
Entity.name n >>= fun () ->
|
|
|
|
return ()
|
|
|
|
end else if n = name && v <> value then begin
|
|
|
|
failwith
|
|
|
|
"another %s is already aliased as %s, \
|
|
|
|
use -force true to update"
|
|
|
|
Entity.name n
|
|
|
|
end else if n <> name && v = value then begin
|
|
|
|
failwith
|
|
|
|
"this %s is already aliased as %s, \
|
|
|
|
use -force true 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
|
2017-04-05 03:02:10 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
2017-04-05 03:02:10 +04:00
|
|
|
save cctxt list >>=? fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.Client_commands.message
|
2017-04-05 03:02:10 +04:00
|
|
|
"New %s alias '%s' saved." Entity.name name >>= fun () ->
|
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let del cctxt name =
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? fun list ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let list = List.filter (fun (n, _) -> n <> name) list in
|
2016-12-03 16:05:02 +04:00
|
|
|
save cctxt list
|
2017-03-15 04:27:34 +04:00
|
|
|
|
|
|
|
let update cctxt name value =
|
2017-04-05 03:02:10 +04:00
|
|
|
load cctxt >>=? fun list ->
|
2017-03-15 04:27:34 +04:00
|
|
|
let list =
|
|
|
|
List.map
|
|
|
|
(fun (n, v) -> (n, if n = name then value else v))
|
|
|
|
list in
|
|
|
|
save cctxt list
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let save cctxt list =
|
2017-04-05 03:02:10 +04:00
|
|
|
save cctxt list >>=? fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.Client_commands.message
|
2017-04-05 03:02:10 +04:00
|
|
|
"Successful update of the %s alias file." Entity.name >>= fun () ->
|
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
include Entity
|
|
|
|
|
2017-04-05 03:02:10 +04:00
|
|
|
let alias_param
|
|
|
|
?(name = "name") ?(desc = "existing " ^ Entity.name ^ " alias") next =
|
2016-11-22 17:23:40 +04:00
|
|
|
param ~name ~desc
|
2017-04-05 03:02:10 +04:00
|
|
|
(fun cctxt s ->
|
2017-04-05 12:22:41 +04:00
|
|
|
find cctxt s >>=? fun v ->
|
|
|
|
return (s, v))
|
2016-11-22 17:23:40 +04:00
|
|
|
next
|
|
|
|
|
2017-04-05 03:02:10 +04:00
|
|
|
let fresh_alias_param
|
|
|
|
?(name = "new") ?(desc = "new " ^ Entity.name ^ " alias") next =
|
2016-11-22 17:23:40 +04:00
|
|
|
param ~name ~desc
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt s ->
|
2017-04-05 03:02:10 +04:00
|
|
|
begin
|
|
|
|
load cctxt >>=? fun list ->
|
|
|
|
begin
|
|
|
|
if cctxt.config.force then
|
|
|
|
return ()
|
|
|
|
else
|
|
|
|
iter_s
|
|
|
|
(fun (n, _v) ->
|
|
|
|
if n = s then
|
|
|
|
failwith
|
|
|
|
"the %s alias %s already exists, use -force true to update"
|
|
|
|
Entity.name n
|
|
|
|
else
|
|
|
|
return ())
|
|
|
|
list
|
|
|
|
end
|
2017-04-05 12:22:41 +04:00
|
|
|
end >>=? fun () ->
|
|
|
|
return s)
|
2016-11-22 17:23:40 +04:00
|
|
|
next
|
|
|
|
|
2016-12-05 16:18:12 +04:00
|
|
|
let source_param ?(name = "src") ?(desc = "source " ^ Entity.name) next =
|
2016-11-22 17:23:40 +04:00
|
|
|
let desc =
|
|
|
|
desc ^ "\n"
|
2016-11-22 20:33:17 +04:00
|
|
|
^ "can be an alias, file or literal (autodetected in this order)\n\
|
|
|
|
use 'file:path', 'text:literal' or 'alias:name' to force" in
|
2016-11-22 17:23:40 +04:00
|
|
|
param ~name ~desc
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt s ->
|
2016-11-22 17:23:40 +04:00
|
|
|
let read path =
|
2017-04-05 01:35:41 +04:00
|
|
|
Lwt.catch
|
2017-04-05 03:02:10 +04:00
|
|
|
(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 Utils.split ~limit:1 ':' s with
|
|
|
|
| [ "alias" ; alias ]->
|
|
|
|
find cctxt alias
|
|
|
|
| [ "text" ; text ] ->
|
|
|
|
of_source cctxt text
|
|
|
|
| [ "file" ; path ] ->
|
|
|
|
read path
|
|
|
|
| _ ->
|
|
|
|
find cctxt s >>= function
|
|
|
|
| Ok v -> return v
|
|
|
|
| Error _ ->
|
|
|
|
read s >>= function
|
|
|
|
| Ok v -> return v
|
|
|
|
| Error _ -> of_source cctxt s
|
2017-04-05 12:22:41 +04:00
|
|
|
end)
|
2016-11-22 17:23:40 +04:00
|
|
|
next
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let name cctxt d =
|
2017-04-05 03:02:10 +04:00
|
|
|
rev_find cctxt d >>=? function
|
2016-12-03 16:05:02 +04:00
|
|
|
| None -> Entity.to_source cctxt d
|
2017-04-05 03:02:10 +04:00
|
|
|
| Some name -> return name
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
end
|