Skip to content

Commit

Permalink
conway governance hash: add option to write to file
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Nov 16, 2023
1 parent a3d4a04 commit 7c3b50b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ newtype GovernanceHashCmds era = GovernanceHashCmd (GovernanceHashCmdArgs era)

data GovernanceHashCmdArgs era
= GovernanceHashCmdArgs {
eon :: !(ConwayEraOnwards era),
toHash :: !GovernanceHashSource
eon :: !(ConwayEraOnwards era)
, toHash :: !GovernanceHashSource
, moutFile :: !(Maybe (File () Out)) -- ^ The output file to which the hash is written
} deriving Show

data GovernanceHashSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ pGovernanceHashCmds era = do
return
$ subParser "hash"
$ Opt.info
( Cmd.GovernanceHashCmd . GovernanceHashCmdArgs eon
<$> pGovernanceHashSource
)
( fmap Cmd.GovernanceHashCmd
(GovernanceHashCmdArgs eon
<$> pGovernanceHashSource
<*> optional pOutputFile))
$ Opt.progDesc "Compute the hash to pass to the various --*-hash arguments of governance commands."

pGovernanceHashSource :: Parser Cmd.GovernanceHashSource
Expand Down
14 changes: 11 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ runGovernanceHashCmds (Cmd.GovernanceHashCmd args)=
runGovernanceHashCmd :: ()
=> Cmd.GovernanceHashCmdArgs era
-> ExceptT GovernanceHashError IO ()
runGovernanceHashCmd Cmd.GovernanceHashCmdArgs { toHash } =
runGovernanceHashCmd Cmd.GovernanceHashCmdArgs { toHash, moutFile } =
-- TODO @smelc we probably want an option to write the computed hash to a file
-- This can be done in a separate PR
case toHash of
Expand All @@ -61,5 +61,13 @@ runGovernanceHashCmd Cmd.GovernanceHashCmdArgs { toHash } =
let hash = Ledger.hashAnchorData $ Ledger.AnchorData $ Text.encodeUtf8 text
printHash hash
where
printHash :: Ledger.SafeHash StandardCrypto i -> ExceptT a IO ()
printHash = liftIO . putStr . Text.unpack . hashToTextAsHex . extractHash
printHash :: Ledger.SafeHash StandardCrypto i -> ExceptT GovernanceHashError IO ()
printHash hash = do
case moutFile of
Nothing -> liftIO . putStr $ Text.unpack text
Just file ->
let dest = unFile file in
firstExceptT (GovernanceHashWriteFileError dest) $
newExceptT $ writeTextFile file text
where
text = hashToTextAsHex . extractHash $ hash
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import Cardano.Api
import Cardano.Prelude (Exception (displayException), IOException)

data GovernanceHashError
= GovernanceHashReadFileError FilePath IOException
= GovernanceHashReadFileError !FilePath !IOException
| GovernanceHashWriteFileError !FilePath !(FileError ())
deriving Show

instance Error GovernanceHashError where
displayError = \case
GovernanceHashReadFileError filepath exc ->
"Cannot read " <> filepath <> ": " <> displayException exc
GovernanceHashWriteFileError filepath fileErr ->
"Cannot write " <> filepath <> ": " <> displayError fileErr

0 comments on commit 7c3b50b

Please sign in to comment.