root / Ganeti / HTools / CLI.hs @ e0eb63f0
History | View | Annotate | Download (2.4 kB)
1 | 209b3711 | Iustin Pop | {-| Implementation of command-line functions. |
---|---|---|---|
2 | 209b3711 | Iustin Pop | |
3 | 209b3711 | Iustin Pop | This module holds the common cli-related functions for the binaries, |
4 | 209b3711 | Iustin Pop | separated into this module since Utils.hs is used in many other places |
5 | 6ef35e3c | Iustin Pop | and this is more IO oriented. |
6 | 209b3711 | Iustin Pop | |
7 | 209b3711 | Iustin Pop | -} |
8 | 209b3711 | Iustin Pop | |
9 | 209b3711 | Iustin Pop | module Ganeti.HTools.CLI |
10 | 209b3711 | Iustin Pop | ( |
11 | 209b3711 | Iustin Pop | parseOpts |
12 | 209b3711 | Iustin Pop | , showVersion |
13 | e0eb63f0 | Iustin Pop | , shTemplate |
14 | 209b3711 | Iustin Pop | ) where |
15 | 209b3711 | Iustin Pop | |
16 | 209b3711 | Iustin Pop | import System.Console.GetOpt |
17 | 209b3711 | Iustin Pop | import System.IO |
18 | 209b3711 | Iustin Pop | import System.Info |
19 | 209b3711 | Iustin Pop | import System |
20 | 209b3711 | Iustin Pop | import Monad |
21 | 209b3711 | Iustin Pop | import Text.Printf (printf) |
22 | 209b3711 | Iustin Pop | import qualified Data.Version |
23 | 209b3711 | Iustin Pop | |
24 | 209b3711 | Iustin Pop | import qualified Ganeti.HTools.Version as Version(version) |
25 | 209b3711 | Iustin Pop | |
26 | 209b3711 | Iustin Pop | -- | Command line parser, using the 'options' structure. |
27 | 209b3711 | Iustin Pop | parseOpts :: [String] -- ^ The command line arguments |
28 | 209b3711 | Iustin Pop | -> String -- ^ The program name |
29 | 209b3711 | Iustin Pop | -> [OptDescr (b -> b)] -- ^ The supported command line options |
30 | 209b3711 | Iustin Pop | -> b -- ^ The default options record |
31 | 209b3711 | Iustin Pop | -> (b -> Bool) -- ^ The function which given the options |
32 | 209b3711 | Iustin Pop | -- tells us whether we need to show help |
33 | 209b3711 | Iustin Pop | -> IO (b, [String]) -- ^ The resulting options a leftover |
34 | 209b3711 | Iustin Pop | -- arguments |
35 | 209b3711 | Iustin Pop | parseOpts argv progname options defaultOptions fn = |
36 | 209b3711 | Iustin Pop | case getOpt Permute options argv of |
37 | 209b3711 | Iustin Pop | (o, n, []) -> |
38 | 209b3711 | Iustin Pop | do |
39 | 209b3711 | Iustin Pop | let resu@(po, _) = (foldl (flip id) defaultOptions o, n) |
40 | 209b3711 | Iustin Pop | when (fn po) $ do |
41 | 209b3711 | Iustin Pop | putStr $ usageInfo header options |
42 | 209b3711 | Iustin Pop | exitWith ExitSuccess |
43 | 209b3711 | Iustin Pop | return resu |
44 | 209b3711 | Iustin Pop | (_, _, errs) -> |
45 | 209b3711 | Iustin Pop | ioError (userError (concat errs ++ usageInfo header options)) |
46 | 209b3711 | Iustin Pop | where header = printf "%s %s\nUsage: %s [OPTION...]" |
47 | 209b3711 | Iustin Pop | progname Version.version progname |
48 | 209b3711 | Iustin Pop | |
49 | 209b3711 | Iustin Pop | |
50 | 209b3711 | Iustin Pop | -- | Return a version string for the program |
51 | 209b3711 | Iustin Pop | showVersion :: String -- ^ The program name |
52 | 209b3711 | Iustin Pop | -> String -- ^ The formatted version and other information data |
53 | 209b3711 | Iustin Pop | showVersion name = |
54 | 209b3711 | Iustin Pop | printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n" |
55 | 209b3711 | Iustin Pop | name Version.version |
56 | 209b3711 | Iustin Pop | compilerName (Data.Version.showVersion compilerVersion) |
57 | 209b3711 | Iustin Pop | os arch |
58 | e0eb63f0 | Iustin Pop | |
59 | e0eb63f0 | Iustin Pop | -- | A shell script template for autogenerated scripts |
60 | e0eb63f0 | Iustin Pop | shTemplate :: String |
61 | e0eb63f0 | Iustin Pop | shTemplate = |
62 | e0eb63f0 | Iustin Pop | printf "#!/bin/sh\n\n\ |
63 | e0eb63f0 | Iustin Pop | \# Auto-generated script for executing cluster rebalancing\n\n\ |
64 | e0eb63f0 | Iustin Pop | \# To stop, touch the file /tmp/stop-htools\n\n\ |
65 | e0eb63f0 | Iustin Pop | \set -e\n\n\ |
66 | e0eb63f0 | Iustin Pop | \check() {\n\ |
67 | e0eb63f0 | Iustin Pop | \ if [ -f /tmp/stop-htools ]; then\n\ |
68 | e0eb63f0 | Iustin Pop | \ echo 'Stop requested, exiting'\n\ |
69 | e0eb63f0 | Iustin Pop | \ exit 0\n\ |
70 | e0eb63f0 | Iustin Pop | \ fi\n\ |
71 | e0eb63f0 | Iustin Pop | \}\n\n" |