diff options
| author | Marius Peter <marius.peter@tutanota.com> | 2025-05-27 23:25:39 +0200 | 
|---|---|---|
| committer | Marius Peter <marius.peter@tutanota.com> | 2025-05-27 23:25:39 +0200 | 
| commit | e85fa4da3ace9a8f77adddf28609fa943273e641 (patch) | |
| tree | 814c5f60a331404d408ef74deb57033bb3e655b3 /lib | |
| parent | b05013516d858af43f3429683e52adfc4447e896 (diff) | |
Search configuration file in alternative directories.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/config.ml | 35 | 
1 files changed, 16 insertions, 19 deletions
| diff --git a/lib/config.ml b/lib/config.ml index ee0129c..540a5d0 100644 --- a/lib/config.ml +++ b/lib/config.ml @@ -5,7 +5,7 @@ open Toml  type t = {    user : string;    default_branch : string; -  repositories_root_path : string; +  git_project_root : string;    commits_max_displayed : int;  } @@ -13,11 +13,16 @@ let default =    {      user = Unix.getlogin ();      default_branch = "master"; -    repositories_root_path = Filename.concat (Sys.getenv "HOME") "git.test"; +    git_project_root = Filename.concat (Sys.getenv "HOME") "git.test";      commits_max_displayed = 10;    } -let config_file = Filename.(concat current_dir_name "config.toml") +let locate_config_file () = +  match List.find_map Sys.getenv_opt [ "OGIT_CONFIG"; "XDG_CONFIG_HOME" ] with +  | Some file -> file +  | None -> "/etc/ogit/config.toml" + +let config_file = locate_config_file ()  let to_table t =    let open Types in @@ -26,8 +31,8 @@ let to_table t =      [        ("user", TString t.user);        ("default_branch", TString t.default_branch); -      ("repositories.root_path", TString t.repositories_root_path); -      ("commits.max_displayed", TInt t.commits_max_displayed); +      ("git_project_root", TString t.git_project_root); +      ("commits_max_displayed", TInt t.commits_max_displayed);      ]    |> Min.of_key_values @@ -42,35 +47,27 @@ let read_file ?(file = config_file) () =      | `Error (e, l) ->          Error (Printf.sprintf "%s: %s at line %d" l.source e l.line)      | `Ok table -> -        let ( let* ) = Result.bind in          let find_string key = -          match Toml.Types.Table.find_opt (Toml.Min.key key) table with +          match Types.Table.find_opt (Min.key key) table with            | Some (TString s) -> Ok s            | Some _ -> Error ("Expected string for key: " ^ key)            | None -> Error ("Missing key: " ^ key)          in          let find_int key = -          match Toml.Types.Table.find_opt (Toml.Min.key key) table with +          match Types.Table.find_opt (Min.key key) table with            | Some (TInt i) -> Ok i            | Some _ -> Error ("Expected int for key: " ^ key)            | None -> Error ("Missing key: " ^ key)          in -        let* repositories_root_path = find_string "repositories_root" in +        let ( let* ) = Result.bind in +        let* git_project_root = find_string "git_project_root" in          let* user = find_string "user" in          let* default_branch = find_string "default_branch" in          let* commits_max_displayed = find_int "max_commits_displayed" in -        Ok -          { -            repositories_root_path; -            user; -            default_branch; -            commits_max_displayed; -          } +        Ok { git_project_root; user; default_branch; commits_max_displayed }    with _ ->      prerr_endline "[config.ml] Falling back to default config.";      Ok default  let config = -  match read_file ~file:config_file () with -  | Ok cfg -> cfg -  | Error err -> failwith err +  match read_file ~file:config_file () with Ok cfg -> cfg | Error _ -> default | 
