Change type of program options to 'IO [Options]'
[ganeti-local] / htools / htools.hs
1 {-| Main htools binary.
2
3 -}
4
5 {-
6
7 Copyright (C) 2011, 2012 Google Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
23
24 -}
25
26 module Main (main) where
27
28 import Control.Exception
29 import Control.Monad (guard)
30 import Data.Char (toLower)
31 import System.Environment
32 import System.IO
33 import System.IO.Error (isDoesNotExistError)
34
35 import Ganeti.Utils
36 import Ganeti.HTools.CLI (parseOpts, genericOpts)
37 import Ganeti.HTools.Program (personalities)
38
39 -- | Display usage and exit.
40 usage :: String -> IO ()
41 usage name = do
42   hPutStrLn stderr $ "Unrecognised personality '" ++ name ++ "'."
43   hPutStrLn stderr "This program must be installed under one of the following\
44                    \ names:"
45   mapM_ (hPutStrLn stderr . ("  - " ++) . fst) personalities
46   exitErr "Please either rename/symlink the program or set\n\
47           \the environment variable HTOOLS to the desired role."
48
49 main :: IO ()
50 main = do
51   binary <- catchJust (guard . isDoesNotExistError)
52             (getEnv "HTOOLS") (const getProgName)
53   let name = map toLower binary
54       boolnames = map (\(x, y) -> (x == name, Just y)) personalities
55   case select Nothing boolnames of
56     Nothing -> usage name
57     Just (fn, options, arguments) -> do
58          cmd_args <- getArgs
59          real_options <- options
60          (opts, args) <- parseOpts cmd_args name (real_options ++ genericOpts)
61                            arguments
62          fn opts args