diff options
| author | Daniil Baturin <daniil@vyos.io> | 2025-06-05 12:06:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-05 12:06:28 +0100 |
| commit | 30d9df43317ae7395a11c9e8a5414f177cefae79 (patch) | |
| tree | 78575fa91147f497a5db3e99af38fd11c03c6f3d /src/vyconf_cli.ml | |
| parent | 014a390488aa8d6601f907f8a691878f3583b5e8 (diff) | |
| parent | 822583a476aff08b20a0ad4d64845c77e70749d0 (diff) | |
| download | vyconf-30d9df43317ae7395a11c9e8a5414f177cefae79.tar.gz vyconf-30d9df43317ae7395a11c9e8a5414f177cefae79.zip | |
Merge pull request #23 from jestabro/vyconf-cli
T7374: add executable to replace legacy my_* cli tools
Diffstat (limited to 'src/vyconf_cli.ml')
| -rw-r--r-- | src/vyconf_cli.ml | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/vyconf_cli.ml b/src/vyconf_cli.ml new file mode 100644 index 0000000..ded03df --- /dev/null +++ b/src/vyconf_cli.ml @@ -0,0 +1,69 @@ +open Vyconfd_client.Vyconf_client +open Vyconf_connect.Vyconf_pbt + +type op_t = + | OpSet + | OpDelete + | OpDiscard + | OpShowConfig + | OpSessionChanged + +let op_of_string s = + match s with + | "vy_set" -> OpSet + | "vy_delete" -> OpDelete + | "vy_discard" -> OpDiscard + | "vy_show" -> OpShowConfig + | "vy_session_changed" -> OpSessionChanged + | _ -> failwith (Printf.sprintf "Unknown operation %s" s) + +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 = Int32.of_int (Unix.getppid()) 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" pid + | _ as c -> c |> Lwt.return + +let main op path = + let%lwt client = get_session () in + let%lwt result = + match client with + | Ok c -> + begin + match op with + | OpSet -> set c path + | OpDelete -> delete c path + | OpDiscard -> discard c + | OpShowConfig -> show_config c path + | OpSessionChanged -> session_changed c + end + | Error e -> Error e |> Lwt.return + in + match result with + | Ok s -> let%lwt () = Lwt_io.write Lwt_io.stdout s in Lwt.return 0 + | Error e -> let%lwt () = Lwt_io.write Lwt_io.stderr (Printf.sprintf "%s\n" e) in Lwt.return 1 + +let () = + let path_list = Array.to_list (Array.sub Sys.argv 1 (Array.length Sys.argv - 1)) + in + let op_str = FilePath.basename Sys.argv.(0) in + let op = op_of_string op_str in + let result = Lwt_main.run (main op path_list) in exit result |
