{-|
Module      : Headroom.Command.Gen
Description : Logic for Generate command
Copyright   : (c) 2019-2020 Vaclav Svejcar
License     : BSD-3
Maintainer  : vaclav.svejcar@gmail.com
Stability   : experimental
Portability : POSIX

Logic for the /Generator/ command, used to generate /stub/ files.
-}
{-# LANGUAGE NoImplicitPrelude #-}
module Headroom.Command.Gen
  ( commandGen
  )
where

import           Headroom.Command.Gen.Env
import           Headroom.Command.Shared        ( bootstrap )
import           Headroom.Embedded              ( configFileStub
                                                , licenseTemplate
                                                )
import           Headroom.License               ( parseLicense )
import           Headroom.Types                 ( HeadroomError(..) )
import           Prelude                        ( putStrLn )
import           RIO


env' :: GenOptions -> LogFunc -> IO Env
env' :: GenOptions -> LogFunc -> IO Env
env' opts :: GenOptions
opts logFunc :: LogFunc
logFunc = Env -> IO Env
forall (m :: * -> *) a. Monad m => a -> m a
return (Env -> IO Env) -> Env -> IO Env
forall a b. (a -> b) -> a -> b
$ $WEnv :: LogFunc -> GenOptions -> Env
Env { envLogFunc :: LogFunc
envLogFunc = LogFunc
logFunc, envGenOptions :: GenOptions
envGenOptions = GenOptions
opts }

-- | Handler for /Generator/ command.
commandGen :: GenOptions -- ^ /Generator/ command options
           -> IO ()      -- ^ execution result
commandGen :: GenOptions -> IO ()
commandGen opts :: GenOptions
opts = (LogFunc -> IO Env) -> Bool -> RIO Env () -> IO ()
forall env a. (LogFunc -> IO env) -> Bool -> RIO env a -> IO a
bootstrap (GenOptions -> LogFunc -> IO Env
env' GenOptions
opts) Bool
False (RIO Env () -> IO ()) -> RIO Env () -> IO ()
forall a b. (a -> b) -> a -> b
$ case GenOptions -> GenMode
goGenMode GenOptions
opts of
  GenConfigFile      -> IO () -> RIO Env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
printConfigFile
  GenLicense license :: Text
license -> IO () -> RIO Env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO Env ()) -> IO () -> RIO Env ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
printLicense Text
license

printConfigFile :: IO ()
printConfigFile :: IO ()
printConfigFile = String -> IO ()
putStrLn String
forall a. IsString a => a
configFileStub

printLicense :: Text -> IO ()
printLicense :: Text -> IO ()
printLicense license :: Text
license = case Text -> Maybe License
parseLicense Text
license of
  Just license' :: License
license' -> String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ License -> String
forall a. IsString a => License -> a
licenseTemplate License
license'
  Nothing       -> HeadroomError -> IO ()
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (HeadroomError -> IO ()) -> HeadroomError -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> HeadroomError
InvalidLicense Text
license