Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / CLI.hs @ e0eb63f0

History | View | Annotate | Download (2.4 kB)

1
{-| Implementation of command-line functions.
2

    
3
This module holds the common cli-related functions for the binaries,
4
separated into this module since Utils.hs is used in many other places
5
and this is more IO oriented.
6

    
7
-}
8

    
9
module Ganeti.HTools.CLI
10
    (
11
      parseOpts
12
    , showVersion
13
    , shTemplate
14
    ) where
15

    
16
import System.Console.GetOpt
17
import System.IO
18
import System.Info
19
import System
20
import Monad
21
import Text.Printf (printf)
22
import qualified Data.Version
23

    
24
import qualified Ganeti.HTools.Version as Version(version)
25

    
26
-- | Command line parser, using the 'options' structure.
27
parseOpts :: [String]            -- ^ The command line arguments
28
          -> String              -- ^ The program name
29
          -> [OptDescr (b -> b)] -- ^ The supported command line options
30
          -> b                   -- ^ The default options record
31
          -> (b -> Bool)         -- ^ The function which given the options
32
                                 -- tells us whether we need to show help
33
          -> IO (b, [String])    -- ^ The resulting options a leftover
34
                                 -- arguments
35
parseOpts argv progname options defaultOptions fn =
36
    case getOpt Permute options argv of
37
      (o, n, []) ->
38
          do
39
            let resu@(po, _) = (foldl (flip id) defaultOptions o, n)
40
            when (fn po) $ do
41
              putStr $ usageInfo header options
42
              exitWith ExitSuccess
43
            return resu
44
      (_, _, errs) ->
45
          ioError (userError (concat errs ++ usageInfo header options))
46
      where header = printf "%s %s\nUsage: %s [OPTION...]"
47
                     progname Version.version progname
48

    
49

    
50
-- | Return a version string for the program
51
showVersion :: String -- ^ The program name
52
            -> String -- ^ The formatted version and other information data
53
showVersion name =
54
    printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
55
           name Version.version
56
           compilerName (Data.Version.showVersion compilerVersion)
57
           os arch
58

    
59
-- | A shell script template for autogenerated scripts
60
shTemplate :: String
61
shTemplate =
62
    printf "#!/bin/sh\n\n\
63
           \# Auto-generated script for executing cluster rebalancing\n\n\
64
           \# To stop, touch the file /tmp/stop-htools\n\n\
65
           \set -e\n\n\
66
           \check() {\n\
67
           \  if [ -f /tmp/stop-htools ]; then\n\
68
           \    echo 'Stop requested, exiting'\n\
69
           \    exit 0\n\
70
           \  fi\n\
71
           \}\n\n"