module Config.Dyre.Options
( removeDyreOptions
, withDyreOptions
, customOptions
, getDenyReconf
, getForceReconf
, getDebug
, getMasterBinary
, getStatePersist
) where
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
import System.IO.Storage (withStore, putValue, getValue, getDefaultValue)
import System.Environment (getArgs, getProgName, withArgs)
import System.Environment.Executable (getExecutablePath)
import Config.Dyre.Params
removeDyreOptions :: [String] -> [String]
removeDyreOptions :: [[Char]] -> [[Char]]
removeDyreOptions = forall a. (a -> Bool) -> [a] -> [a]
filter forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Eq a => [[a]] -> [a] -> Bool
prefixElem [[Char]]
dyreArgs
where prefixElem :: [[a]] -> [a] -> Bool
prefixElem [[a]]
xs = forall (t :: * -> *). Foldable t => t Bool -> Bool
or forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a b. (a -> b) -> a -> b
($) (forall a b. (a -> b) -> [a] -> [b]
map forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [[a]]
xs) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a]
repeat
withDyreOptions :: Params c r -> IO a -> IO a
withDyreOptions :: forall c r a. Params c r -> IO a -> IO a
withDyreOptions Params{configCheck :: forall cfgType a. Params cfgType a -> Bool
configCheck = Bool
check} IO a
action = forall a. [Char] -> IO a -> IO a
withStore [Char]
"dyre" forall a b. (a -> b) -> a -> b
$ do
[[Char]]
args <- IO [[Char]]
getArgs
[Char]
this <- if Bool
check then IO [Char]
getExecutablePath else IO [Char]
getProgName
forall a. Typeable a => [Char] -> [Char] -> a -> IO ()
putValue [Char]
"dyre" [Char]
"masterBinary" [Char]
this
[[Char]] -> [Char] -> [Char] -> IO ()
storeFlag [[Char]]
args [Char]
"--dyre-master-binary=" [Char]
"masterBinary"
[[Char]] -> [Char] -> [Char] -> IO ()
storeFlag [[Char]]
args [Char]
"--dyre-state-persist=" [Char]
"persistState"
forall a. Typeable a => [Char] -> [Char] -> a -> IO ()
putValue [Char]
"dyre" [Char]
"forceReconf" forall a b. (a -> b) -> a -> b
$ [Char]
"--force-reconf" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
args
forall a. Typeable a => [Char] -> [Char] -> a -> IO ()
putValue [Char]
"dyre" [Char]
"denyReconf" forall a b. (a -> b) -> a -> b
$ [Char]
"--deny-reconf" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
args
forall a. Typeable a => [Char] -> [Char] -> a -> IO ()
putValue [Char]
"dyre" [Char]
"debugMode" forall a b. (a -> b) -> a -> b
$ [Char]
"--dyre-debug" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
args
forall a. [[Char]] -> IO a -> IO a
withArgs ([[Char]] -> [[Char]]
removeDyreOptions [[Char]]
args) IO a
action
getForceReconf :: IO Bool
getForceReconf :: IO Bool
getForceReconf = forall a. Typeable a => [Char] -> [Char] -> a -> IO a
getDefaultValue [Char]
"dyre" [Char]
"forceReconf" Bool
False
getDenyReconf :: IO Bool
getDenyReconf :: IO Bool
getDenyReconf = forall a. Typeable a => [Char] -> [Char] -> a -> IO a
getDefaultValue [Char]
"dyre" [Char]
"denyReconf" Bool
False
getDebug :: IO Bool
getDebug :: IO Bool
getDebug = forall a. Typeable a => [Char] -> [Char] -> a -> IO a
getDefaultValue [Char]
"dyre" [Char]
"debugMode" Bool
False
getMasterBinary :: IO (Maybe String)
getMasterBinary :: IO (Maybe [Char])
getMasterBinary = forall a. Typeable a => [Char] -> [Char] -> IO (Maybe a)
getValue [Char]
"dyre" [Char]
"masterBinary"
getStatePersist :: IO (Maybe String)
getStatePersist :: IO (Maybe [Char])
getStatePersist = forall a. Typeable a => [Char] -> [Char] -> IO (Maybe a)
getValue [Char]
"dyre" [Char]
"persistState"
customOptions :: Maybe [String] -> IO [String]
customOptions :: Maybe [[Char]] -> IO [[Char]]
customOptions Maybe [[Char]]
otherArgs = do
Maybe [Char]
masterPath <- IO (Maybe [Char])
getMasterBinary
Maybe [Char]
stateFile <- IO (Maybe [Char])
getStatePersist
Bool
debugMode <- IO Bool
getDebug
[[Char]]
mainArgs <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe IO [[Char]]
getArgs forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [[Char]]
otherArgs
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ [[Char]]
mainArgs forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [[Char]
"--dyre-debug" | Bool
debugMode]
, [[Char]
"--dyre-state-persist=" forall a. [a] -> [a] -> [a]
++ [Char]
sf | Just [Char]
sf <- [Maybe [Char]
stateFile]]
, [ [Char]
"--dyre-master-binary="
forall a. [a] -> [a] -> [a]
++ forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"'dyre' data-store doesn't exist (in Config.Dyre.Options.customOptions)") Maybe [Char]
masterPath]
]
storeFlag :: [String] -> String -> String -> IO ()
storeFlag :: [[Char]] -> [Char] -> [Char] -> IO ()
storeFlag [[Char]]
args [Char]
flag [Char]
name
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Char]]
match = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = forall a. Typeable a => [Char] -> [Char] -> a -> IO ()
putValue [Char]
"dyre" [Char]
name forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
flag) (forall a. [a] -> a
head [[Char]]
match)
where match :: [[Char]]
match = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf [Char]
flag) [[Char]]
args
dyreArgs :: [String]
dyreArgs :: [[Char]]
dyreArgs = [ [Char]
"--force-reconf", [Char]
"--deny-reconf"
, [Char]
"--dyre-state-persist", [Char]
"--dyre-debug"
, [Char]
"--dyre-master-binary" ]