diff options
author | John Estabrook <jestabro@vyos.io> | 2024-10-23 18:50:46 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2024-10-27 21:49:53 -0500 |
commit | 4aee642874a29f4f77704c97286f201d3c4bd2c3 (patch) | |
tree | 0cee5e4c65c1fa18530f01ee98b599d1c2dbea16 /src/adapter/vy_set.ml | |
parent | b8dbd4d03ebb058aaf1e8ddd9261b0628e520e8b (diff) | |
download | vyconf-4aee642874a29f4f77704c97286f201d3c4bd2c3.tar.gz vyconf-4aee642874a29f4f77704c97286f201d3c4bd2c3.zip |
T6718: move vyos1x-adapter into subdirectory
The vyos1x-adapter provides access to the legacy CStore set/delete
functions using ctypes. Developed as a separate package, include as a
subdir, to be retired when full replacements are available.
Diffstat (limited to 'src/adapter/vy_set.ml')
-rw-r--r-- | src/adapter/vy_set.ml | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/adapter/vy_set.ml b/src/adapter/vy_set.ml new file mode 100644 index 0000000..1fb29aa --- /dev/null +++ b/src/adapter/vy_set.ml @@ -0,0 +1,79 @@ +let legacy = ref false +let no_set = ref false +let valid = ref false +let output = ref "" +let path_opt = ref [] + +let usage = "Usage: " ^ Sys.argv.(0) ^ " [options]" + +let read_path p = + path_opt := p::!path_opt + +let speclist = [ + ("--legacy", Arg.Unit (fun _ -> legacy := true), "Use legacy validation"); + ("--no-set", Arg.Unit (fun _ -> no_set := true), "Do not set path"); + ] + +let format_out l = + let fl = List.filter (fun s -> (String.length s) > 0) l in + String.concat "\n\n" fl + +let is_valid v = + match v with + | None -> true + | Some _ -> false + +let valid_err v = + Option.value v ~default:"" + +let () = + let () = Arg.parse speclist read_path usage in + let path_list = List.rev !path_opt in + let () = + if List.length path_list = 0 then + (Printf.printf "no path specified\n"; exit 1) + in + let handle = + if !legacy || not !no_set then + let h = Vyos1x_adapter.cstore_handle_init () in + if not (Vyos1x_adapter.cstore_in_config_session_handle h) then + (Vyos1x_adapter.cstore_handle_free h; + Printf.printf "not in config session\n"; exit 1) + else Some h + else None + in + let valid = + if not !legacy then + Vyos1x_adapter.vyconf_validate_path path_list + else + begin + let out = + match handle with + | Some h -> Vyos1x_adapter.legacy_validate_path h path_list + | None -> "missing session handle" + in + match out with + | "" -> None + | _ -> Some out + end + in + let res = + if not !no_set && (is_valid valid) then + match handle with + | Some h -> + Vyos1x_adapter.cstore_set_path h path_list + | None -> "missing session handle" + else "" + in + let ret = + if (is_valid valid) && (res = "") then 0 + else 1 + in + let output = format_out [(valid_err valid); res] in + let () = + match handle with + | Some h -> Vyos1x_adapter.cstore_handle_free h + | None -> () + in + let () = print_endline output in + exit ret |