{-# LANGUAGE RecordWildCards #-}
module Clckwrks.GetOpts where

import Control.Applicative   ((<$>))
import Clckwrks.Monad        (ClckwrksConfig(..), TLSSettings(..))
import Data.Maybe            (fromMaybe)
import System.Console.GetOpt (OptDescr(..), ArgDescr(..), ArgOrder(..), getOpt, usageInfo)
import System.Directory      (doesFileExist)
import System.Exit           (exitFailure, exitSuccess)

------------------------------------------------------------------------------
-- Command line options
------------------------------------------------------------------------------

-- | command-line Flags
data Flag
    = ModifyConfig (ClckwrksConfig -> ClckwrksConfig)
    | Help
    | Version

-- | Flag selectors
isHelp, isVersion :: Flag -> Bool
isHelp :: Flag -> Bool
isHelp    Flag
flag = case Flag
flag of Flag
Help    -> Bool
True; Flag
_ -> Bool
False
isVersion :: Flag -> Bool
isVersion Flag
flag = case Flag
flag of Flag
Version -> Bool
True; Flag
_ -> Bool
False

-- | Command line options.
clckwrksOpts :: ClckwrksConfig -> [OptDescr Flag]
clckwrksOpts :: ClckwrksConfig -> [OptDescr Flag]
clckwrksOpts ClckwrksConfig
def =
    [ [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"help"]          (Flag -> ArgDescr Flag
forall a. a -> ArgDescr a
NoArg Flag
Help)                    [Char]
"Display this help message"
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"http-port"]     (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setPort [Char]
"port")         ([Char]
"Port to bind http server, default: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show (ClckwrksConfig -> Int
clckPort ClckwrksConfig
def))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"https-port"]    (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setTLSPort [Char]
"port")      ([Char]
"Port to bind https server, default:" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> (Int -> [Char]) -> Maybe Int -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"disabled." Int -> [Char]
forall a. Show a => a -> [Char]
show (TLSSettings -> Int
clckTLSPort (TLSSettings -> Int) -> Maybe TLSSettings -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ClckwrksConfig -> Maybe TLSSettings
clckTLS ClckwrksConfig
def)))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"tls-cert"]      (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setTLSCert [Char]
"path")      ([Char]
"Path to tls .cert file. (required for https).")
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"tls-key"]       (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setTLSKey [Char]
"path")       ([Char]
"Path to tls .key file. (required for https).")
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"tls-ca"]        (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setTLSCA [Char]
"path")       ([Char]
"Path to tls .pem file. (required for some certs).")
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"hide-port"]     (Flag -> ArgDescr Flag
forall a. a -> ArgDescr a
NoArg Flag
setHidePort)             [Char]
"Do not show the port number in the URL"
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"hostname"]      (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setHostname [Char]
"hostname") ([Char]
"Server hostname, default: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (ClckwrksConfig -> [Char]
clckHostname ClckwrksConfig
def))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"jquery-path"]   (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setJQueryPath   [Char]
"path") ([Char]
"path to jquery directory, default: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (ClckwrksConfig -> [Char]
clckJQueryPath ClckwrksConfig
def))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"jqueryui-path"] (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setJQueryUIPath [Char]
"path") ([Char]
"path to jqueryui directory, default: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (ClckwrksConfig -> [Char]
clckJQueryUIPath ClckwrksConfig
def))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"jstree-path"]   (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setJSTreePath   [Char]
"path") ([Char]
"path to jstree directory, default: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (ClckwrksConfig -> [Char]
clckJSTreePath ClckwrksConfig
def))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"json2-path"]    (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setJSON2Path    [Char]
"path") ([Char]
"path to json2 directory, default: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show (ClckwrksConfig -> [Char]
clckJSON2Path ClckwrksConfig
def))
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"top"]           (([Char] -> Flag) -> [Char] -> ArgDescr Flag
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg [Char] -> Flag
setTopDir       [Char]
"path") ([Char]
"path to directory that holds the state directory, uploads, etc")
    , [Char] -> [[Char]] -> ArgDescr Flag -> [Char] -> OptDescr Flag
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [] [[Char]
"enable-analytics"] (Flag -> ArgDescr Flag
forall a. a -> ArgDescr a
NoArg Flag
setAnalytics)         [Char]
"enable google analytics tracking"
    ]
    where
      nullTLSSettings :: TLSSettings
nullTLSSettings     = TLSSettings :: Int -> [Char] -> [Char] -> Maybe [Char] -> TLSSettings
TLSSettings { clckTLSPort :: Int
clckTLSPort = Int
443
                                        , clckTLSCert :: [Char]
clckTLSCert = [Char]
""
                                        , clckTLSKey :: [Char]
clckTLSKey  = [Char]
""
                                        , clckTLSCA :: Maybe [Char]
clckTLSCA   = Maybe [Char]
forall a. Maybe a
Nothing
                                        }
      modifyTLS :: (TLSSettings -> a) -> ClckwrksConfig -> Maybe a
modifyTLS TLSSettings -> a
f ClckwrksConfig
cc =
          a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ case ClckwrksConfig -> Maybe TLSSettings
clckTLS ClckwrksConfig
cc of
            Maybe TLSSettings
Nothing -> TLSSettings -> a
f TLSSettings
nullTLSSettings
            (Just TLSSettings
tls) -> TLSSettings -> a
f TLSSettings
tls
      noop :: p -> Flag
noop            p
_   = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ ClckwrksConfig -> ClckwrksConfig
forall a. a -> a
id
      setPort :: [Char] -> Flag
setPort         [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckPort :: Int
clckPort         = [Char] -> Int
forall a. Read a => [Char] -> a
read [Char]
str }
      setTLSPort :: [Char] -> Flag
setTLSPort      [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckTLS :: Maybe TLSSettings
clckTLS          = (TLSSettings -> TLSSettings) -> ClckwrksConfig -> Maybe TLSSettings
forall a. (TLSSettings -> a) -> ClckwrksConfig -> Maybe a
modifyTLS (\TLSSettings
tls -> TLSSettings
tls { clckTLSPort :: Int
clckTLSPort = [Char] -> Int
forall a. Read a => [Char] -> a
read [Char]
str }) ClckwrksConfig
c }
      setTLSCert :: [Char] -> Flag
setTLSCert      [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckTLS :: Maybe TLSSettings
clckTLS          = (TLSSettings -> TLSSettings) -> ClckwrksConfig -> Maybe TLSSettings
forall a. (TLSSettings -> a) -> ClckwrksConfig -> Maybe a
modifyTLS (\TLSSettings
tls -> TLSSettings
tls { clckTLSCert :: [Char]
clckTLSCert = [Char]
str }) ClckwrksConfig
c }
      setTLSKey :: [Char] -> Flag
setTLSKey       [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckTLS :: Maybe TLSSettings
clckTLS          = (TLSSettings -> TLSSettings) -> ClckwrksConfig -> Maybe TLSSettings
forall a. (TLSSettings -> a) -> ClckwrksConfig -> Maybe a
modifyTLS (\TLSSettings
tls -> TLSSettings
tls { clckTLSKey :: [Char]
clckTLSKey = [Char]
str }) ClckwrksConfig
c }
      setTLSCA :: [Char] -> Flag
setTLSCA        [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckTLS :: Maybe TLSSettings
clckTLS          = (TLSSettings -> TLSSettings) -> ClckwrksConfig -> Maybe TLSSettings
forall a. (TLSSettings -> a) -> ClckwrksConfig -> Maybe a
modifyTLS (\TLSSettings
tls -> TLSSettings
tls { clckTLSCA :: Maybe [Char]
clckTLSCA = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
str }) ClckwrksConfig
c }
      setHostname :: [Char] -> Flag
setHostname     [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckHostname :: [Char]
clckHostname     = [Char]
str      }
      setHidePort :: Flag
setHidePort         = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckHidePort :: Bool
clckHidePort     = Bool
True     }
      setJQueryPath :: [Char] -> Flag
setJQueryPath   [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckJQueryPath :: [Char]
clckJQueryPath   = [Char]
str      }
      setJQueryUIPath :: [Char] -> Flag
setJQueryUIPath [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckJQueryUIPath :: [Char]
clckJQueryUIPath = [Char]
str      }
      setJSTreePath :: [Char] -> Flag
setJSTreePath   [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckJSTreePath :: [Char]
clckJSTreePath   = [Char]
str      }
      setJSON2Path :: [Char] -> Flag
setJSON2Path    [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckJSON2Path :: [Char]
clckJSON2Path    = [Char]
str      }
      setTopDir :: [Char] -> Flag
setTopDir       [Char]
str = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckTopDir :: Maybe [Char]
clckTopDir       = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
str }
      setAnalytics :: Flag
setAnalytics        = (ClckwrksConfig -> ClckwrksConfig) -> Flag
ModifyConfig ((ClckwrksConfig -> ClckwrksConfig) -> Flag)
-> (ClckwrksConfig -> ClckwrksConfig) -> Flag
forall a b. (a -> b) -> a -> b
$ \ClckwrksConfig
c -> ClckwrksConfig
c { clckEnableAnalytics :: Bool
clckEnableAnalytics = Bool
True  }

-- | Parse the command line arguments into a list of flags. Exits with usage
-- message, in case of failure.
parseArgs :: [OptDescr Flag] -> [String] -> IO (ClckwrksConfig -> IO ClckwrksConfig)
parseArgs :: [OptDescr Flag]
-> [[Char]] -> IO (ClckwrksConfig -> IO ClckwrksConfig)
parseArgs [OptDescr Flag]
opts [[Char]]
args =
    do case ArgOrder Flag
-> [OptDescr Flag] -> [[Char]] -> ([Flag], [[Char]], [[Char]])
forall a.
ArgOrder a -> [OptDescr a] -> [[Char]] -> ([a], [[Char]], [[Char]])
getOpt ArgOrder Flag
forall a. ArgOrder a
Permute [OptDescr Flag]
opts [[Char]]
args of
         ([Flag]
flags,[[Char]]
_,[]) ->
             if (Flag -> Bool) -> [Flag] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Flag -> Bool
isHelp [Flag]
flags
             then do [Char] -> IO ()
putStr ([OptDescr Flag] -> [Char]
helpMessage [OptDescr Flag]
opts)
                     IO (ClckwrksConfig -> IO ClckwrksConfig)
forall a. IO a
exitSuccess
             else do let f :: ClckwrksConfig -> IO ClckwrksConfig
f ClckwrksConfig
config = ClckwrksConfig -> IO ClckwrksConfig
checkTLS (ClckwrksConfig -> IO ClckwrksConfig)
-> ClckwrksConfig -> IO ClckwrksConfig
forall a b. (a -> b) -> a -> b
$ ((ClckwrksConfig -> ClckwrksConfig)
 -> (ClckwrksConfig -> ClckwrksConfig)
 -> ClckwrksConfig
 -> ClckwrksConfig)
-> (ClckwrksConfig -> ClckwrksConfig)
-> [ClckwrksConfig -> ClckwrksConfig]
-> ClckwrksConfig
-> ClckwrksConfig
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (ClckwrksConfig -> ClckwrksConfig)
-> (ClckwrksConfig -> ClckwrksConfig)
-> ClckwrksConfig
-> ClckwrksConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) ClckwrksConfig -> ClckwrksConfig
forall a. a -> a
id [ClckwrksConfig -> ClckwrksConfig
f | (ModifyConfig ClckwrksConfig -> ClckwrksConfig
f) <- [Flag]
flags ] ClckwrksConfig
config
                     (ClckwrksConfig -> IO ClckwrksConfig)
-> IO (ClckwrksConfig -> IO ClckwrksConfig)
forall (m :: * -> *) a. Monad m => a -> m a
return ClckwrksConfig -> IO ClckwrksConfig
f
         ([Flag]
_,[[Char]]
_,[[Char]]
errs)   ->
             do [Char] -> IO ()
putStr ([Char]
"Failure while parsing command line:\n"[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[[Char]] -> [Char]
unlines [[Char]]
errs)
                [Char] -> IO ()
putStr ([OptDescr Flag] -> [Char]
helpMessage [OptDescr Flag]
opts)
                IO (ClckwrksConfig -> IO ClckwrksConfig)
forall a. IO a
exitFailure
    where
      checkTLS :: ClckwrksConfig -> IO ClckwrksConfig
checkTLS ClckwrksConfig
cc =
          case ClckwrksConfig -> Maybe TLSSettings
clckTLS ClckwrksConfig
cc of
            Maybe TLSSettings
Nothing -> ClckwrksConfig -> IO ClckwrksConfig
forall (m :: * -> *) a. Monad m => a -> m a
return ClckwrksConfig
cc
            (Just TLSSettings{Int
[Char]
Maybe [Char]
clckTLSCA :: Maybe [Char]
clckTLSKey :: [Char]
clckTLSCert :: [Char]
clckTLSPort :: Int
clckTLSCA :: TLSSettings -> Maybe [Char]
clckTLSKey :: TLSSettings -> [Char]
clckTLSCert :: TLSSettings -> [Char]
clckTLSPort :: TLSSettings -> Int
..}) ->
                do Maybe [Char]
mCertError <-
                       if [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
clckTLSCert
                          then Maybe [Char] -> IO (Maybe [Char])
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"--tls-cert not set.")
                          else do Bool
b <- [Char] -> IO Bool
doesFileExist [Char]
clckTLSCert
                                  if Bool
b
                                     then Maybe [Char] -> IO (Maybe [Char])
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [Char]
forall a. Maybe a
Nothing
                                     else Maybe [Char] -> IO (Maybe [Char])
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> Maybe [Char]) -> [Char] -> Maybe [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
"Can not find the file " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
clckTLSCert)
                   Maybe [Char]
mKeyError <-
                       if [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
clckTLSKey
                          then Maybe [Char] -> IO (Maybe [Char])
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"--tls-key not set.")
                          else do Bool
b <- [Char] -> IO Bool
doesFileExist [Char]
clckTLSKey
                                  if Bool
b
                                     then Maybe [Char] -> IO (Maybe [Char])
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [Char]
forall a. Maybe a
Nothing
                                     else Maybe [Char] -> IO (Maybe [Char])
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> Maybe [Char]) -> [Char] -> Maybe [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
"Can not find the file " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
clckTLSKey)
                   case (Maybe [Char]
mCertError, Maybe [Char]
mKeyError) of
                     (Maybe [Char]
Nothing, Maybe [Char]
Nothing) -> ClckwrksConfig -> IO ClckwrksConfig
forall (m :: * -> *) a. Monad m => a -> m a
return ClckwrksConfig
cc
                     (Maybe [Char], Maybe [Char])
_ -> do [Char] -> IO ()
putStrLn [Char]
"It seems you are trying to enable https support. To do that you need to use both the --tls-cert and --tls-key flags."
                             [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines [[Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"" Maybe [Char]
mCertError, [Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"" Maybe [Char]
mKeyError]
                             [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [OptDescr Flag] -> [Char]
helpMessage [OptDescr Flag]
opts
                             IO ClckwrksConfig
forall a. IO a
exitFailure

-- | A simple usage message listing all flags possible.
helpMessage :: [OptDescr Flag] -> String
helpMessage :: [OptDescr Flag] -> [Char]
helpMessage [OptDescr Flag]
opts =
    [Char] -> [OptDescr Flag] -> [Char]
forall a. [Char] -> [OptDescr a] -> [Char]
usageInfo [Char]
header [OptDescr Flag]
opts
    where
      header :: [Char]
header = [Char]
"Usage: clckwrks [OPTION...]"