Skip to content

Add version number CLI commands for standard and extended db-sync #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions cardano-db-sync-extended/app/cardano-db-sync-extended.hs
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

import Cardano.Prelude

import Cardano.Db (MigrationDir (..))
import Cardano.DbSync (ConfigFile (..), DbSyncNodeParams (..), LedgerStateDir (..),
SocketPath (..), runDbSyncNode)
import Cardano.DbSync (ConfigFile (..), DbSyncCommand (..), DbSyncNodeParams (..),
LedgerStateDir (..), SocketPath (..), runDbSyncNode)
import Cardano.DbSync.Plugin.Extended (extendedDbSyncNodePlugin)

import Cardano.Slotting.Slot (SlotNo (..))

import Data.String (String)
import qualified Data.Text as Text
import Data.Version (showVersion)

import Development.GitRev (gitHash)


import Options.Applicative (Parser, ParserInfo)
import qualified Options.Applicative as Opt

import Paths_cardano_db_sync_extended (version)

import System.Info (arch, compilerName, compilerVersion, os)

main :: IO ()
main = do
runDbSyncNode extendedDbSyncNodePlugin =<< Opt.execParser opts
cmd <- Opt.execParser opts
case cmd of
CmdVersion -> runVersionCommand
CmdRun params -> runDbSyncNode extendedDbSyncNodePlugin params

-- -------------------------------------------------------------------------------------------------

opts :: ParserInfo DbSyncNodeParams
opts :: ParserInfo DbSyncCommand
opts =
Opt.info (pCommandLine <**> Opt.helper)
( Opt.fullDesc
<> Opt.progDesc "Extended Cardano POstgreSQL sync node."
)

pCommandLine :: Parser DbSyncNodeParams
pCommandLine :: Parser DbSyncCommand
pCommandLine =
asum
[ pVersionCommand
, CmdRun <$> pRunDbSyncNode
]

pRunDbSyncNode :: Parser DbSyncNodeParams
pRunDbSyncNode =
DbSyncNodeParams
<$> pConfigFile
<*> pSocketPath
Expand Down Expand Up @@ -78,3 +100,34 @@ pSlotNo =
<> Opt.help "Force a rollback to the specified slot (mainly for testing and debugging)."
<> Opt.metavar "WORD"
)

pVersionCommand :: Parser DbSyncCommand
pVersionCommand =
asum
[ Opt.subparser
( mconcat
[ command' "version" "Show the program version" (pure CmdVersion) ]
)
, Opt.flag' CmdVersion
( Opt.long "version"
<> Opt.help "Show the program version"
<> Opt.hidden
)
]

command' :: String -> String -> Parser a -> Opt.Mod Opt.CommandFields a
command' c descr p =
Opt.command c
$ Opt.info (p <**> Opt.helper)
$ mconcat [ Opt.progDesc descr ]

runVersionCommand :: IO ()
runVersionCommand = do
liftIO . putTextLn $ mconcat
[ "cardano-db-sync-extended ", renderVersion version
, " - ", Text.pack os, "-", Text.pack arch
, " - ", Text.pack compilerName, "-", renderVersion compilerVersion
, "\ngit revision ", Text.pack $(gitHash)
]
where
renderVersion = Text.pack . showVersion
4 changes: 4 additions & 0 deletions cardano-db-sync-extended/cardano-db-sync-extended.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ executable cardano-db-sync-extended
-Wno-unsafe
-threaded

other-modules: Paths_cardano_db_sync_extended

build-depends: base >= 4.12 && < 4.13
, bytestring
, cardano-db
, cardano-db-sync
, cardano-db-sync-extended
, cardano-prelude
, cardano-slotting
, gitrev
, optparse-applicative
, ouroboros-network
, text
63 changes: 58 additions & 5 deletions cardano-db-sync/app/cardano-db-sync.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

import Cardano.Prelude

import Cardano.Db (MigrationDir (..))
import Cardano.DbSync (ConfigFile (..), DbSyncNodeParams (..), LedgerStateDir (..),
SocketPath (..), defDbSyncNodePlugin, runDbSyncNode)
import Cardano.DbSync (ConfigFile (..), DbSyncCommand (..), DbSyncNodeParams (..),
LedgerStateDir (..), SocketPath (..), defDbSyncNodePlugin, runDbSyncNode)

import Cardano.Slotting.Slot (SlotNo (..))

import Data.String (String)
import qualified Data.Text as Text
import Data.Version (showVersion)

import Development.GitRev (gitHash)

import Options.Applicative (Parser, ParserInfo)
import qualified Options.Applicative as Opt

import Paths_cardano_db_sync (version)

import System.Info (arch, compilerName, compilerVersion, os)


main :: IO ()
main = do
runDbSyncNode defDbSyncNodePlugin =<< Opt.execParser opts
cmd <- Opt.execParser opts
case cmd of
CmdVersion -> runVersionCommand
CmdRun params -> runDbSyncNode defDbSyncNodePlugin params

-- -------------------------------------------------------------------------------------------------

opts :: ParserInfo DbSyncNodeParams
opts :: ParserInfo DbSyncCommand
opts =
Opt.info (pCommandLine <**> Opt.helper)
( Opt.fullDesc
<> Opt.progDesc "Cardano PostgreSQL sync node."
)

pCommandLine :: Parser DbSyncNodeParams
pCommandLine :: Parser DbSyncCommand
pCommandLine =
asum
[ pVersionCommand
, CmdRun <$> pRunDbSyncNode
]

pRunDbSyncNode :: Parser DbSyncNodeParams
pRunDbSyncNode =
DbSyncNodeParams
<$> pConfigFile
<*> pSocketPath
Expand Down Expand Up @@ -77,3 +99,34 @@ pSlotNo =
<> Opt.help "Force a rollback to the specified slot (mainly for testing and debugging)."
<> Opt.metavar "WORD"
)

pVersionCommand :: Parser DbSyncCommand
pVersionCommand =
asum
[ Opt.subparser
( mconcat
[ command' "version" "Show the program version" (pure CmdVersion) ]
)
, Opt.flag' CmdVersion
( Opt.long "version"
<> Opt.help "Show the program version"
<> Opt.hidden
)
]

command' :: String -> String -> Parser a -> Opt.Mod Opt.CommandFields a
command' c descr p =
Opt.command c
$ Opt.info (p <**> Opt.helper)
$ mconcat [ Opt.progDesc descr ]

runVersionCommand :: IO ()
runVersionCommand = do
liftIO . putTextLn $ mconcat
[ "cardano-db-sync ", renderVersion version
, " - ", Text.pack os, "-", Text.pack arch
, " - ", Text.pack compilerName, "-", renderVersion compilerVersion
, "\ngit revision ", Text.pack $(gitHash)
]
where
renderVersion = Text.pack . showVersion
6 changes: 6 additions & 0 deletions cardano-db-sync/cardano-db-sync.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ library

Cardano.DbSync.Tracing.ToObjectOrphans


other-modules: Cardano.DbSync.Era.Byron.Genesis
Cardano.DbSync.Era.Byron.Insert
Cardano.DbSync.Era.Shelley.Genesis
Expand Down Expand Up @@ -134,11 +135,16 @@ executable cardano-db-sync
-Wno-unsafe
-threaded


other-modules: Paths_cardano_db_sync

build-depends: base >= 4.12 && < 4.13
, bytestring
, cardano-db
, cardano-db-sync
, cardano-prelude
, cardano-slotting
, gitrev
, optparse-applicative
, ouroboros-network
, text
1 change: 1 addition & 0 deletions cardano-db-sync/src/Cardano/DbSync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

module Cardano.DbSync
( ConfigFile (..)
, DbSyncCommand (..)
, DbSyncNodeParams (..)
, DbSyncNodePlugin (..)
, GenesisFile (..)
Expand Down
1 change: 1 addition & 0 deletions cardano-db-sync/src/Cardano/DbSync/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

module Cardano.DbSync.Config
( ConfigFile (..)
, DbSyncCommand (..)
, DbSyncProtocol (..)
, DbSyncNodeConfig (..)
, DbSyncNodeParams (..)
Expand Down
5 changes: 5 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Config/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Cardano.DbSync.Config.Types
( CardanoBlock
, CardanoProtocol
, ConfigFile (..)
, DbSyncCommand (..)
, DbSyncEnv (..)
, DbSyncNodeParams (..)
, DbSyncProtocol (..)
Expand Down Expand Up @@ -67,6 +68,10 @@ newtype ConfigFile = ConfigFile
{ unConfigFile :: FilePath
}

data DbSyncCommand
= CmdRun !DbSyncNodeParams
| CmdVersion

data DbSyncEnv = DbSyncEnv
{ envProtocol :: !DbSyncProtocol
, envNetwork :: !Shelley.Network
Expand Down