From d66aa238c83c46f22c641e72c092d804c9125c87 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 8 Nov 2012 15:04:10 +0100
Subject: [PATCH] Change type of program options to 'IO [Options]'

Some options have defaults that depend on the environment, and we
could handle these in two ways:

- use a place-holder value (e.g. data X a = Default | Custom a) that
  is later read from the environment
- move the options list to IO monad, where it can read the
  environment, etc.

The second option allows also displaying the actual defaults in the
`--help' output, even though it's not as nice, so I went with it.

This patch only changes the option types, without actually changing
any options yet.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>
---
 htest/Test/Ganeti/HTools/CLI.hs        |  6 ++-
 htools/Ganeti/HTools/Program.hs        |  3 +-
 htools/Ganeti/HTools/Program/Hail.hs   | 15 +++----
 htools/Ganeti/HTools/Program/Hbal.hs   | 57 +++++++++++++-------------
 htools/Ganeti/HTools/Program/Hcheck.hs | 49 +++++++++++-----------
 htools/Ganeti/HTools/Program/Hinfo.hs  | 23 ++++++-----
 htools/Ganeti/HTools/Program/Hscan.hs  | 15 +++----
 htools/Ganeti/HTools/Program/Hspace.hs | 41 +++++++++---------
 htools/htools.hs                       |  3 +-
 9 files changed, 111 insertions(+), 101 deletions(-)

diff --git a/htest/Test/Ganeti/HTools/CLI.hs b/htest/Test/Ganeti/HTools/CLI.hs
index 39e93da9a..a9ae7435b 100644
--- a/htest/Test/Ganeti/HTools/CLI.hs
+++ b/htest/Test/Ganeti/HTools/CLI.hs
@@ -119,8 +119,10 @@ case_wrong_arg =
 -- | Test that all binaries support some common options.
 case_stdopts :: Assertion
 case_stdopts =
-  mapM_ (\(name, (_, o, a)) -> checkEarlyExit defaultOptions name
-                               (o ++ genericOpts) a) Program.personalities
+  mapM_ (\(name, (_, o, a)) -> do
+           o' <- o
+           checkEarlyExit defaultOptions name
+             (o' ++ genericOpts) a) Program.personalities
 
 testSuite "HTools/CLI"
           [ 'prop_parseISpec
diff --git a/htools/Ganeti/HTools/Program.hs b/htools/Ganeti/HTools/Program.hs
index fff678cbe..e47a6bd77 100644
--- a/htools/Ganeti/HTools/Program.hs
+++ b/htools/Ganeti/HTools/Program.hs
@@ -39,7 +39,8 @@ import qualified Ganeti.HTools.Program.Hinfo as Hinfo
 
 -- | Supported binaries.
 personalities :: [(String,
-                   (Options -> [String] -> IO (), [OptType], [ArgCompletion]))]
+                   (Options -> [String] -> IO (), IO [OptType],
+                    [ArgCompletion]))]
 personalities = [ ("hail",   (Hail.main,   Hail.options,   Hail.arguments))
                 , ("hbal",   (Hbal.main,   Hbal.options,   Hbal.arguments))
                 , ("hcheck", (Hcheck.main, Hcheck.options, Hcheck.arguments))
diff --git a/htools/Ganeti/HTools/Program/Hail.hs b/htools/Ganeti/HTools/Program/Hail.hs
index c2f647387..adcddf989 100644
--- a/htools/Ganeti/HTools/Program/Hail.hs
+++ b/htools/Ganeti/HTools/Program/Hail.hs
@@ -43,14 +43,15 @@ import Ganeti.HTools.ExtLoader (maybeSaveData, loadExternalData)
 import Ganeti.Utils
 
 -- | Options list and functions.
-options :: [OptType]
+options :: IO [OptType]
 options =
-  [ oPrintNodes
-  , oSaveCluster
-  , oDataFile
-  , oNodeSim
-  , oVerbose
-  ]
+  return
+    [ oPrintNodes
+    , oSaveCluster
+    , oDataFile
+    , oNodeSim
+    , oVerbose
+    ]
 
 -- | The list of arguments supported by the program.
 arguments :: [ArgCompletion]
diff --git a/htools/Ganeti/HTools/Program/Hbal.hs b/htools/Ganeti/HTools/Program/Hbal.hs
index c0d34f5df..aeeb2b48b 100644
--- a/htools/Ganeti/HTools/Program/Hbal.hs
+++ b/htools/Ganeti/HTools/Program/Hbal.hs
@@ -62,35 +62,36 @@ import qualified Ganeti.Luxi as L
 import Ganeti.Jobs
 
 -- | Options list and functions.
-options :: [OptType]
+options :: IO [OptType]
 options =
-  [ oPrintNodes
-  , oPrintInsts
-  , oPrintCommands
-  , oDataFile
-  , oEvacMode
-  , oRapiMaster
-  , oLuxiSocket
-  , oIAllocSrc
-  , oExecJobs
-  , oGroup
-  , oMaxSolLength
-  , oVerbose
-  , oQuiet
-  , oOfflineNode
-  , oMinScore
-  , oMaxCpu
-  , oMinDisk
-  , oMinGain
-  , oMinGainLim
-  , oDiskMoves
-  , oSelInst
-  , oInstMoves
-  , oDynuFile
-  , oExTags
-  , oExInst
-  , oSaveCluster
-  ]
+  return
+    [ oPrintNodes
+    , oPrintInsts
+    , oPrintCommands
+    , oDataFile
+    , oEvacMode
+    , oRapiMaster
+    , oLuxiSocket
+    , oIAllocSrc
+    , oExecJobs
+    , oGroup
+    , oMaxSolLength
+    , oVerbose
+    , oQuiet
+    , oOfflineNode
+    , oMinScore
+    , oMaxCpu
+    , oMinDisk
+    , oMinGain
+    , oMinGainLim
+    , oDiskMoves
+    , oSelInst
+    , oInstMoves
+    , oDynuFile
+    , oExTags
+    , oExInst
+    , oSaveCluster
+    ]
 
 -- | The list of arguments supported by the program.
 arguments :: [ArgCompletion]
diff --git a/htools/Ganeti/HTools/Program/Hcheck.hs b/htools/Ganeti/HTools/Program/Hcheck.hs
index fb5909478..d8b110092 100644
--- a/htools/Ganeti/HTools/Program/Hcheck.hs
+++ b/htools/Ganeti/HTools/Program/Hcheck.hs
@@ -50,31 +50,32 @@ import Ganeti.HTools.Types
 import Ganeti.Utils
 
 -- | Options list and functions.
-options :: [OptType]
+options :: IO [OptType]
 options =
-  [ oDataFile
-  , oDiskMoves
-  , oDynuFile
-  , oEvacMode
-  , oExInst
-  , oExTags
-  , oIAllocSrc
-  , oInstMoves
-  , oLuxiSocket
-  , oMachineReadable
-  , oMaxCpu
-  , oMaxSolLength
-  , oMinDisk
-  , oMinGain
-  , oMinGainLim
-  , oMinScore
-  , oNoSimulation
-  , oOfflineNode
-  , oQuiet
-  , oRapiMaster
-  , oSelInst
-  , oVerbose
-  ]
+  return
+    [ oDataFile
+    , oDiskMoves
+    , oDynuFile
+    , oEvacMode
+    , oExInst
+    , oExTags
+    , oIAllocSrc
+    , oInstMoves
+    , oLuxiSocket
+    , oMachineReadable
+    , oMaxCpu
+    , oMaxSolLength
+    , oMinDisk
+    , oMinGain
+    , oMinGainLim
+    , oMinScore
+    , oNoSimulation
+    , oOfflineNode
+    , oQuiet
+    , oRapiMaster
+    , oSelInst
+    , oVerbose
+    ]
 
 -- | The list of arguments supported by the program.
 arguments :: [ArgCompletion]
diff --git a/htools/Ganeti/HTools/Program/Hinfo.hs b/htools/Ganeti/HTools/Program/Hinfo.hs
index 367bf0f20..d6140171b 100644
--- a/htools/Ganeti/HTools/Program/Hinfo.hs
+++ b/htools/Ganeti/HTools/Program/Hinfo.hs
@@ -48,18 +48,19 @@ import Ganeti.HTools.Loader
 import Ganeti.Utils
 
 -- | Options list and functions.
-options :: [OptType]
+options :: IO [OptType]
 options =
-  [ oPrintNodes
-  , oPrintInsts
-  , oDataFile
-  , oRapiMaster
-  , oLuxiSocket
-  , oIAllocSrc
-  , oVerbose
-  , oQuiet
-  , oOfflineNode
-  ]
+  return
+    [ oPrintNodes
+    , oPrintInsts
+    , oDataFile
+    , oRapiMaster
+    , oLuxiSocket
+    , oIAllocSrc
+    , oVerbose
+    , oQuiet
+    , oOfflineNode
+    ]
 
 -- | The list of arguments supported by the program.
 arguments :: [ArgCompletion]
diff --git a/htools/Ganeti/HTools/Program/Hscan.hs b/htools/Ganeti/HTools/Program/Hscan.hs
index d184e1ecb..ee6470651 100644
--- a/htools/Ganeti/HTools/Program/Hscan.hs
+++ b/htools/Ganeti/HTools/Program/Hscan.hs
@@ -52,14 +52,15 @@ import Ganeti.Common
 import Ganeti.HTools.CLI
 
 -- | Options list and functions.
-options :: [OptType]
+options :: IO [OptType]
 options =
-  [ oPrintNodes
-  , oOutputDir
-  , oLuxiSocket
-  , oVerbose
-  , oNoHeaders
-  ]
+  return
+    [ oPrintNodes
+    , oOutputDir
+    , oLuxiSocket
+    , oVerbose
+    , oNoHeaders
+    ]
 
 -- | The list of arguments supported by the program.
 arguments :: [ArgCompletion]
diff --git a/htools/Ganeti/HTools/Program/Hspace.hs b/htools/Ganeti/HTools/Program/Hspace.hs
index 6dd490503..a785d5b86 100644
--- a/htools/Ganeti/HTools/Program/Hspace.hs
+++ b/htools/Ganeti/HTools/Program/Hspace.hs
@@ -53,27 +53,28 @@ import Ganeti.HTools.Loader
 import Ganeti.Utils
 
 -- | Options list and functions.
-options :: [OptType]
+options :: IO [OptType]
 options =
-  [ oPrintNodes
-  , oDataFile
-  , oDiskTemplate
-  , oSpindleUse
-  , oNodeSim
-  , oRapiMaster
-  , oLuxiSocket
-  , oIAllocSrc
-  , oVerbose
-  , oQuiet
-  , oOfflineNode
-  , oMachineReadable
-  , oMaxCpu
-  , oMaxSolLength
-  , oMinDisk
-  , oStdSpec
-  , oTieredSpec
-  , oSaveCluster
-  ]
+  return
+    [ oPrintNodes
+    , oDataFile
+    , oDiskTemplate
+    , oSpindleUse
+    , oNodeSim
+    , oRapiMaster
+    , oLuxiSocket
+    , oIAllocSrc
+    , oVerbose
+    , oQuiet
+    , oOfflineNode
+    , oMachineReadable
+    , oMaxCpu
+    , oMaxSolLength
+    , oMinDisk
+    , oStdSpec
+    , oTieredSpec
+    , oSaveCluster
+    ]
 
 -- | The list of arguments supported by the program.
 arguments :: [ArgCompletion]
diff --git a/htools/htools.hs b/htools/htools.hs
index 35dc99d74..1b491e2b4 100644
--- a/htools/htools.hs
+++ b/htools/htools.hs
@@ -56,6 +56,7 @@ main = do
     Nothing -> usage name
     Just (fn, options, arguments) -> do
          cmd_args <- getArgs
-         (opts, args) <- parseOpts cmd_args name (options ++ genericOpts)
+         real_options <- options
+         (opts, args) <- parseOpts cmd_args name (real_options ++ genericOpts)
                            arguments
          fn opts args
-- 
GitLab