module HMGit.Commands.Plumbing.HashObject.Cmd (
    hashObjectCmd
) where

import           HMGit.Commands                          (Cmd (..))
import           HMGit.Commands.Plumbing.HashObject.Core (HashObject,
                                                          hashObjectShow,
                                                          hashObjectWrite)
import           HMGit.Internal.Parser                   (ObjectType (..))

import           Control.Exception.Safe                  (MonadCatch)
import           Control.Monad.IO.Class                  (MonadIO)
import           Data.Foldable                           (asum)
import qualified Options.Applicative                     as OA

hashObjectMode :: (MonadCatch m, MonadIO m) => OA.Parser (HashObject m)
hashObjectMode :: Parser (HashObject m)
hashObjectMode = [Parser (HashObject m)] -> Parser (HashObject m)
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [
    HashObject m
-> Mod FlagFields (HashObject m) -> Parser (HashObject m)
forall a. a -> Mod FlagFields a -> Parser a
OA.flag' HashObject m
forall (m :: * -> *). (MonadCatch m, MonadIO m) => HashObject m
hashObjectWrite (Mod FlagFields (HashObject m) -> Parser (HashObject m))
-> Mod FlagFields (HashObject m) -> Parser (HashObject m)
forall a b. (a -> b) -> a -> b
$ [Mod FlagFields (HashObject m)] -> Mod FlagFields (HashObject m)
forall a. Monoid a => [a] -> a
mconcat [
        Char -> Mod FlagFields (HashObject m)
forall (f :: * -> *) a. HasName f => Char -> Mod f a
OA.short Char
'w'
      , String -> Mod FlagFields (HashObject m)
forall (f :: * -> *) a. String -> Mod f a
OA.help String
"Actually write the object into the object database."
      ]
  , HashObject m -> Parser (HashObject m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashObject m
forall (m :: * -> *). (MonadCatch m, MonadIO m) => HashObject m
hashObjectShow
  ]

hashObjectType :: OA.Parser ObjectType
hashObjectType :: Parser ObjectType
hashObjectType = ReadM ObjectType
-> Mod OptionFields ObjectType -> Parser ObjectType
forall a. ReadM a -> Mod OptionFields a -> Parser a
OA.option ReadM ObjectType
forall a. Read a => ReadM a
OA.auto (Mod OptionFields ObjectType -> Parser ObjectType)
-> Mod OptionFields ObjectType -> Parser ObjectType
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields ObjectType] -> Mod OptionFields ObjectType
forall a. Monoid a => [a] -> a
mconcat [
    Char -> Mod OptionFields ObjectType
forall (f :: * -> *) a. HasName f => Char -> Mod f a
OA.short Char
't'
  , String -> Mod OptionFields ObjectType
forall (f :: * -> *) a. String -> Mod f a
OA.help String
"Specify the type"
  , String -> Mod OptionFields ObjectType
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
OA.metavar String
"<type>"
  , ObjectType -> Mod OptionFields ObjectType
forall (f :: * -> *) a. HasValue f => a -> Mod f a
OA.value ObjectType
Blob
  ]

hashObjectFilePath :: OA.Parser String
hashObjectFilePath :: Parser String
hashObjectFilePath = [Parser String] -> Parser String
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [
    String -> Mod FlagFields String -> Parser String
forall a. a -> Mod FlagFields a -> Parser a
OA.flag' String
"/dev/stdin" (Mod FlagFields String -> Parser String)
-> Mod FlagFields String -> Parser String
forall a b. (a -> b) -> a -> b
$ [Mod FlagFields String] -> Mod FlagFields String
forall a. Monoid a => [a] -> a
mconcat [
        String -> Mod FlagFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
OA.long String
"stdin"
      , String -> Mod FlagFields String
forall (f :: * -> *) a. String -> Mod f a
OA.help String
"Read the object from standard input instead of from a file."
      ]
  , Mod ArgumentFields String -> Parser String
forall s. IsString s => Mod ArgumentFields s -> Parser s
OA.strArgument (Mod ArgumentFields String -> Parser String)
-> Mod ArgumentFields String -> Parser String
forall a b. (a -> b) -> a -> b
$ [Mod ArgumentFields String] -> Mod ArgumentFields String
forall a. Monoid a => [a] -> a
mconcat [
        String -> Mod ArgumentFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
OA.metavar String
"<file>"
      , String -> Mod ArgumentFields String
forall (f :: * -> *) a. String -> Mod f a
OA.help String
"File path"
      ]
  ]

hashObjectCmd :: (MonadCatch m, MonadIO m) => OA.Mod OA.CommandFields (Cmd m)
hashObjectCmd :: Mod CommandFields (Cmd m)
hashObjectCmd = String -> ParserInfo (Cmd m) -> Mod CommandFields (Cmd m)
forall a. String -> ParserInfo a -> Mod CommandFields a
OA.command String
"hash-object"
    (ParserInfo (Cmd m) -> Mod CommandFields (Cmd m))
-> ParserInfo (Cmd m) -> Mod CommandFields (Cmd m)
forall a b. (a -> b) -> a -> b
$ Parser (Cmd m) -> InfoMod (Cmd m) -> ParserInfo (Cmd m)
forall a. Parser a -> InfoMod a -> ParserInfo a
OA.info (ObjectType -> HashObject m -> String -> Cmd m
forall (m :: * -> *). ObjectType -> HashObject m -> String -> Cmd m
CmdHashObject (ObjectType -> HashObject m -> String -> Cmd m)
-> Parser ObjectType -> Parser (HashObject m -> String -> Cmd m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser (ObjectType -> ObjectType)
forall a. Parser (a -> a)
OA.helper Parser (ObjectType -> ObjectType)
-> Parser ObjectType -> Parser ObjectType
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ObjectType
hashObjectType) Parser (HashObject m -> String -> Cmd m)
-> Parser (HashObject m) -> Parser (String -> Cmd m)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (HashObject m)
forall (m :: * -> *).
(MonadCatch m, MonadIO m) =>
Parser (HashObject m)
hashObjectMode Parser (String -> Cmd m) -> Parser String -> Parser (Cmd m)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser String
hashObjectFilePath)
    (InfoMod (Cmd m) -> ParserInfo (Cmd m))
-> InfoMod (Cmd m) -> ParserInfo (Cmd m)
forall a b. (a -> b) -> a -> b
$ String -> InfoMod (Cmd m)
forall a. String -> InfoMod a
OA.progDesc String
"Compute object ID and optionally creates a blob from a file"