summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/config.ml88
-rw-r--r--lib/config_writer.ml3
-rw-r--r--lib/default_config.toml4
-rw-r--r--lib/dune2
-rw-r--r--lib/git_presenters.ml4
-rw-r--r--lib/handlers.ml2
-rw-r--r--lib/views.ml4
7 files changed, 82 insertions, 25 deletions
diff --git a/lib/config.ml b/lib/config.ml
index fb1fc88..ee0129c 100644
--- a/lib/config.ml
+++ b/lib/config.ml
@@ -1,26 +1,76 @@
-let config_file = "default_config.toml"
+(* -*- mode: tuareg; -*- *)
+
+open Toml
type t = {
- repositories_root : string;
user : string;
default_branch : string;
- max_commits_displayed : int;
+ repositories_root_path : string;
+ commits_max_displayed : int;
}
+let default =
+ {
+ user = Unix.getlogin ();
+ default_branch = "master";
+ repositories_root_path = Filename.concat (Sys.getenv "HOME") "git.test";
+ commits_max_displayed = 10;
+ }
+
+let config_file = Filename.(concat current_dir_name "config.toml")
+
+let to_table t =
+ let open Types in
+ List.map
+ (fun (k, v) -> (Min.key k, v))
+ [
+ ("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);
+ ]
+ |> Min.of_key_values
+
+let write_file ?(file = config_file) table =
+ let oc = open_out file in
+ Printer.string_of_table table |> Printf.fprintf oc "%s\n";
+ close_out oc
+
+let read_file ?(file = config_file) () =
+ try
+ match Toml.Parser.from_filename file with
+ | `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
+ | 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
+ | 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* 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;
+ }
+ with _ ->
+ prerr_endline "[config.ml] Falling back to default config.";
+ Ok default
+
let config =
- match Toml.Parser.from_filename config_file with
- | `Error (e, _) -> failwith ("Config parse error: " ^ e)
- | `Ok table ->
- let find key = Toml.Types.Table.find (Toml.Min.key key) table in
- let find_string key =
- match find key with TString s -> s | _ -> raise Not_found
- in
- let find_int key =
- match find key with TInt i -> i | _ -> raise Not_found
- in
- {
- repositories_root = find_string "repositories_root";
- user = find_string "user";
- default_branch = find_string "default_branch";
- max_commits_displayed = find_int "max_commits_displayed";
- }
+ match read_file ~file:config_file () with
+ | Ok cfg -> cfg
+ | Error err -> failwith err
diff --git a/lib/config_writer.ml b/lib/config_writer.ml
new file mode 100644
index 0000000..4a9f594
--- /dev/null
+++ b/lib/config_writer.ml
@@ -0,0 +1,3 @@
+(* -*- mode: tuareg; -*- *)
+
+let () = Config.(default |> to_table |> write_file)
diff --git a/lib/default_config.toml b/lib/default_config.toml
deleted file mode 100644
index 0318182..0000000
--- a/lib/default_config.toml
+++ /dev/null
@@ -1,4 +0,0 @@
-repositories_root = "~/git.test/"
-user = "Marius Peter"
-default_branch = "master"
-max_commits_displayed = 100 \ No newline at end of file
diff --git a/lib/dune b/lib/dune
index 323ea0e..0b351c0 100644
--- a/lib/dune
+++ b/lib/dune
@@ -1,3 +1,5 @@
+;; -*- mode: lisp; -*-
+
(library
(name ogit)
(libraries dream dream-html git-unix toml))
diff --git a/lib/git_presenters.ml b/lib/git_presenters.ml
index 680a16c..a3dbc96 100644
--- a/lib/git_presenters.ml
+++ b/lib/git_presenters.ml
@@ -1,7 +1,9 @@
+(* -*- mode: tuareg; -*- *)
+
module Store = Git_unix.Store
open Config
-let full_path path = Filename.concat config.repositories_root path
+let full_path path = Filename.concat config.repositories_root_path path
let store repo =
let path = Fpath.v @@ full_path repo in
diff --git a/lib/handlers.ml b/lib/handlers.ml
index dadf0e1..6b3df29 100644
--- a/lib/handlers.ml
+++ b/lib/handlers.ml
@@ -1,3 +1,5 @@
+(* -*- mode: tuareg; -*- *)
+
let ( let* ) m f =
let open Lwt.Infix in
m >>= function
diff --git a/lib/views.ml b/lib/views.ml
index 0fc9764..456bbd1 100644
--- a/lib/views.ml
+++ b/lib/views.ml
@@ -1,3 +1,5 @@
+(* -*- mode: tuareg; -*- *)
+
open Config
type head_data = { page_title : string }
@@ -92,7 +94,7 @@ let root () =
title = "Ogit";
subtitle = "Repositories for " ^ config.user;
topnav = HTML.null [];
- content = [ repositories_in config.repositories_root ];
+ content = [ repositories_in config.repositories_root_path ];
}
in
Page.render body_data
Copyright 2019--2025 Marius PETER