Skip to content

Commit

Permalink
Move anoma client-config option to be global to all anoma commands
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcadman committed Nov 29, 2024
1 parent d42e201 commit 5efa8e7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 63 deletions.
19 changes: 8 additions & 11 deletions app/Commands/Dev/Anoma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,33 @@ import Commands.Dev.Anoma.Base
import Commands.Dev.Anoma.Client
import Commands.Dev.Anoma.Options
import Commands.Dev.Anoma.Prove qualified as Prove
import Commands.Dev.Anoma.Prove.Options
import Commands.Dev.Anoma.Start qualified as Start
import Juvix.Data.CodeAnn
import Juvix.Data.Yaml qualified as Y

runCommand :: forall r. (Members AppEffects r) => AnomaCommand -> Sem r ()
runCommand =
runAppError @SimpleError . \case
runCommand :: forall r. (Members AppEffects r) => AnomaCommandGlobal -> Sem r ()
runCommand g =
runAppError @SimpleError $ case (g ^. anomaCommandGlobalCommand) of
AnomaCommandStart opts -> Start.runCommand opts
AnomaCommandStatus -> checkRunning >>= renderStdOutLn . ppCodeAnn
AnomaCommandStop -> checkRunning >>= stopClient >> removeConfig
AnomaCommandProve opts ->
runAnomaWithHostConfig
(opts ^. proveClientInfo)
(Prove.runCommand opts)
AnomaCommandAddTransaction opts ->
runAnomaWithHostConfig
(opts ^. addTransactionClientInfo)
(addTransaction (opts ^. addTransactionFile))
where
runAnomaWithHostConfig :: (Members (Error SimpleError ': AppEffects) x) => Maybe (AppPath File) -> Sem (Anoma ': x) () -> Sem x ()
runAnomaWithHostConfig mconfigFile eff = do
host <- getHostConfig mconfigFile
runAnomaWithHostConfig :: (Members (Error SimpleError ': AppEffects) x) => Sem (Anoma ': x) () -> Sem x ()
runAnomaWithHostConfig eff = do
host <- getHostConfig
runAnomaWithClient host eff

checkRunning :: (Members (Error SimpleError ': AppEffects) x) => Sem x ClientConfig
checkRunning = fromMaybeM (logInfo "The Anoma client is not running" >> exitFailure) checkClientRunning

getHostConfig :: (Members (Error SimpleError ': AppEffects) x) => Maybe (AppPath File) -> Sem x AnomaClientInfo
getHostConfig = \case
getHostConfig :: (Members (Error SimpleError ': AppEffects) x) => Sem x AnomaClientInfo
getHostConfig = case g ^. anomaCommandGlobalClientConfig of
Just p -> fromAppFile p >>= readClientInfo
Nothing -> (^. clientConfigHost) <$> checkRunning

Expand Down
6 changes: 2 additions & 4 deletions app/Commands/Dev/Anoma/AddTransaction/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module Commands.Dev.Anoma.AddTransaction.Options where

import CommonOptions

data AddTransactionOptions = AddTransactionOptions
{ _addTransactionFile :: AppPath File,
_addTransactionClientInfo :: Maybe (AppPath File)
newtype AddTransactionOptions = AddTransactionOptions
{ _addTransactionFile :: AppPath File
}
deriving stock (Data)

Expand All @@ -13,5 +12,4 @@ makeLenses ''AddTransactionOptions
parseAddTransactionOptions :: Parser AddTransactionOptions
parseAddTransactionOptions = do
_addTransactionFile <- parseInputFile FileExtNockma
_addTransactionClientInfo <- optional anomaClientConfigOpt
pure AddTransactionOptions {..}
67 changes: 26 additions & 41 deletions app/Commands/Dev/Anoma/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ data AnomaCommand
| AnomaCommandAddTransaction AddTransactionOptions
deriving stock (Data)

parseAnomaCommand :: Parser AnomaCommand
data AnomaCommandGlobal = AnomaCommandGlobal
{ _anomaCommandGlobalClientConfig :: Maybe (AppPath File),
_anomaCommandGlobalCommand :: AnomaCommand
}
deriving stock (Data)

makeLenses ''AnomaCommandGlobal

parseAnomaCommand :: Parser AnomaCommandGlobal
parseAnomaCommand =
hsubparser
( mconcat
[ commandStart,
commandStatus,
commandStop,
commandProve,
commandAddTransaction
]
)
AnomaCommandGlobal
<$> optional anomaClientConfigOpt
<*> hsubparser
( mconcat
[ commandStart,
commandStatus,
commandStop,
commandProve,
commandAddTransaction
]
)
where
commandStart :: Mod CommandFields AnomaCommand
commandStart = command "start" runInfo
Expand Down Expand Up @@ -63,15 +73,12 @@ parseAnomaCommand =
( Just
( vsep
( [ "The prove command submits a Nockma program to the Anoma.Protobuf.NockService.Prove gRPC endpoint.",
""
"",
"The gRPC response (a Nockma program) is saved to a file named <input>.proved.nockma, where <input> is the base name of the input file.",
"Use the -o/--output option to specify a custom output filename.",
"",
"If the program generates traces, they will be written to standard output."
]
<> configHelp
<> [ "",
"The gRPC response (a Nockma program) is saved to a file named <input>.proved.nockma, where <input> is the base name of the input file.",
"Use the -o/--output option to specify a custom output filename.",
"",
"If the program generates traces, they will be written to standard output."
]
)
)
)
Expand All @@ -85,26 +92,4 @@ parseAnomaCommand =
runInfo =
info
(AnomaCommandAddTransaction <$> parseAddTransactionOptions)
( headerDoc
( Just
( vsep
( [ "The add-transaction command submits a Nockma transaction candidate to the Anoma.Protobuf.Mempool.AddTransaction gRPC endpoint.",
""
]
<> configHelp
)
)
)
<> progDesc "Submit a Nockma transaction candidate to Anoma.Protobuf.Mempool.AddTransaction"
)

configHelp :: [Doc AnsiStyle]
configHelp =
[ "By default, the gRPC request is made to the client that is started by juvix dev anoma start.",
"Use the -c/--config option to use a different Anoma client.",
"The config file format is:",
"",
"url: <ANOMA_CLIENT_URL>",
"port: <ANOMA_CLIENT_GRPC_PORT>",
"nodeid: <ANOMA_CLIENT_NODE_ID>"
]
(progDesc "Submit a Nockma transaction candidate to Anoma.Protobuf.Mempool.AddTransaction")
2 changes: 0 additions & 2 deletions app/Commands/Dev/Anoma/Prove/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import CommonOptions
data ProveOptions = ProveOptions
{ _proveFile :: AppPath File,
_proveArgs :: Maybe (AppPath File),
_proveClientInfo :: Maybe (AppPath File),
_proveOutputFile :: Maybe (AppPath File)
}
deriving stock (Data)
Expand All @@ -16,6 +15,5 @@ parseProveOptions :: Parser ProveOptions
parseProveOptions = do
_proveFile <- parseInputFile FileExtNockma
_proveArgs <- optional anomaArgsOpt
_proveClientInfo <- optional anomaClientConfigOpt
_proveOutputFile <- optional parseGenericOutputFile
pure ProveOptions {..}
28 changes: 23 additions & 5 deletions app/Commands/Dev/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ data DevCommand
| JuvixDevRepl ReplOptions
| MigrateJuvixYaml MigrateJuvixYamlOptions
| Nockma NockmaCommand
| Anoma AnomaCommand
| Anoma AnomaCommandGlobal
deriving stock (Data)

parseDevCommand :: Parser DevCommand
Expand Down Expand Up @@ -212,7 +212,25 @@ commandNockma =

commandAnoma :: Mod CommandFields DevCommand
commandAnoma =
command "anoma" $
info
(Anoma <$> parseAnomaCommand)
(progDesc "Subcommands related to the Anoma client")
let descr :: (IsString a) => a
descr = "Subcommands related to the Anoma client"
in command "anoma" $
info
(Anoma <$> parseAnomaCommand)
( headerDoc
( Just
( vsep
[ descr,
"",
"By default, the gRPC request is made to the client that is started by juvix dev anoma start.",
"Use the -c/--config option to use a different Anoma client.",
"The config file format is:",
"",
"url: <ANOMA_CLIENT_URL>",
"port: <ANOMA_CLIENT_GRPC_PORT>",
"nodeid: <ANOMA_CLIENT_NODE_ID>"
]
)
)
<> progDesc descr
)

0 comments on commit 5efa8e7

Please sign in to comment.