Skip to content

Commit

Permalink
Add dev anoma print-config command (#3260)
Browse files Browse the repository at this point in the history
E.g.
```
juvix dev anoma start
juvix dev anoma print-config
```
output:
```
nodeid: '130225697'
port: 45367
url: localhost
```
  • Loading branch information
janmasrovira authored Dec 24, 2024
1 parent a4faa31 commit b386fbc
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
11 changes: 5 additions & 6 deletions app/Commands/Dev/Anoma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Commands.Dev.Anoma.Base
import Commands.Dev.Anoma.Client
import Commands.Dev.Anoma.Indexer qualified as Indexer
import Commands.Dev.Anoma.Options
import Commands.Dev.Anoma.PrintConfig qualified as PrintConfig
import Commands.Dev.Anoma.Prove qualified as Prove
import Commands.Dev.Anoma.Start qualified as Start
import Juvix.Data.CodeAnn
Expand All @@ -22,8 +23,9 @@ 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
AnomaCommandStatus -> getClientConfig >>= renderStdOutLn . ppCodeAnn
AnomaCommandStop -> getClientConfig >>= stopClient >> removeConfig
AnomaCommandPrintConfig opts -> PrintConfig.runCommand opts
AnomaCommandProve opts ->
runAnomaWithHostConfig
(Prove.runCommand opts)
Expand All @@ -37,13 +39,10 @@ runCommand g =
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) => Sem x AnomaClientInfo
getHostConfig = case g ^. anomaCommandGlobalClientConfig of
Just p -> fromAppFile p >>= readClientInfo
Nothing -> (^. clientConfigHost) <$> checkRunning
Nothing -> (^. clientConfigHost) <$> getClientConfig

readClientInfo :: (Members '[Files, Error SimpleError] x) => Path Abs File -> Sem x AnomaClientInfo
readClientInfo fp = do
Expand Down
15 changes: 12 additions & 3 deletions app/Commands/Dev/Anoma/Client.hs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
module Commands.Dev.Anoma.Client where
module Commands.Dev.Anoma.Client
( module Commands.Dev.Anoma.Client,
module Anoma.Client.Config,
)
where

import Anoma.Client.Config
import Anoma.Effect.Base
import Commands.Base
import Data.Foldable.Extra qualified as E
import Juvix.Prelude.Posix

isClientRunning :: (Members '[Files, EmbedIO, Error SimpleError, Logger] r) => ClientConfig -> Sem r Bool
isClientRunning :: (Members '[Files, EmbedIO, Error SimpleError] r) => ClientConfig -> Sem r Bool
isClientRunning c =
runAnomaWithClient
(c ^. clientConfigHost)
(catchError @SimpleError (anomaListMethods >> return True) (\_ _ -> return False))

checkClientRunning :: (Members '[Logger, Files, EmbedIO, Error SimpleError] r) => Sem r (Maybe ClientConfig)
checkClientRunning :: (Members '[Files, EmbedIO, Error SimpleError] r) => Sem r (Maybe ClientConfig)
checkClientRunning = do
mconfig <- readConfig
E.findM isClientRunning mconfig

getClientConfig ::
(Members '[Files, EmbedIO, Error SimpleError] r) =>
Sem r ClientConfig
getClientConfig = fromMaybeM (throw @SimpleError "The Anoma client is not running") checkClientRunning

stopClient :: (Members '[Files, EmbedIO] r) => ClientConfig -> Sem r ()
stopClient = terminateProcessPid . (^. clientConfigPid)
12 changes: 12 additions & 0 deletions app/Commands/Dev/Anoma/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ module Commands.Dev.Anoma.Options where

import Commands.Dev.Anoma.AddTransaction.Options
import Commands.Dev.Anoma.Indexer.Options
import Commands.Dev.Anoma.PrintConfig.Options
import Commands.Dev.Anoma.Prove.Options
import Commands.Dev.Anoma.Start.Options
import CommonOptions

data AnomaCommand
= AnomaCommandStart StartOptions
| AnomaCommandStatus
| AnomaCommandPrintConfig PrintConfigOptions
| AnomaCommandStop
| AnomaCommandProve ProveOptions
| AnomaCommandAddTransaction AddTransactionOptions
Expand All @@ -33,11 +35,21 @@ parseAnomaCommand =
commandStatus,
commandStop,
commandProve,
commandPrintConfig,
commandAddTransaction,
commandIndexer
]
)
where
commandPrintConfig :: Mod CommandFields AnomaCommand
commandPrintConfig = command "print-config" runInfo
where
runInfo :: ParserInfo AnomaCommand
runInfo =
info
(AnomaCommandPrintConfig <$> parsePrintConfigOptions)
(progDesc "Prints the yaml configuration of the Anoma client to stdout")

commandStart :: Mod CommandFields AnomaCommand
commandStart = command "start" runInfo
where
Expand Down
11 changes: 11 additions & 0 deletions app/Commands/Dev/Anoma/PrintConfig.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Commands.Dev.Anoma.PrintConfig where

import Commands.Base
import Commands.Dev.Anoma.Client
import Commands.Dev.Anoma.PrintConfig.Options
import Data.Yaml qualified as Yaml

runCommand :: (Members AppEffects r) => PrintConfigOptions -> Sem r ()
runCommand _ = runAppError @SimpleError $ do
cfg <- (^. clientConfigHost) <$> getClientConfig
renderStdOutRaw (Yaml.encode cfg)
9 changes: 9 additions & 0 deletions app/Commands/Dev/Anoma/PrintConfig/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Commands.Dev.Anoma.PrintConfig.Options where

import CommonOptions

data PrintConfigOptions = PrintConfigOptions
deriving stock (Data)

parsePrintConfigOptions :: Parser PrintConfigOptions
parsePrintConfigOptions = pure PrintConfigOptions
4 changes: 2 additions & 2 deletions src/Anoma/Effect/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ runAnomaEphemeral anomapath body = runEnvironment . runReader anomapath . runPro
AnomaListMethods -> anomaListMethods'
GetNodeInfo -> return NodeInfo {_nodeInfoId = grpcInfo ^. anomaClientInfoNodeId}

runAnomaWithClient :: forall r a. (Members '[Logger, EmbedIO, Error SimpleError] r) => AnomaClientInfo -> Sem (Anoma ': r) a -> Sem r a
runAnomaWithClient :: forall r a. (Members '[EmbedIO, Error SimpleError] r) => AnomaClientInfo -> Sem (Anoma ': r) a -> Sem r a
runAnomaWithClient grpcInfo body = do
let grpcInfo' = hardcodeNodeId grpcInfo
runProcess
Expand All @@ -122,7 +122,7 @@ runAnomaWithClient grpcInfo body = do
AnomaListMethods -> anomaListMethods'
GetNodeInfo -> return NodeInfo {_nodeInfoId = grpcInfo' ^. anomaClientInfoNodeId}

fromJSONErr :: (Members '[Error SimpleError, Logger] r) => (Aeson.FromJSON a) => Value -> Sem r a
fromJSONErr :: (Members '[Error SimpleError] r) => (Aeson.FromJSON a) => Value -> Sem r a
fromJSONErr v = case Aeson.fromJSON v of
Aeson.Success r -> return r
Aeson.Error err -> throw (SimpleError (mkAnsiText err))
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Data/Error/GenericError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ newtype SimpleError = SimpleError
{ _simpleErrorMessage :: AnsiText
}

instance IsString SimpleError where
fromString = SimpleError . mkAnsiText

makeLenses ''GenericError
makeLenses ''GenericOptions
makeLenses ''SimpleError
Expand Down

0 comments on commit b386fbc

Please sign in to comment.