packages feed

horizon-gen-nix-0.4.0: src/Horizon/Gen/Nix/Options.hs

{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TemplateHaskell       #-}
module Horizon.Gen.Nix.Options
  ( horizonGenNixOpts
  , horizonGenNixOptsInfo
  , InputFile (..)
  , HorizonOptions (..)
  ,   ) where

import           Data.Either.Combinators (mapLeft)
import           Data.Kind               (Type)
import           Options.Applicative     (Parser, ParserInfo, ReadM,
                                          eitherReader, help, helper, info,
                                          long, metavar, option, value)
import           Path                    (File, Path, Rel, mkRelFile,
                                          parseRelFile)
import           Path.Dhall              ()


type InputFile :: Type
newtype InputFile where
  MkInputFile :: { fromInputFile :: Path Rel File }
              -> InputFile
  deriving stock (Eq, Show)

parseInputFile :: ReadM InputFile
parseInputFile = eitherReader $ mapLeft show . fmap MkInputFile . parseRelFile

defaultInputFile :: InputFile
defaultInputFile = MkInputFile $(mkRelFile "horizon.dhall")

inputFileOption :: Parser InputFile
inputFileOption =
  option parseInputFile
    (  long "input-file"
    <> metavar "INPUT_FILE"
    <> value defaultInputFile
    <> help "The name of the input file."
    )


type HorizonOptions :: Type
data HorizonOptions where
  MkHorizonOptions :: { optInputFile :: InputFile } -> HorizonOptions
  deriving stock (Eq, Show)

horizonGenNixOpts :: Parser HorizonOptions
horizonGenNixOpts =
      MkHorizonOptions
  <$> inputFileOption

horizonGenNixOptsInfo :: ParserInfo HorizonOptions
horizonGenNixOptsInfo = info (helper <*> horizonGenNixOpts) mempty