summaryrefslogtreecommitdiff
path: root/src/session.ml
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-02-16 21:11:21 +0700
committerDaniil Baturin <daniil@baturin.org>2018-02-16 21:11:21 +0700
commite4cac118645ffb290ec78e4cde6c9757219d3a10 (patch)
tree8a29e91560649ae24981a346828206c65c5d3c67 /src/session.ml
parent56130bfe30781c210c7459e5df9afa7d894aeec7 (diff)
downloadvyconf-e4cac118645ffb290ec78e4cde6c9757219d3a10.tar.gz
vyconf-e4cac118645ffb290ec78e4cde6c9757219d3a10.zip
Implement config reading functions and a minimal command line client for using them.
Yes, I hate oversized commits too, but this is hard to avoid sometimes. Adjustments to the Session functions logic required to make it work: Do not try to validate the path. The validation function is geared towards validating _set_ paths, so when path lacks a value, it doesn't work right. We assume that the path has been through set at some point, so if a path currently exists in the config tree, it is also a valid path that can be used for Reference_tree.is_leaf etc.
Diffstat (limited to 'src/session.ml')
-rw-r--r--src/session.ml48
1 files changed, 31 insertions, 17 deletions
diff --git a/src/session.ml b/src/session.ml
index 90ab5c8..a502629 100644
--- a/src/session.ml
+++ b/src/session.ml
@@ -77,27 +77,41 @@ let delete w s path =
{s with proposed_config=config; changeset=(op :: s.changeset)}
let get_value w s path =
- let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in
- if RT.is_leaf w.reference_tree path then
- if not ((RT.is_multi w.reference_tree path) || (RT.is_valueless w.reference_tree path))
- then CT.get_value s.proposed_config path
- else raise (Session_error "This node can have more than one value")
- else raise (Session_error "Cannot get a value of a non-leaf node")
+ if not (Vytree.exists s.proposed_config path) then
+ raise (Session_error ("Path does not exist"))
+ else if not (RT.is_leaf w.reference_tree path) then
+ raise (Session_error "Cannot get a value of a non-leaf node")
+ else if (RT.is_multi w.reference_tree path) then
+ raise (Session_error "This node can have more than one value")
+ else if (RT.is_valueless w.reference_tree path) then
+ raise (Session_error "This node can have more than one value")
+ else CT.get_value s.proposed_config path
let get_values w s path =
- let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in
- if RT.is_leaf w.reference_tree path then
- if RT.is_multi w.reference_tree path
- then CT.get_values s.proposed_config path
- else raise (Session_error "This node can have only one value")
- else raise (Session_error "Cannot get a value of a non-leaf node")
+ if not (Vytree.exists s.proposed_config path) then
+ raise (Session_error ("Path does not exist"))
+ else if not (RT.is_leaf w.reference_tree path) then
+ raise (Session_error "Cannot get a value of a non-leaf node")
+ else if not (RT.is_multi w.reference_tree path) then
+ raise (Session_error "This node can have only one value")
+ else CT.get_values s.proposed_config path
let list_children w s path =
- let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in
- if not (RT.is_leaf w.reference_tree path)
- then Vytree.children_of_path s.proposed_config path
- else raise (Session_error "Cannot list children of a leaf node")
+ if not (Vytree.exists s.proposed_config path) then
+ raise (Session_error ("Path does not exist"))
+ else if (RT.is_leaf w.reference_tree path) then
+ raise (Session_error "Cannot list children of a leaf node")
+ else Vytree.children_of_path s.proposed_config path
let exists w s path =
- let path, _ = RT.validate_path D.(w.dirs.validators) w.reference_tree path in
Vytree.exists s.proposed_config path
+
+let show_config w s path fmt =
+ let open Vyconf_types in
+ if not (Vytree.exists s.proposed_config path) then
+ raise (Session_error ("Path does not exist"))
+ else
+ let node = Vytree.get s.proposed_config path in
+ match fmt with
+ | Curly -> CT.render node
+ | Json -> CT.to_yojson node |> Yojson.Safe.pretty_to_string