Search configuration file in alternative directories.

Commit
e85fa4da3ace9a8f77adddf28609fa943273e641
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
lib/config.ml
index ee0129ce..540a5d00 100644..100644
@@ -5,7 +5,7 @@
5 5 type t = {
6 6 user : string;
7 7 default_branch : string;
8 Removed: repositories_root_path : string;
8 Added: git_project_root : string;
9 9 commits_max_displayed : int;
10 10 }
11 11
@@ -13,12 +13,17 @@
13 13 {
14 14 user = Unix.getlogin ();
15 15 default_branch = "master";
16 Removed: repositories_root_path = Filename.concat (Sys.getenv "HOME") "git.test";
16 Added: git_project_root = Filename.concat (Sys.getenv "HOME") "git.test";
17 17 commits_max_displayed = 10;
18 18 }
19 19
20 Removed: let config_file = Filename.(concat current_dir_name "config.toml")
20 Added: let locate_config_file () =
21 Added: match List.find_map Sys.getenv_opt [ "OGIT_CONFIG"; "XDG_CONFIG_HOME" ] with
22 Added: | Some file -> file
23 Added: | None -> "/etc/ogit/config.toml"
21 24
25 Added: let config_file = locate_config_file ()
26 Added:
22 27 let to_table t =
23 28 let open Types in
24 29 List.map
@@ -26,8 +31,8 @@
26 31 [
27 32 ("user", TString t.user);
28 33 ("default_branch", TString t.default_branch);
29 Removed: ("repositories.root_path", TString t.repositories_root_path);
30 Removed: ("commits.max_displayed", TInt t.commits_max_displayed);
34 Added: ("git_project_root", TString t.git_project_root);
35 Added: ("commits_max_displayed", TInt t.commits_max_displayed);
31 36 ]
32 37 |> Min.of_key_values
33 38
@@ -42,35 +47,27 @@
42 47 | `Error (e, l) ->
43 48 Error (Printf.sprintf "%s: %s at line %d" l.source e l.line)
44 49 | `Ok table ->
45 Removed: let ( let* ) = Result.bind in
46 50 let find_string key =
47 Removed: match Toml.Types.Table.find_opt (Toml.Min.key key) table with
51 Added: match Types.Table.find_opt (Min.key key) table with
48 52 | Some (TString s) -> Ok s
49 53 | Some _ -> Error ("Expected string for key: " ^ key)
50 54 | None -> Error ("Missing key: " ^ key)
51 55 in
52 56 let find_int key =
53 Removed: match Toml.Types.Table.find_opt (Toml.Min.key key) table with
57 Added: match Types.Table.find_opt (Min.key key) table with
54 58 | Some (TInt i) -> Ok i
55 59 | Some _ -> Error ("Expected int for key: " ^ key)
56 60 | None -> Error ("Missing key: " ^ key)
57 61 in
58 Removed: let* repositories_root_path = find_string "repositories_root" in
62 Added: let ( let* ) = Result.bind in
63 Added: let* git_project_root = find_string "git_project_root" in
59 64 let* user = find_string "user" in
60 65 let* default_branch = find_string "default_branch" in
61 66 let* commits_max_displayed = find_int "max_commits_displayed" in
62 Removed: Ok
63 Removed: {
64 Removed: repositories_root_path;
65 Removed: user;
66 Removed: default_branch;
67 Removed: commits_max_displayed;
68 Removed: }
67 Added: Ok { git_project_root; user; default_branch; commits_max_displayed }
69 68 with _ ->
70 69 prerr_endline "[config.ml] Falling back to default config.";
71 70 Ok default
72 71
73 72 let config =
74 Removed: match read_file ~file:config_file () with
75 Removed: | Ok cfg -> cfg
76 Removed: | Error err -> failwith err
73 Added: match read_file ~file:config_file () with Ok cfg -> cfg | Error _ -> default