{-
-Copyright (C) 2011 Google Inc.
+Copyright (C) 2011, 2012 Google 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
module Main (main) where
+import Control.Exception
+import Control.Monad (guard)
import Data.Char (toLower)
-import System
+import System.Environment
import System.IO
+import System.IO.Error (isDoesNotExistError)
-import Ganeti.HTools.Utils
-import qualified Ganeti.HTools.Program.Hail as Hail
-import qualified Ganeti.HTools.Program.Hbal as Hbal
-import qualified Ganeti.HTools.Program.Hscan as Hscan
-import qualified Ganeti.HTools.Program.Hspace as Hspace
-
--- | Supported binaries.
-personalities :: [(String, IO ())]
-personalities = [ ("hail", Hail.main)
- , ("hbal", Hbal.main)
- , ("hscan", Hscan.main)
- , ("hspace", Hspace.main)
- ]
+import Ganeti.Utils
+import Ganeti.HTools.CLI (parseOpts, genericOpts)
+import Ganeti.HTools.Program (personalities)
-- | Display usage and exit.
usage :: String -> IO ()
hPutStrLn stderr "This program must be installed under one of the following\
\ names:"
mapM_ (hPutStrLn stderr . (" - " ++) . fst) personalities
- hPutStrLn stderr "Please either rename/symlink the program or set\n\
- \the environment variable HTOOLS to the desired role."
- exitWith $ ExitFailure 1
+ exitErr "Please either rename/symlink the program or set\n\
+ \the environment variable HTOOLS to the desired role."
main :: IO ()
main = do
- binary <- getEnv "HTOOLS" `catch` const getProgName
+ binary <- catchJust (guard . isDoesNotExistError)
+ (getEnv "HTOOLS") (const getProgName)
let name = map toLower binary
- boolnames = map (\(x, y) -> (x == name, y)) personalities
- select (usage name) boolnames
+ boolnames = map (\(x, y) -> (x == name, Just y)) personalities
+ case select Nothing boolnames of
+ Nothing -> usage name
+ Just (fn, options, arguments) -> do
+ cmd_args <- getArgs
+ real_options <- options
+ (opts, args) <- parseOpts cmd_args name (real_options ++ genericOpts)
+ arguments
+ fn opts args