Skip to content

Commit

Permalink
New Cardano.Api.IO module
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 27, 2023
1 parent 0592629 commit 448cc2a
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cardano-api/cardano-api.cabal
Expand Up @@ -68,6 +68,7 @@ library
Cardano.Api.GenesisParameters
Cardano.Api.Hash
Cardano.Api.HasTypeProxy
Cardano.Api.IO
Cardano.Api.IPC
Cardano.Api.IPC.Monad
Cardano.Api.InMode
Expand All @@ -83,6 +84,7 @@ library
Cardano.Api.Modes
Cardano.Api.NetworkId
Cardano.Api.OperationalCertificate
Cardano.Api.Options
Cardano.Api.ProtocolParameters
Cardano.Api.Protocol
Cardano.Api.Query
Expand Down
20 changes: 19 additions & 1 deletion cardano-api/src/Cardano/Api.hs
Expand Up @@ -51,6 +51,15 @@ module Cardano.Api (
generateSigningKey,
generateInsecureSigningKey,

-- ** Files
File(..),
Directory(..),
fileMap,
In,
Out,
InOut,
HasFileMode(..),

-- ** Hashes
-- | In Cardano most keys are identified by their hash, and hashes are
-- used in many other places.
Expand Down Expand Up @@ -724,7 +733,6 @@ module Cardano.Api (
chainPointToSlotNo,
chainPointToHeaderHash,
makeChainTip,
parseFilePath,
writeSecrets,

-- ** Cast functions
Expand Down Expand Up @@ -756,6 +764,14 @@ module Cardano.Api (

-- ** CLI option parsing
bounded,
parseFilePath,
fileOption,
inFileOption,
outFileOption,
parseFile,
parseFileIn,
parseFileOut,
parseDirectory,
) where

import Cardano.Api.Address
Expand All @@ -774,6 +790,7 @@ import Cardano.Api.GenesisParameters
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.InMode
import Cardano.Api.IO
import Cardano.Api.IPC
import Cardano.Api.IPC.Monad
import Cardano.Api.Keys.Byron
Expand All @@ -785,6 +802,7 @@ import Cardano.Api.LedgerState
import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.OperationalCertificate
import Cardano.Api.Options
import Cardano.Api.Protocol
import Cardano.Api.ProtocolParameters
import Cardano.Api.Query hiding (LedgerState (..))
Expand Down
52 changes: 52 additions & 0 deletions cardano-api/src/Cardano/Api/IO.hs
@@ -0,0 +1,52 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}

module Cardano.Api.IO
( File(..)
, Directory(..)
, fileMap
, In
, Out
, InOut
, HasFileMode(..)
) where

import Data.Aeson (FromJSON, ToJSON)
import Data.Kind (Type)
import Data.String (IsString)

-- | Phantom type to indicate a file for reading.
data In

-- | Phantom type to indicate a file for writing.
data Out

-- | Phantom type to indicate a file for reading or writing.
data InOut

-- | A file path with additional type information to indicate whether it is to
-- be used for reading or writing.
newtype File direction = File { unFile :: FilePath }
deriving (Eq, Ord)
deriving newtype (Show, IsString, FromJSON, ToJSON)

-- | A directory path.
newtype Directory = Directory { unDirectory :: FilePath }
deriving (Eq, Ord)
deriving newtype (Show, IsString, FromJSON, ToJSON)

fileMap :: (FilePath -> FilePath) -> File direction -> File direction
fileMap f = File . f . unFile

class HasFileMode (f :: Type -> Type) where
usingIn :: f InOut -> f In
usingOut :: f InOut -> f Out

instance HasFileMode File where
usingIn :: File InOut -> File In
usingIn = File . unFile

usingOut :: File InOut -> File Out
usingOut = File . unFile
61 changes: 61 additions & 0 deletions cardano-api/src/Cardano/Api/Options.hs
@@ -0,0 +1,61 @@
{-# LANGUAGE RankNTypes #-}

module Cardano.Api.Options
( fileOption
, inFileOption
, outFileOption
, parseFilePath
, parseFile
, parseFileIn
, parseFileOut
, parseDirectory
) where

import Cardano.Api.IO (Directory, File (..), In, Out)

import Options.Applicative (OptionFields)
import qualified Options.Applicative as Opt
import Options.Applicative.Builder (Mod)
import Options.Applicative.Types (Parser)

fileOption :: forall direction. Mod OptionFields FilePath -> Parser (File direction)
fileOption = fmap File . Opt.strOption

inFileOption :: forall. Mod OptionFields FilePath -> Parser (File In)
inFileOption = fileOption

outFileOption :: forall. Mod OptionFields FilePath -> Parser (File Out)
outFileOption = fileOption

parseDirectory :: String -> String -> Opt.Parser Directory
parseDirectory optname desc =
Opt.strOption
( Opt.long optname
<> Opt.metavar "DIRECTORY"
<> Opt.help desc
<> Opt.completer (Opt.bashCompleter "file")
)

parseFilePath :: String -> String -> Opt.Parser FilePath
parseFilePath optname desc =
Opt.strOption
( Opt.long optname
<> Opt.metavar "FILEPATH"
<> Opt.help desc
<> Opt.completer (Opt.bashCompleter "file")
)

parseFile :: String -> String -> Opt.Parser (File direction)
parseFile optname desc =
Opt.strOption
( Opt.long optname
<> Opt.metavar "FILEPATH"
<> Opt.help desc
<> Opt.completer (Opt.bashCompleter "file")
)

parseFileIn :: String -> String -> Opt.Parser (File In)
parseFileIn = parseFile

parseFileOut :: String -> String -> Opt.Parser (File Out)
parseFileOut = parseFile
11 changes: 0 additions & 11 deletions cardano-api/src/Cardano/Api/Utils.hs
Expand Up @@ -19,7 +19,6 @@ module Cardano.Api.Utils
, failEitherWith
, noInlineMaybeToStrictMaybe
, note
, parseFilePath
, readFileBlocking
, renderEra
, runParsecParser
Expand All @@ -40,7 +39,6 @@ import Data.Maybe.Strict
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.IO.Handle.FD (openFileBlocking)
import qualified Options.Applicative as Opt
import System.FilePath ((</>))
import System.IO (IOMode (ReadMode), hClose)
import qualified Text.Parsec as Parsec
Expand Down Expand Up @@ -94,15 +92,6 @@ note msg = \case
Nothing -> fail msg
Just a -> pure a

parseFilePath :: String -> String -> Opt.Parser FilePath
parseFilePath optname desc =
Opt.strOption
( Opt.long optname
<> Opt.metavar "FILEPATH"
<> Opt.help desc
<> Opt.completer (Opt.bashCompleter "file")
)

writeSecrets :: FilePath -> [Char] -> [Char] -> (a -> BS.ByteString) -> [a] -> IO ()
writeSecrets outDir prefix suffix secretOp xs =
forM_ (zip xs [0::Int ..]) $
Expand Down

0 comments on commit 448cc2a

Please sign in to comment.