summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2022-02-08 21:06:58 -0600
committerJohn Estabrook <jestabro@vyos.io>2022-04-21 12:08:03 -0500
commitdd6d808bcbd96a80c63e43fe57a93776ba1ba86e (patch)
treee29ba53c198decdd7cc3bb743f2dbfa7faa3e12e
parentf0f8dfc17cc271ee63e946964ea4a2d17cc71a86 (diff)
downloadvyos1x-config-dd6d808bcbd96a80c63e43fe57a93776ba1ba86e.tar.gz
vyos1x-config-dd6d808bcbd96a80c63e43fe57a93776ba1ba86e.zip
configtree: T4235: render delete commands
(cherry picked from commit cfc2e186822de7f15c15ae5e15f5872714c7dcd7)
-rw-r--r--src/config_tree.ml30
-rw-r--r--src/config_tree.mli3
2 files changed, 22 insertions, 11 deletions
diff --git a/src/config_tree.ml b/src/config_tree.ml
index c85b358..6ae7ad2 100644
--- a/src/config_tree.ml
+++ b/src/config_tree.ml
@@ -1,4 +1,5 @@
type value_behaviour = AddValue | ReplaceValue
+type command = Set | Delete
exception Duplicate_value
exception Node_has_no_value
@@ -21,6 +22,11 @@ let default_data = {
let make name = Vytree.make default_data name
+let op_to_string op =
+ match op with
+ | Set -> "set"
+ | Delete -> "delete"
+
let replace_value node path value =
let data = {default_data with values=[value]} in
Vytree.update node path data
@@ -98,16 +104,20 @@ let is_tag node path =
module Renderer =
struct
(* Rendering configs as set commands *)
- let render_set_path path value =
+ let render_set_path ?(op=Set) path value =
let v = Printf.sprintf "\'%s\'" value in
- List.append path [v] |> String.concat " " |> Printf.sprintf "set %s"
+ List.append path [v] |> String.concat " " |> Printf.sprintf "%s %s" (op_to_string op)
- let rec render_commands path ct =
+ let rec render_commands ?(op=Set) path ct =
let new_path = List.append path [Vytree.name_of_node ct] in
let new_path_str = String.concat " " new_path in
let data = Vytree.data_of_node ct in
(* Get the node comment, if any *)
- let comment = Util.default "" data.comment in
+ let comment =
+ match op with
+ | Set -> Util.default "" data.comment
+ | Delete -> ""
+ in
let comment_cmd = (if comment = "" then "" else Printf.sprintf "comment %s \'%s\'" new_path_str comment) in
let child_names = Vytree.list_children ct in
(* Now handle the different cases for nodes with and without children *)
@@ -120,20 +130,20 @@ struct
match values with
| [] ->
(* Valueless leaf node *)
- String.concat " " new_path |> Printf.sprintf "set %s"
+ String.concat " " new_path |> Printf.sprintf "%s %s" (op_to_string op)
| [v] ->
(* Single value, just one command *)
- render_set_path new_path v
+ render_set_path ~op:op new_path v
| vs ->
(* A leaf node with multiple values *)
- List.map (render_set_path new_path) vs |> String.concat "\n"
+ List.map (render_set_path ~op:op new_path) vs |> String.concat "\n"
end
in
if comment_cmd = "" then cmds else Printf.sprintf "%s\n%s" cmds comment_cmd
| _ :: _ ->
(* A node with children *)
let children = List.map (fun n -> Vytree.get ct [n]) child_names in
- let rendered_children = List.map (render_commands new_path) children in
+ let rendered_children = List.map (render_commands ~op:op new_path) children in
let cmds = String.concat "\n" rendered_children in
if comment_cmd = "" then cmds else Printf.sprintf "%s\n%s" cmds comment_cmd
@@ -239,14 +249,14 @@ module JSONRenderer = struct
Printf.sprintf "{%s}" child_configs
end (* JSONRenderer *)
-let render_commands node path =
+let render_commands ?(op=Set) node path =
let node =
match path with
| [] -> node
| _ -> Vytree.get node path
in
let children = Vytree.children_of_node node in
- let commands = List.map (Renderer.render_commands path) children in
+ let commands = List.map (Renderer.render_commands ~op:op path) children in
String.concat "\n" commands
let render_config = Renderer.render_config
diff --git a/src/config_tree.mli b/src/config_tree.mli
index 730b32f..cf94af7 100644
--- a/src/config_tree.mli
+++ b/src/config_tree.mli
@@ -1,4 +1,5 @@
type value_behaviour = AddValue | ReplaceValue
+type command = Set | Delete
exception Duplicate_value
exception Node_has_no_value
@@ -33,7 +34,7 @@ val set_tag : t -> string list -> bool -> t
val is_tag : t -> string list -> bool
-val render_commands : t -> string list -> string
+val render_commands : ?op:command -> t -> string list -> string
val render_config : t -> string