Statistics
| Branch: | Tag: | Revision:

root / htools / htools.hs @ 66ad857a

History | View | Annotate | Download (2 kB)

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