Skip to content

Commit 9d755b1

Browse files
author
Elie Canonici Merle
committed
give the option to target only a given access in a base when changing accesses
1 parent b8cfcd1 commit 9d755b1

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

gwpublic/gwaccess.ml

+23-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let input_file list file =
3333
close_in ic
3434

3535
let () =
36+
let target_access = ref None in
3637
let access = ref None in
3738
let list = ref [] in
3839
let bname = ref "" in
@@ -41,10 +42,23 @@ let () =
4142
let access_opt opt doc x =
4243
(opt, Arg.Unit (fun () -> set_access x), " Set access flag to " ^ doc ^ ".")
4344
in
45+
let access_of_string s = match s with
46+
| "public" -> Some Def.Public
47+
| "private" -> Some Def.Private
48+
| "default" -> Some Def.IfTitles
49+
| _ -> None
50+
in
51+
let set_target_access_of_string s =
52+
assert (!target_access = None);
53+
let access_opt = access_of_string s in
54+
assert (Option.is_some access_opt);
55+
target_access := access_opt
56+
in
4457
let speclist =
4558
[ access_opt "--private" "PRIVATE" Def.Private
4659
; access_opt "--public" "PUBLIC" Def.Public
4760
; access_opt "--default" "IFTITLE" Def.IfTitles
61+
; "--target", Arg.String (fun s -> set_target_access_of_string s), "[public|default|private] Proccess only persons with given access"
4862
; "--everybody", Arg.Set everybody, " process the wole base."
4963
; "--key", Arg.String (fun s -> list := s :: !list), "<KEY>"
5064
; "--keys", Arg.String (fun s -> input_file list s), "<FILE> file containing one key per line."
@@ -54,12 +68,17 @@ let () =
5468
let anonfun i = bname := i in
5569
let usage = "Usage: " ^ Sys.argv.(0) ^ " [-everybody] [-ind key] [-list-ind file] base" in
5670
Arg.parse speclist anonfun usage ;
57-
match !bname, !access, !everybody, !list with
58-
| ("", _, _, _) | (_, None, _, _) | (_, _, false, []) ->
71+
match !bname, !access, !everybody, !target_access, !list with
72+
| ("", _, _, _, _) | (_, None, _, _, _) | (_, _, false, None, []) ->
5973
Arg.usage speclist usage ;
6074
exit 2 ;
61-
| (bname, Some access, everybody, list) ->
75+
| (bname, Some access, everybody, target_access, list) ->
6276
Secure.set_base_dir (Filename.dirname bname);
6377
Lock.control_retry (Files.lock_file bname) ~onerror:Lock.print_error_and_exit @@ fun () ->
64-
if everybody then Gwaccess_util.access_everybody access bname
78+
if Option.is_some target_access then
79+
Gwaccess_util.change_only_old_access
80+
~old_access:(Option.get target_access)
81+
~new_access:access
82+
bname
83+
else if everybody then Gwaccess_util.access_everybody access bname
6584
else access_some access bname list

gwpublic/gwaccess_util.ml

+9
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ let access_everybody access bname =
7373
patch_person base p.Def.key_index p
7474
end (Gwdb.persons base) ;
7575
commit_patches base
76+
77+
let change_only_old_access ~old_access ~new_access bname =
78+
let base = Gwdb.open_base bname in
79+
Gwdb.Collection.iter begin fun p ->
80+
if Gwdb.get_access p = old_access then
81+
let p = {(gen_person_of_person p) with Def.access = new_access} in
82+
patch_person base p.Def.key_index p
83+
end (Gwdb.persons base) ;
84+
commit_patches base

0 commit comments

Comments
 (0)