diff options
author | Daniil Baturin <daniil@vyos.io> | 2020-05-06 18:50:59 +0300 |
---|---|---|
committer | Daniil Baturin <daniil@vyos.io> | 2020-05-06 18:50:59 +0300 |
commit | ae68e5b11573b1b7dc0eba9d95c7ccbba1f1b23d (patch) | |
tree | 60151af2b20bc83118262a00a4c469934da24e88 /src/validate_value.ml | |
parent | 02f2571c473d073a9370cf1dfaff9fac34470d63 (diff) | |
download | vyos-utils-ae68e5b11573b1b7dc0eba9d95c7ccbba1f1b23d.tar.gz vyos-utils-ae68e5b11573b1b7dc0eba9d95c7ccbba1f1b23d.zip |
Initial import.
Diffstat (limited to 'src/validate_value.ml')
-rw-r--r-- | src/validate_value.ml | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/validate_value.ml b/src/validate_value.ml new file mode 100644 index 0000000..3af58e0 --- /dev/null +++ b/src/validate_value.ml @@ -0,0 +1,42 @@ +type check = Regex of string | Exec of string + +let validate_value value_constraint value = + match value_constraint with + | Regex s -> + (try + let _ = Pcre.exec ~pat:s value in true + with Not_found -> false) + | Exec c -> + (* XXX: Using Unix.system is a bad idea on multiple levels, + especially when the input comes directly from the user... + We should do something about it. + *) + let result = Unix.system (Printf.sprintf "%s %s" c value) in + match result with + | Unix.WEXITED 0 -> true + | Unix.WEXITED 127 -> + let () = Printf.printf "Could not execute validator %s" c in + false + | _ -> false + +let value = ref "" + +let checks = ref [] + +let args = [ + ("--regex", Arg.String (fun s -> checks := (Regex s) :: !checks), "Check the value against a regex"); + ("--exec", Arg.String (fun s -> checks := (Exec s) :: !checks), "Check the value against an external command"); + ("--value", Arg.String (fun s -> value := s), "Value to check"); +] +let usage = Printf.sprintf "Usage: %s [OPTIONS] <number>" Sys.argv.(0) + +let () = Arg.parse args (fun _ -> ()) usage + +let _ = + let value = !value in + let checks = !checks in + match checks with + | [] -> exit 0 + | _ -> + List.iter (fun c -> if (validate_value c value) then exit 0 else ()) checks; + exit 1 |