diff options
-rw-r--r-- | src/dune | 8 | ||||
-rw-r--r-- | src/validate.ml | 32 | ||||
-rw-r--r-- | src/vyconf_client_session.ml | 64 | ||||
-rw-r--r-- | src/vyconf_client_session.mli | 16 |
4 files changed, 119 insertions, 1 deletions
@@ -14,7 +14,7 @@ (library (name client) (public_name vyconf.vyconf-client) - (modules vyconf_client) + (modules vyconf_client vyconf_client_session) (libraries vyos1x-config vyconf_connect lwt lwt.unix lwt_log lwt_ppx ocaml-protoc toml sha yojson ppx_deriving.show ppx_deriving_yojson) (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving_yojson))) @@ -33,6 +33,12 @@ (libraries client) (preprocess (pps lwt_ppx))) +(executable + (name validate) + (public_name validate) + (modules validate) + (libraries client)) + (rule (alias protoc) (mode promote) diff --git a/src/validate.ml b/src/validate.ml new file mode 100644 index 0000000..7b3b596 --- /dev/null +++ b/src/validate.ml @@ -0,0 +1,32 @@ +open Client.Vyconf_client_session + +let path_opt = ref "" + +let usage = "Usage: " ^ Sys.argv.(0) ^ " [options]" + +let args = [ + ("--path", Arg.String (fun s -> path_opt := s), "<string> Configuration path"); + ] + +let get_sockname = + "/var/run/vyconfd.sock" + +let main socket path_list = + let token = session_init socket in + match token with + | Error e -> "Failed to initialize session: " ^ e + | Ok token -> + let out = session_validate_path socket token path_list + in + let _ = session_free socket token in + match out with + | Error e -> "Failed to validate path: " ^ e + | Ok _ -> "No error" + +let _ = + let () = Arg.parse args (fun _ -> ()) usage in + let path_list = Vyos1x.Util.list_of_path !path_opt in + let socket = get_sockname in + let result = main socket path_list in + let () = print_endline result in + exit 0 diff --git a/src/vyconf_client_session.ml b/src/vyconf_client_session.ml new file mode 100644 index 0000000..70a2a13 --- /dev/null +++ b/src/vyconf_client_session.ml @@ -0,0 +1,64 @@ +open Vyconf_connect.Vyconf_pbt + +type op_t = + | OpSetupSession + | OpExists + | OpTeardownSession + | OpShowConfig + | OpValidate + +let config_format_of_string s = + match s with + | "curly" -> Curly + | "json" -> Json + | _ -> failwith (Printf.sprintf "Unknown config format %s, should be curly or json" s) + +let output_format_of_string s = + match s with + | "plain" -> Out_plain + | "json" -> Out_json + | _ -> failwith (Printf.sprintf "Unknown output format %s, should be plain or json" s) + +let call_op ?(out_format="plain") ?(config_format="curly") socket token op path = + let config_format = config_format_of_string config_format in + let out_format = output_format_of_string out_format in + let run = + let%lwt client = + Vyconf_client.create ~token:token socket out_format config_format + in + let%lwt result = match op with + | None -> Error "Operation required" |> Lwt.return + | Some o -> + begin + match o with + | OpSetupSession -> + let%lwt resp = Vyconf_client.setup_session client "vyconf_client_session" in + begin + match resp with + | Ok c -> Vyconf_client.get_token c + | Error e -> Error e |> Lwt.return + end + | OpExists -> Vyconf_client.exists client path + | OpTeardownSession -> Vyconf_client.teardown_session client + | OpShowConfig -> Vyconf_client.show_config client path + | OpValidate -> Vyconf_client.validate client path + end + in + Lwt.return result + in + Lwt_main.run run + +let session_init ?(out_format="plain") ?(config_format="curly") socket = + call_op ~out_format:out_format ~config_format:config_format socket None (Some OpSetupSession) [] + +let session_free socket token = + call_op socket (Some token) (Some OpTeardownSession) [] + +let session_validate_path socket token path = + call_op socket (Some token) (Some OpValidate) path + +let session_show_config socket token path = + call_op socket (Some token) (Some OpShowConfig) path + +let session_path_exists socket token path = + call_op socket (Some token) (Some OpExists) path diff --git a/src/vyconf_client_session.mli b/src/vyconf_client_session.mli new file mode 100644 index 0000000..98fa3c2 --- /dev/null +++ b/src/vyconf_client_session.mli @@ -0,0 +1,16 @@ +type op_t = + | OpSetupSession + | OpExists + | OpTeardownSession + | OpShowConfig + | OpValidate + +val session_init : ?out_format:string -> ?config_format:string -> string -> (string, string) result + +val session_free : string -> string -> (string, string) result + +val session_validate_path : string -> string -> string list -> (string, string) result + +val session_show_config : string -> string -> string list -> (string, string) result + +val session_path_exists : string -> string -> string list -> (string, string) result |