summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2025-11-17 11:56:58 -0600
committerJohn Estabrook <jestabro@vyos.io>2025-11-20 19:43:13 -0600
commit6716443921891921619ff915b9306fbb9818fcdb (patch)
treeb11e63c8253e65d44fe48367c8659fc38883db49 /src
parent5a41a32b39acbac17a9e80df501195a9b84f4450 (diff)
downloadvyconf-6716443921891921619ff915b9306fbb9818fcdb.tar.gz
vyconf-6716443921891921619ff915b9306fbb9818fcdb.zip
T8009: add executable for replacing retained cli-shell-api calls
For integration with CLI completion and shell operations we call standard options through vyconf_cli_compat.
Diffstat (limited to 'src')
-rw-r--r--src/dune7
-rw-r--r--src/vyconf_cli_compat.ml131
2 files changed, 138 insertions, 0 deletions
diff --git a/src/dune b/src/dune
index 60f1e30..82b95bd 100644
--- a/src/dune
+++ b/src/dune
@@ -56,6 +56,13 @@
(preprocess (pps lwt_ppx)))
(executable
+ (name vyconf_cli_compat)
+ (public_name vyconf_cli_compat)
+ (modules vyconf_cli_compat)
+ (libraries vyconfd_client)
+ (preprocess (pps lwt_ppx)))
+
+(executable
(name validate)
(public_name validate)
(modules validate)
diff --git a/src/vyconf_cli_compat.ml b/src/vyconf_cli_compat.ml
new file mode 100644
index 0000000..eaa3501
--- /dev/null
+++ b/src/vyconf_cli_compat.ml
@@ -0,0 +1,131 @@
+open Vyconfd_client.Vyconf_client
+open Vyconf_connect.Vyconf_pbt
+
+type op_t =
+ | OpSetEditLevel
+ | OpSetEditLevelUp
+ | OpResetEditLevel
+ | OpGetEditLevel
+ | OpEditLevelRoot
+ | OpShowConfig
+ | OpSessionChanged
+
+let op_of_arg s =
+ match s with
+ | "getEditEnv" -> OpSetEditLevel
+ | "getEditUpEnv" -> OpSetEditLevelUp
+ | "getEditResetEnv" -> OpResetEditLevel
+ | "getEditLevelStr" -> OpGetEditLevel
+ | "editLevelAtRoot" -> OpEditLevelRoot
+ | "showCfg" -> OpShowConfig
+ | "sessionChanged" -> OpSessionChanged
+ | _ -> failwith (Printf.sprintf "Unknown operation %s" s)
+
+let in_cli_config_session () =
+ let env = Unix.environment () in
+ let res = Array.find_opt (fun c -> String.starts_with ~prefix:"_OFR_CONFIGURE" c) env
+ in
+ match res with
+ | Some _ -> true
+ | None -> false
+
+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 get_session () =
+ let pid =
+ try (* set for config session *)
+ Int32.of_string (Sys.getenv "SESSION_PID")
+ with Not_found ->
+ Int32.of_int (Unix.getppid())
+ in
+ let user =
+ try Sys.getenv "USER"
+ with Not_found -> ""
+ in
+ let sudo_user =
+ try Sys.getenv "SUDO_USER"
+ with Not_found -> ""
+ in
+ let socket = "/var/run/vyconfd.sock" in
+ let config_format = config_format_of_string "curly" in
+ let out_format = output_format_of_string "plain" in
+ let%lwt client =
+ create socket out_format config_format
+ in
+ let%lwt resp = session_of_pid client pid in
+ match resp with
+ | Error _ -> setup_session client "vyconf_cli_compat" sudo_user user pid
+ | _ as c -> c |> Lwt.return
+
+let close_session () =
+ let%lwt client = get_session () in
+ match client with
+ | Ok c ->
+ teardown_session c
+ | Error e -> Error e |> Lwt.return
+
+let main op path =
+ let%lwt client = get_session () in
+ let%lwt result =
+ match client with
+ | Ok c ->
+ begin
+ match op with
+ | OpSetEditLevel -> set_edit_level c path
+ | OpSetEditLevelUp -> set_edit_level_up c
+ | OpResetEditLevel -> reset_edit_level c
+ | OpGetEditLevel -> get_edit_level c
+ | OpEditLevelRoot -> edit_level_root c
+ | OpShowConfig -> show_config c path
+ | OpSessionChanged -> session_changed c
+ end
+ | Error e -> Error e |> Lwt.return
+ in
+ let () =
+ if not (in_cli_config_session ()) then
+ close_session () |> Lwt.ignore_result
+ in
+ match result with
+ | Ok s ->
+ begin
+ match s with
+ | "" -> Lwt.return 0
+ | _ ->
+ let%lwt () =
+ Lwt_io.write Lwt_io.stdout (Printf.sprintf "%s\n" s) in Lwt.return 0
+ end
+ | Error e ->
+ begin
+ match e with
+ | "" -> Lwt.return 1
+ | _ ->
+ let%lwt () =
+ Lwt_io.write Lwt_io.stderr (Printf.sprintf "%s\n" e) in Lwt.return 1
+ end
+
+let () =
+ if (Array.length Sys.argv) < 2 then
+ let () = print_endline "Must specify operation" in exit 1
+ else
+ let path_list = Array.to_list (Array.sub Sys.argv 2 (Array.length Sys.argv - 2))
+ in
+ let op =
+ try
+ op_of_arg Sys.argv.(1)
+ with Failure msg -> let () = print_endline msg in exit 1
+ in
+ match op, path_list with
+ | OpSetEditLevel, [] ->
+ let () = print_endline "Must specify config path" in exit 1
+ | _, _ ->
+ let result = Lwt_main.run (main op path_list) in exit result