Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use YAML for configuration #504

Merged
merged 7 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ $ _build/default/examples/trees.exe
The same thing can also be accomplished using `irmin`, the command-line application installed with `irmin-unix`, by running:

```bash
$ echo "root=." > .irminconfig
$ echo "root: ." > irmin.yml
$ irmin init
$ irmin set foo/bar "testing 123"
$ irmin get foo/bar
```

`.irminconfig` allows for `irmin` flags to be set globally on a per-directory basis. Run `irmin help irminconfig` for further details.
`irmin.yml` allows for `irmin` flags to be set globally on a per-directory basis. Run `irmin help irmin.yml` for further details.

Also see `irmin --help` for list of all commands and either `irmin <command> --help` or `irmin help <command>` for more help with a specific command.

Expand Down
1 change: 1 addition & 0 deletions irmin-unix.opam
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ depends: [
"irmin-fs" {>= "1.3.0"}
"git-unix" {>= "1.11.4"}
"irmin-watcher" {>= "0.2.0"}
"yaml" {>= "0.1.0"}
"alcotest" {test}
"mtime" {test & >= "1.0.0"}
]
Expand Down
37 changes: 17 additions & 20 deletions src/irmin-unix/bin/ir_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -503,33 +503,30 @@ let dot = {
Term.(mk dot $ store $ basename $ depth $ no_dot_call $ full);
}

let irminconfig_man =
let config_man =
let version_string = Printf.sprintf "Irmin %s" Irmin.version in
("irminconfig", 5, "", version_string, "Irmin Manual"), [
("irmin.yml", 5, "", version_string, "Irmin Manual"), [
`S Manpage.s_name;
`P "irminconfig - Specify certain command-line options to save on typing";
`P "irmin.yml";

`S Manpage.s_synopsis;
`P ".irminconfig";
`P "Configure certain command-line options to cut down on mistakes and save on typing";

`S Manpage.s_description;
`P "An $(b,irminconfig) file lets the user specify repetitve command-line options \
in a text file. The $(b,irminconfig) file is only read if it is found in \
the current working directory. Every line is of the form $(i,key)=$(i,value), \
where $(i,key) is one of the following: $(b,contents), $(b,store), $(b,branch), \
$(b,root), $(b,bare), $(b,head), or $(b,uri). These correspond to the irmin \
options of the same names.";
`P "An $(b,irmin.yml) file lets the user specify repetitve command-line options \
in a YAML file. The $(b,irmin.yml) is read by default if it is found in \
the current working directory. The configuration file path can also be set using the \
$(b,--config) command-line flag. \

`S "NOTES";
`P "When specifying a value for the $(b,contents) or $(b,store) options, the \
shortest unique substring starting from index 0 is sufficient. For example, \
\"store=g\" is equivalent to \"store=git\".";
The following keys are allowed: $(b,contents), $(b,store), \
$(b,branch), $(b,root), $(b,bare), $(b,head), or $(b,uri). These correspond to the irmin \
options of the same names.";

`S Manpage.s_examples;
`P "Here is an example $(b,irminconfig) for accessing a local http irmin store. This \
$(b,irminconfig) prevents the user from having to specify the $(b,store) and $(b,uri) \
`P "Here is an example $(b,irmin.yml) for accessing a local http irmin store. This \
$(b,irmin.yml) prevents the user from having to specify the $(b,store) and $(b,uri) \
options for every command.";
`Pre " \\$ cat .irminconfig\n store=http\n uri=http://127.0.0.1:8080";
`Pre " \\$ cat irmin.yml\n store: http\n uri: http://127.0.0.1:8080";
] @ help_sections

(* HELP *)
Expand All @@ -547,13 +544,13 @@ let help = {
let help man_format cmds topic = match topic with
| None -> `Help (`Pager, None)
| Some topic ->
let topics = "irminconfig" :: cmds in
let topics = "irmin.yml" :: cmds in
let conv, _ = Arg.enum (List.rev_map (fun s -> (s, s)) ("topics" :: topics)) in
match conv topic with
| `Error e -> `Error (false, e)
| `Ok t when t = "topics" -> List.iter print_endline topics; `Ok ()
| `Ok t when t = "irminconfig" ->
`Ok (Cmdliner.Manpage.print man_format Format.std_formatter irminconfig_man)
| `Ok t when t = "irmin.yml" ->
`Ok (Cmdliner.Manpage.print man_format Format.std_formatter config_man)
| `Ok t -> `Help (man_format, Some t) in
Term.(ret (mk help $Term.man_format $Term.choice_names $topic))
}
Expand Down
42 changes: 26 additions & 16 deletions src/irmin-unix/bin/ir_resolver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,31 @@ let key k default =

let opt_key k = key k (Irmin.Private.Conf.default k)

let config_path_key =
Irmin.Private.Conf.key
~docs:global_option_section
~docv:"PATH"
~doc:"Allows configuration file to be specified on the command-line"
"config" Irmin.Private.Conf.string "irmin.yml"

let config_term =
let add k v config = Irmin.Private.Conf.add config k v in
let create root bare head level uri =
let create root bare head level uri config_path =
Irmin.Private.Conf.empty
|> add Irmin.Private.Conf.root root
|> add Irmin_git.bare bare
|> add Irmin_git.head head
|> add Irmin_git.level level
|> add Irmin_http.uri uri
|> add config_path_key config_path
in
Term.(const create $
opt_key Irmin.Private.Conf.root $
flag_key Irmin_git.bare $
opt_key Irmin_git.head $
opt_key Irmin_git.level $
opt_key Irmin_http.uri)
opt_key Irmin_http.uri $
opt_key config_path_key)

let mk_contents k: contents = match k with
| `String -> (module Irmin.Contents.String)
Expand Down Expand Up @@ -131,27 +140,27 @@ let store_term =
in
Term.(const create $ store $ contents)

let cfg = ".irminconfig"

type t = S: (module Irmin.S with type t = 'a) * 'a Lwt.t -> t

(* FIXME: use a proper configuration format (toml?) and interface
properly with cmdliner *)
let read_config_file (): t option =
(* Read configuration from a YAML file *)
let read_config_file cfg: t option =
if not (Sys.file_exists cfg) then None
else
let oc = open_in cfg in
let len = in_channel_length oc in
let buf = Bytes.create len in
really_input oc buf 0 len;
let lines = String.cuts ~sep:"\n" (Bytes.to_string buf) in
let lines = List.map (fun s -> String.trim s) lines in
let lines = List.map (fun s -> String.cut ~sep:"=" s) lines in
let lines =
List.fold_left (fun l -> function None -> l | Some x -> x::l) [] lines
let buf = really_input_string oc len in
close_in oc;
let y = match Yaml.of_string buf with
| Ok (`O y) -> y
| _ -> []
in
let string_value = function
| `String s -> s
| _ -> raise Not_found
in
let assoc name fn =
try Some (fn (List.assoc name lines)) with Not_found -> None
try Some (fn (List.assoc name y |> string_value))
with Not_found -> None
in
let contents =
let kind =
Expand Down Expand Up @@ -224,8 +233,9 @@ let store =
in
S ((module S), t)
| None ->
let cfg = Irmin.Private.Conf.get config config_path_key in
(* then look at the config file options *)
match read_config_file () with
match read_config_file cfg with
| Some c -> c
| None ->
let s = mk_store `Git (mk_contents `String) in
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-unix/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
(public_name irmin-unix)
(wrapped false)
(libraries (irmin irmin-fs git irmin-git irmin-http git-unix
irmin-watcher))))
irmin-watcher yaml))))