-----------------------------------------------------------------------------
--
-- Module      :  $Headers
-- Copyright   :  (c) 2021 Brian W Bush
-- License     :  MIT
--
-- Maintainer  :  Brian W Bush <code@functionally.io>
-- Stability   :  Experimental
-- Portability :  Portable
--
-- | Command-line for image-token service.
--
-----------------------------------------------------------------------------

module Main (
-- * Service
  main
) where


import Mantis.Types       (runMantisToIO)
import Pigy.Chain         (runChain)
import Pigy.Types         (makeContext, readConfiguration)
import System.Environment (getArgs)
import System.Exit        (exitFailure)
import System.IO          (hPutStrLn, stderr)


-- | Run the service.
main :: IO ()
main :: IO ()
main =
  do
    [String
filename] <- IO [String]
getArgs
    Either String ()
result <-
      MantisM IO () -> IO (Either String ())
forall a. MantisM IO a -> IO (Either String a)
runMantisToIO
        (MantisM IO () -> IO (Either String ()))
-> MantisM IO () -> IO (Either String ())
forall a b. (a -> b) -> a -> b
$ do
            Configuration
configuration <- String -> MantisM IO Configuration
forall (m :: * -> *).
MonadIO m =>
String -> MantisM m Configuration
readConfiguration String
filename
            Context
context <- Configuration -> MantisM IO Context
forall (m :: * -> *).
(MonadFail m, MonadIO m) =>
Configuration -> MantisM m Context
makeContext Configuration
configuration
            Context -> MantisM IO ()
forall (m :: * -> *).
(MonadFail m, MonadIO m) =>
Context -> MantisM m ()
runChain Context
context
    case Either String ()
result of
      Right () -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Left String
e   -> Handle -> String -> IO ()
hPutStrLn Handle
stderr String
e IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
forall a. IO a
exitFailure