diff options
author | Daniil Baturin <daniil@baturin.org> | 2016-12-08 02:40:26 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2016-12-08 02:40:26 +0600 |
commit | 658c297efca6deb669062ab8e2582f8abe25373e (patch) | |
tree | 6f015c99b6cb8d031d2a1f5329c37aa63a531d61 /src | |
parent | b12b6ab4ff086383f406d9c00fbe63c6325337a7 (diff) | |
download | vyconf-658c297efca6deb669062ab8e2582f8abe25373e.tar.gz vyconf-658c297efca6deb669062ab8e2582f8abe25373e.zip |
Initial mockup of the vyconfd binary.
Diffstat (limited to 'src')
-rw-r--r-- | src/defaults.ml | 2 | ||||
-rw-r--r-- | src/defaults.mli | 1 | ||||
-rw-r--r-- | src/vyconfd.ml | 60 |
3 files changed, 58 insertions, 5 deletions
diff --git a/src/defaults.ml b/src/defaults.ml index 7bf46a8..f815266 100644 --- a/src/defaults.ml +++ b/src/defaults.ml @@ -1,7 +1,9 @@ type vyconf_defaults = { config_file: string; + version: string } let defaults = { config_file = "/etc/vyconfd.conf"; + version = "0.0.1" } diff --git a/src/defaults.mli b/src/defaults.mli index 48e5cdb..44ed76d 100644 --- a/src/defaults.mli +++ b/src/defaults.mli @@ -1,5 +1,6 @@ type vyconf_defaults = { config_file: string; + version: string } val defaults : vyconf_defaults diff --git a/src/vyconfd.ml b/src/vyconfd.ml index 29345fb..3f80c1d 100644 --- a/src/vyconfd.ml +++ b/src/vyconfd.ml @@ -1,15 +1,65 @@ open Lwt open Defaults +open Vyconf_config let () = Lwt_log.add_rule "*" Lwt_log.Info -let config = - let result = Vyconf_config.load defaults.config_file in +(* Default VyConf configuration *) +let daemonize = ref true +let config_file = ref defaults.config_file +let log_file = ref None +let log_template = ref "$(date): $(message)" + +(* Global data *) + +(* Command line arguments *) +let args = [ + ("--no-daemon", Arg.Unit (fun () -> daemonize := false), + "Do not daemonize"); + ("--config", Arg.String (fun s -> config_file := s), + (Printf.sprintf "<string> Configuration file, default is %s" defaults.config_file)); + ("--log-file", Arg.String (fun s -> log_file := Some s), + "<string> Log file"); + ("--version", Arg.Unit (fun () -> Printf.printf "VyConf version: %s\n" defaults.version; exit 0), "Print version and exit") + ] +let usage = "Usage: " ^ Sys.argv.(0) ^ " [options]" + +let load_config path = + let result = Vyconf_config.load path in match result with - | `Ok cfg -> cfg - | `Error err -> + | Result.Ok cfg -> cfg + | Result.Error err -> Lwt_log.fatal (Printf.sprintf "Could not load the configuration file %s" err); exit 1 +let setup_logger daemonize log_file template = + (* + If log file is specified, log to the file whether we are a daemon or not + If we are a daemon and log file is not specified, log to syslog + If we are not a daemon and log file is not specified, log to stderr + *) + match log_file with + | None -> + if daemonize then + begin + Lwt_log.default := Lwt_log.syslog ~template:template ~facility:`Daemon (); + Lwt.return_unit + end + else + begin + Lwt_log.default := Lwt_log.channel ~template:template ~close_mode:`Keep ~channel:Lwt_io.stderr (); + Lwt.return_unit + end + | Some file -> + let%lwt l = Lwt_log.file ~template:template ~mode:`Append ~file_name:file () in + Lwt_log.default := l; Lwt.return_unit +let main_loop config () = + let%lwt () = setup_logger !daemonize !log_file !log_template in + let%lwt () = Lwt_log.notice @@ Printf.sprintf "Loading %s" config.app_name in + Lwt.return_unit -let () = print_endline "This is VyConf. Or, rather, will be." +let () = + let () = Arg.parse args (fun f -> ()) usage in + let config = load_config !config_file in + Lwt_main.run @@ main_loop config () + |