module Cardano.Chairman.Commands.Version
  ( VersionOptions(..)
  , cmdVersion
  , runVersionOptions
  ) where

import           Cardano.Config.Git.Rev (gitRev)
import           Data.Eq
import           Data.Function
import           Data.Monoid
import           Data.Version (showVersion)
import           Options.Applicative
import           Paths_cardano_node_chairman (version)
import           System.Info (arch, compilerName, compilerVersion, os)
import           System.IO (IO)
import           Text.Show

import qualified Data.Text as T
import qualified System.IO as IO

data VersionOptions = VersionOptions deriving (VersionOptions -> VersionOptions -> Bool
(VersionOptions -> VersionOptions -> Bool)
-> (VersionOptions -> VersionOptions -> Bool) -> Eq VersionOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VersionOptions -> VersionOptions -> Bool
$c/= :: VersionOptions -> VersionOptions -> Bool
== :: VersionOptions -> VersionOptions -> Bool
$c== :: VersionOptions -> VersionOptions -> Bool
Eq, Int -> VersionOptions -> ShowS
[VersionOptions] -> ShowS
VersionOptions -> String
(Int -> VersionOptions -> ShowS)
-> (VersionOptions -> String)
-> ([VersionOptions] -> ShowS)
-> Show VersionOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VersionOptions] -> ShowS
$cshowList :: [VersionOptions] -> ShowS
show :: VersionOptions -> String
$cshow :: VersionOptions -> String
showsPrec :: Int -> VersionOptions -> ShowS
$cshowsPrec :: Int -> VersionOptions -> ShowS
Show)

optsVersion :: Parser VersionOptions
optsVersion :: Parser VersionOptions
optsVersion = VersionOptions -> Parser VersionOptions
forall (f :: * -> *) a. Applicative f => a -> f a
pure VersionOptions
VersionOptions

runVersionOptions :: VersionOptions -> IO ()
runVersionOptions :: VersionOptions -> IO ()
runVersionOptions VersionOptions
VersionOptions = do
  String -> IO ()
IO.putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall a. Monoid a => [a] -> a
mconcat
    [ String
"cardano-node ", Version -> String
showVersion Version
version
    , String
" - ", String
os, String
"-", String
arch
    , String
" - ", String
compilerName, String
"-", Version -> String
showVersion Version
compilerVersion
    , String
"\ngit rev ", Text -> String
T.unpack Text
gitRev
    ]

cmdVersion :: Mod CommandFields (IO ())
cmdVersion :: Mod CommandFields (IO ())
cmdVersion = String -> ParserInfo (IO ()) -> Mod CommandFields (IO ())
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"version" (ParserInfo (IO ()) -> Mod CommandFields (IO ()))
-> ParserInfo (IO ()) -> Mod CommandFields (IO ())
forall a b. (a -> b) -> a -> b
$ (Parser (IO ()) -> InfoMod (IO ()) -> ParserInfo (IO ()))
-> InfoMod (IO ()) -> Parser (IO ()) -> ParserInfo (IO ())
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser (IO ()) -> InfoMod (IO ()) -> ParserInfo (IO ())
forall a. Parser a -> InfoMod a -> ParserInfo a
info InfoMod (IO ())
forall m. Monoid m => m
idm (Parser (IO ()) -> ParserInfo (IO ()))
-> Parser (IO ()) -> ParserInfo (IO ())
forall a b. (a -> b) -> a -> b
$ VersionOptions -> IO ()
runVersionOptions (VersionOptions -> IO ())
-> Parser VersionOptions -> Parser (IO ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser VersionOptions
optsVersion