never executed always true always false
    1 {-# LANGUAGE FlexibleContexts #-}
    2 {-# LANGUAGE LambdaCase #-}
    3 
    4 {-# OPTIONS_HADDOCK hide #-}
    5 
    6 module Command
    7     ( CLI
    8 
    9     -- * I/O interface
   10     , setup
   11     , parse
   12     , run
   13     ) where
   14 
   15 import Prelude
   16 
   17 import Control.Exception
   18     ( handle )
   19 import Control.Monad
   20     ( void )
   21 import Options.Applicative
   22     ( ParserInfo
   23     , customExecParser
   24     , footerDoc
   25     , helper
   26     , info
   27     , prefs
   28     , progDesc
   29     , showHelpOnEmpty
   30     , subparser
   31     , (<|>)
   32     )
   33 import Options.Applicative.Help.Pretty
   34     ( bold, hsep, string, vsep )
   35 import System.Console.ANSI
   36     ( hSupportsANSIWithoutEmulation )
   37 import System.IO
   38     ( BufferMode (..), Handle, hSetBuffering, stderr, stdin, stdout )
   39 import System.IO.Extra
   40     ( prettyIOException, progName )
   41 
   42 import qualified Command.Address as Address
   43 import qualified Command.Key as Key
   44 import qualified Command.RecoveryPhrase as RecoveryPhrase
   45 import qualified Command.Script as Script
   46 import qualified Command.Version as Version
   47 
   48 data CLI
   49     = RecoveryPhrase RecoveryPhrase.Cmd
   50     | Key Key.Cmd
   51     | Address Address.Cmd
   52     | Script Script.Cmd
   53     | Version
   54     deriving (Show)
   55 
   56 cli :: ParserInfo CLI
   57 cli = info (helper <*> parser) $ mempty
   58     <> progDesc "Command-line for address and key manipulation in Cardano."
   59     <> footerDoc (Just $ vsep
   60         [ string "💡 Need auto-completion?"
   61         , string ""
   62         , hsep
   63             [ string "  ↳"
   64             , bold $ string "source <("
   65             , bold $ string progName
   66             , bold $ string $ "--bash-completion-script `which "<>progName<>"`)"
   67             ]
   68         , string ""
   69         , string "Or alternatively --fish-completion-script / --zsh-completion-script."
   70         , string "For a long-term solution, you may want to put this script in the relevant place. e.g.:"
   71         , string ""
   72         , hsep [string "  ↳", bold $ string "/etc/bash_completion.d"]
   73         ])
   74   where
   75     parser = Version.opt Version <|> subparser (mconcat
   76         [ RecoveryPhrase.mod RecoveryPhrase
   77         , Key.mod Key
   78         , Address.mod Address
   79         , Script.mod Script
   80         ])
   81 
   82 -- | Run a given command
   83 run :: CLI -> IO ()
   84 run = handle prettyIOException . \case
   85     RecoveryPhrase sub -> RecoveryPhrase.run sub
   86     Key sub -> Key.run sub
   87     Address sub -> Address.run sub
   88     Script sub -> Script.run sub
   89     Version -> Version.run
   90 
   91 -- | Parse command line options and arguments
   92 parse :: IO CLI
   93 parse = customExecParser (prefs showHelpOnEmpty) cli
   94 
   95 -- | Enable ANSI colors on Windows and correct output buffering
   96 setup :: IO ()
   97 setup =
   98     mapM_ hSetup [stderr, stdout, stdin]
   99   where
  100     hSetup :: Handle -> IO ()
  101     hSetup h = do
  102       void $ hSupportsANSIWithoutEmulation h
  103       hSetBuffering h NoBuffering