Skip to content

Commit

Permalink
Merge pull request #373 from input-output-hk/rvl/cli-version
Browse files Browse the repository at this point in the history
cli: Use Paths module to get program version
  • Loading branch information
KtorZ authored Jun 6, 2019
2 parents 853964b + 082a72e commit 24eb7a7
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 44 deletions.
4 changes: 4 additions & 0 deletions cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ executable cardano-wallet
, text
, text-class
, warp
other-modules:
Paths_cardano_wallet
hs-source-dirs:
exe/wallet
main-is:
Expand Down Expand Up @@ -72,6 +74,8 @@ executable cardano-wallet-launcher
, say
, text
, text-class
other-modules:
Paths_cardano_wallet
hs-source-dirs:
exe/launcher
main-is:
Expand Down
12 changes: 9 additions & 3 deletions exe/launcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Main where
import Prelude

import Cardano.CLI
( Port, help, parseArgWith, showVersion )
( Port, help, parseArgWith )
import Cardano.Launcher
( Command (Command)
, ProcessHasExited (ProcessHasExited)
Expand All @@ -22,8 +22,12 @@ import Control.Monad
( when )
import Data.Text.Class
( FromText (..), ToText (..) )
import Data.Version
( showVersion )
import Fmt
( blockListF, fmt )
import Paths_cardano_wallet
( version )
import Say
( sayErr )
import System.Console.Docopt
Expand All @@ -39,7 +43,7 @@ import System.Console.Docopt
import System.Environment
( getArgs )
import System.Exit
( exitWith )
( exitSuccess, exitWith )

import qualified Data.Text as T

Expand Down Expand Up @@ -72,7 +76,9 @@ main = do
args <- parseArgsOrExit cli =<< getArgs
when (args `isPresent` (longOption "help")) $ help cli
when (args `isPresent` (shortOption 'h')) $ help cli
when (args `isPresent` (longOption "version")) showVersion
when (args `isPresent` (longOption "version")) $ do
putStrLn (showVersion version)
exitSuccess

network <- args `parseArg` longOption "network"
bridgePort <- args `parseArg` longOption "http-bridge-port"
Expand Down
11 changes: 8 additions & 3 deletions exe/wallet/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import Cardano.CLI
, parseArgWith
, putErrLn
, setUtf8Encoding
, showVersion
)
import Cardano.Wallet
( newWalletLayer )
Expand Down Expand Up @@ -78,8 +77,12 @@ import Data.Text
( Text )
import Data.Text.Class
( FromText (..), ToText (..) )
import Data.Version
( showVersion )
import Network.HTTP.Client
( Manager, defaultManagerSettings, newManager )
import Paths_cardano_wallet
( version )
import Servant
( (:<|>) (..), (:>) )
import Servant.Client
Expand All @@ -102,7 +105,7 @@ import System.Console.Docopt
import System.Environment
( getArgs )
import System.Exit
( exitFailure )
( exitFailure, exitSuccess )
import System.IO
( BufferMode (NoBuffering), hSetBuffering, stderr, stdout )

Expand Down Expand Up @@ -260,7 +263,9 @@ exec execServer manager args
wId <- args `parseArg` argument "wallet-id"
runClient Aeson.encodePretty $ listAddresses (ApiT wId) Nothing

| args `isPresent` longOption "version" = showVersion
| args `isPresent` longOption "version" = do
putStrLn (showVersion version)
exitSuccess

| otherwise =
exitWithUsage cli
Expand Down
3 changes: 0 additions & 3 deletions lib/cli/cardano-wallet-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ library
-Werror
build-depends:
base
, bytestring
, ansi-terminal
, docopt
, file-embed
, fmt
, regex-applicative
, text
, text-class
hs-source-dirs:
Expand Down
25 changes: 0 additions & 25 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}

-- |
-- Copyright: © 2018-2019 IOHK
Expand Down Expand Up @@ -34,20 +33,13 @@ module Cardano.CLI
, hGetLine
, getSensitiveLine
, hGetSensitiveLine

-- * Show version
, showVersion
) where

import Prelude hiding
( getLine )

import Control.Applicative
( many )
import Control.Exception
( bracket )
import Data.FileEmbed
( embedFile )
import Data.Functor
( (<$) )
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -96,10 +88,7 @@ import System.IO
, stdout
, utf8
)
import Text.Regex.Applicative
( anySym, few, match, string, sym )

import qualified Data.ByteString.Char8 as B8
import qualified Data.Text as T
import qualified Data.Text.IO as TIO

Expand Down Expand Up @@ -270,20 +259,6 @@ getSensitiveLine = hGetSensitiveLine (stdin, stderr)
Internals
-------------------------------------------------------------------------------}

showVersion :: IO ()
showVersion = do
let cabal = B8.unpack $(embedFile "../../cardano-wallet.cabal")
let re = few anySym
*> string "version:" *> many (sym ' ') *> few anySym
<* sym '\n' <* many anySym
case match re cabal of
Nothing -> do
putErrLn "Couldn't find program version!"
exitFailure
Just version -> do
TIO.putStrLn $ T.pack version
exitSuccess

withBuffering :: Handle -> BufferMode -> IO a -> IO a
withBuffering h buffering action = bracket aFirst aLast aBetween
where
Expand Down
1 change: 1 addition & 0 deletions lib/http-bridge/cardano-wallet-http-bridge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ test-suite integration
Test.Integration.Scenario.CLI.Mnemonics
Test.Integration.Scenario.CLI.Transactions
Test.Integration.Scenario.CLI.Wallets
Paths_cardano_wallet_http_bridge

benchmark restore
default-language:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import Control.Monad
( forM_ )
import Data.List
( length )
import Data.Text
( Text )
import Data.Version
( showVersion )
import Paths_cardano_wallet_http_bridge
( version )
import System.Command
( Exit (..), Stderr (..), Stdout (..) )
import System.Exit
Expand All @@ -24,23 +26,20 @@ import Test.Hspec.Expectations.Lifted
import Test.Integration.Framework.DSL
( cardanoWalletCLI, cardanoWalletLauncherCLI, generateMnemonicsViaCLI )

import qualified Data.Text as T

version :: Text
version = "2019.5.24"
import qualified Data.List as L

spec :: SpecWith ()
spec = do
it "CLI_VERSION - cardano-wallet shows version" $ do
(Exit c, Stdout out) <- cardanoWalletCLI ["--version"]
let v = T.dropWhileEnd (== '\n') (T.pack out)
v `shouldBe` version
let v = L.dropWhileEnd (== '\n') out
v `shouldBe` (showVersion version)
c `shouldBe` ExitSuccess

it "CLI_VERSION - cardano-wallet-launcher shows version" $ do
(Exit c, Stdout out) <- cardanoWalletLauncherCLI ["--version"]
let v = T.dropWhileEnd (== '\n') (T.pack out)
v `shouldBe` version
let v = L.dropWhileEnd (== '\n') out
v `shouldBe` (showVersion version)
c `shouldBe` ExitSuccess

it "CLI_HELP - cardano-wallet-launcher shows help on bad argument" $ do
Expand Down

0 comments on commit 24eb7a7

Please sign in to comment.