From 21839f4786fae67204768a790d18a028056e8a31 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Sun, 15 Jan 2012 00:44:31 +0100 Subject: [PATCH] Parse options in htools.hs, not in each program MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch moves the call to getArgs/parseOpts into htools.hs, as opposed to the current situation where htools.hs only calls the main file of each program, which in turn calls these two functions. By doing it only once we remove some duplicate code and we also export each program's options, which allows us to test some common behaviour in the future. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- htools/Ganeti/HTools/Program/Hail.hs | 12 ++++-------- htools/Ganeti/HTools/Program/Hbal.hs | 12 ++++-------- htools/Ganeti/HTools/Program/Hscan.hs | 10 ++++------ htools/Ganeti/HTools/Program/Hspace.hs | 10 +++------- htools/htools.hs | 22 ++++++++++++++-------- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/htools/Ganeti/HTools/Program/Hail.hs b/htools/Ganeti/HTools/Program/Hail.hs index bb8f1fb69..426def4ba 100644 --- a/htools/Ganeti/HTools/Program/Hail.hs +++ b/htools/Ganeti/HTools/Program/Hail.hs @@ -4,7 +4,7 @@ {- -Copyright (C) 2009, 2010, 2011 Google Inc. +Copyright (C) 2009, 2010, 2011, 2012 Google Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,11 +23,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -} -module Ganeti.HTools.Program.Hail (main) where +module Ganeti.HTools.Program.Hail (main, options) where import Control.Monad import Data.Maybe (fromMaybe) -import System.Environment (getArgs) import System.IO import qualified Ganeti.HTools.Cluster as Cluster @@ -50,11 +49,8 @@ options = ] -- | Main function. -main :: IO () -main = do - cmd_args <- getArgs - (opts, args) <- parseOpts cmd_args "hail" options - +main :: Options -> [String] -> IO () +main opts args = do let shownodes = optShowNodes opts verbose = optVerbose opts savecluster = optSaveCluster opts diff --git a/htools/Ganeti/HTools/Program/Hbal.hs b/htools/Ganeti/HTools/Program/Hbal.hs index 689e8aae3..30c3d61e8 100644 --- a/htools/Ganeti/HTools/Program/Hbal.hs +++ b/htools/Ganeti/HTools/Program/Hbal.hs @@ -4,7 +4,7 @@ {- -Copyright (C) 2009, 2010, 2011 Google Inc. +Copyright (C) 2009, 2010, 2011, 2012 Google Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -} -module Ganeti.HTools.Program.Hbal (main) where +module Ganeti.HTools.Program.Hbal (main, options) where import Control.Concurrent (threadDelay) import Control.Exception (bracket) @@ -31,7 +31,6 @@ import Control.Monad import Data.List import Data.Maybe (isJust, isNothing, fromJust) import Data.IORef -import System.Environment (getArgs) import System.Exit import System.IO import System.Posix.Process @@ -338,11 +337,8 @@ checkNeedRebalance opts ini_cv = do exitWith ExitSuccess -- | Main function. -main :: IO () -main = do - cmd_args <- getArgs - (opts, args) <- parseOpts cmd_args "hbal" options - +main :: Options -> [String] -> IO () +main opts args = do unless (null args) $ do hPutStrLn stderr "Error: this program doesn't take any arguments." exitWith $ ExitFailure 1 diff --git a/htools/Ganeti/HTools/Program/Hscan.hs b/htools/Ganeti/HTools/Program/Hscan.hs index cac708f38..1324bd661 100644 --- a/htools/Ganeti/HTools/Program/Hscan.hs +++ b/htools/Ganeti/HTools/Program/Hscan.hs @@ -4,7 +4,7 @@ {- -Copyright (C) 2009, 2010, 2011 Google Inc. +Copyright (C) 2009, 2010, 2011, 2012 Google Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -} -module Ganeti.HTools.Program.Hscan (main) where +module Ganeti.HTools.Program.Hscan (main, options) where import Control.Monad import Data.Maybe (isJust, fromJust, fromMaybe) @@ -125,10 +125,8 @@ writeDataInner nlen name opts cdata fixdata = do return True -- | Main function. -main :: IO () -main = do - cmd_args <- getArgs - (opts, clusters) <- parseOpts cmd_args "hscan" options +main :: Options -> [String] -> IO () +main opts clusters = do let local = "LOCAL" let nlen = if null clusters diff --git a/htools/Ganeti/HTools/Program/Hspace.hs b/htools/Ganeti/HTools/Program/Hspace.hs index 48c6066c7..f4bd0d587 100644 --- a/htools/Ganeti/HTools/Program/Hspace.hs +++ b/htools/Ganeti/HTools/Program/Hspace.hs @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -} -module Ganeti.HTools.Program.Hspace (main) where +module Ganeti.HTools.Program.Hspace (main, options) where import Control.Monad import Data.Char (toUpper, isAlphaNum, toLower) @@ -33,7 +33,6 @@ import Data.Maybe (fromMaybe) import Data.Ord (comparing) import System.Exit import System.IO -import System.Environment (getArgs) import Text.Printf (printf, hPrintf) @@ -392,11 +391,8 @@ instFromSpec spx disk_template = (rspecCpu spx) Running [] True (-1) (-1) disk_template -- | Main function. -main :: IO () -main = do - cmd_args <- getArgs - (opts, args) <- parseOpts cmd_args "hspace" options - +main :: Options -> [String] -> IO () +main opts args = do unless (null args) $ do hPutStrLn stderr "Error: this program doesn't take any arguments." exitWith $ ExitFailure 1 diff --git a/htools/htools.hs b/htools/htools.hs index cf2e4a264..d33b96150 100644 --- a/htools/htools.hs +++ b/htools/htools.hs @@ -4,7 +4,7 @@ {- -Copyright (C) 2011 Google Inc. +Copyright (C) 2011, 2012 Google Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,17 +31,18 @@ import System.Exit import System.IO import Ganeti.HTools.Utils +import Ganeti.HTools.CLI (OptType, Options, parseOpts) import qualified Ganeti.HTools.Program.Hail as Hail import qualified Ganeti.HTools.Program.Hbal as Hbal import qualified Ganeti.HTools.Program.Hscan as Hscan import qualified Ganeti.HTools.Program.Hspace as Hspace -- | Supported binaries. -personalities :: [(String, IO ())] -personalities = [ ("hail", Hail.main) - , ("hbal", Hbal.main) - , ("hscan", Hscan.main) - , ("hspace", Hspace.main) +personalities :: [(String, (Options -> [String] -> IO (), [OptType]))] +personalities = [ ("hail", (Hail.main, Hail.options)) + , ("hbal", (Hbal.main, Hbal.options)) + , ("hscan", (Hscan.main, Hscan.options)) + , ("hspace", (Hspace.main, Hspace.options)) ] -- | Display usage and exit. @@ -59,5 +60,10 @@ main :: IO () main = do binary <- getEnv "HTOOLS" `catch` const getProgName let name = map toLower binary - boolnames = map (\(x, y) -> (x == name, y)) personalities - select (usage name) boolnames + boolnames = map (\(x, y) -> (x == name, Just y)) personalities + case select Nothing boolnames of + Nothing -> usage name + Just (fn, options) -> do + cmd_args <- getArgs + (opts, args) <- parseOpts cmd_args name options + fn opts args -- GitLab