(* mclu: Mini Cloud * Copyright (C) 2014-2015 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) open Utils open Printf let () = Random.self_init () let config_file = let default = try Sys.getenv "MCLU_CONFIG" with Not_found -> Config.sysconfdir // "mclu.conf" in ref default let verbose = ref false let show_version () = printf "%s %s\n" Config.package_name Config.package_version; exit 0 let global_speclist = Arg.align [ "--config-file", Arg.Set_string config_file, "FILE Set configuration file"; "-f", Arg.Set_string config_file, "FILE Set configuration file"; "-v", Arg.Set verbose, " Enable verbose/debugging messages"; "--verbose", Arg.Set verbose, " Enable verbose/debugging messages"; "-V", Arg.Unit show_version, " Display version and exit"; "--version", Arg.Unit show_version, " Display version and exit"; ] let speclist = ref global_speclist let subcommand_run = ref (fun ~verbose _ -> assert false) let anon_fun, get_anon_args = let i = ref 0 in let args = ref [] in let anon_fun arg = incr i; let i = !i in if i = 1 then ( match arg with | "boot" -> speclist := Mclu_boot.get_arg_speclist (); subcommand_run := Mclu_boot.run | "console" -> speclist := Mclu_console.get_arg_speclist (); subcommand_run := Mclu_console.run | "destroy" -> speclist := Mclu_destroy.get_arg_speclist (); subcommand_run := Mclu_destroy.run | "list" -> speclist := Mclu_list.get_arg_speclist (); subcommand_run := Mclu_list.run | "off" -> speclist := Mclu_onoff.get_arg_speclist (); subcommand_run := Mclu_onoff.run ~on:false | "on" -> speclist := Mclu_onoff.get_arg_speclist (); subcommand_run := Mclu_onoff.run ~on:true | "reboot" -> speclist := Mclu_reboot.get_arg_speclist (); subcommand_run := Mclu_reboot.run | "status" -> speclist := Mclu_status.get_arg_speclist (); subcommand_run := Mclu_status.run | "viewer" -> speclist := Mclu_viewer.get_arg_speclist (); subcommand_run := Mclu_viewer.run | _ -> eprintf "mclu: unknown subcommand '%s' For help, use mclu --help or read the mclu(1) man page.\n" arg; exit 1 ) else args := arg :: !args in let get_anon_args () = List.rev !args in anon_fun, get_anon_args let usage_msg = "\ Usage: mclu [-f mclu.conf] [--options] [list|status|boot|...] ... For more help, use mclu --help or read the mclu(1) man page. Options:" let () = (* Parse the command line and subcommand arguments. *) Arg.parse_dynamic speclist anon_fun usage_msg; (* Load the configuration file. *) Mclu_conf.load_configuration !config_file; (* Run the subcommand. *) let verbose = !verbose in !subcommand_run ~verbose (get_anon_args ())