|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +-- Copyright 2020 United States Government as represented by the Administrator |
| 3 | +-- of the National Aeronautics and Space Administration. All Rights Reserved. |
| 4 | +-- |
| 5 | +-- Disclaimers |
| 6 | +-- |
| 7 | +-- No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY |
| 8 | +-- OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT |
| 9 | +-- LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO |
| 10 | +-- SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 11 | +-- PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE |
| 12 | +-- SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF |
| 13 | +-- PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN |
| 14 | +-- ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR |
| 15 | +-- RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR |
| 16 | +-- ANY OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, |
| 17 | +-- GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING |
| 18 | +-- THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES |
| 19 | +-- IT "AS IS." |
| 20 | +-- |
| 21 | +-- Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST |
| 22 | +-- THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS |
| 23 | +-- ANY PRIOR RECIPIENT. IF RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN |
| 24 | +-- ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM SUCH USE, |
| 25 | +-- INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S |
| 26 | +-- USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE |
| 27 | +-- UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY |
| 28 | +-- PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY |
| 29 | +-- FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS |
| 30 | +-- AGREEMENT. |
| 31 | +-- |
| 32 | +-- | CLI interface to the Overview subcommand. |
| 33 | +module CLI.CommandOverview |
| 34 | + ( |
| 35 | + -- * Direct command access |
| 36 | + command |
| 37 | + , CommandOpts |
| 38 | + , ErrorCode |
| 39 | + |
| 40 | + -- * CLI |
| 41 | + , commandDesc |
| 42 | + , commandOptsParser |
| 43 | + ) |
| 44 | + where |
| 45 | + |
| 46 | +-- External imports |
| 47 | +import Data.Aeson (toJSON) |
| 48 | +import qualified Data.Text.Lazy as T |
| 49 | +import qualified Data.Text.Lazy.IO as T |
| 50 | +import Options.Applicative (Parser, help, long, metavar, optional, |
| 51 | + short, showDefault, strOption, value) |
| 52 | +import Text.Microstache |
| 53 | + |
| 54 | +-- External imports: command results |
| 55 | +import Command.Result ( Result(..) ) |
| 56 | + |
| 57 | +-- External imports: actions or commands supported |
| 58 | +import Command.Overview (ErrorCode) |
| 59 | +import qualified Command.Overview |
| 60 | + |
| 61 | +-- * Command |
| 62 | + |
| 63 | +-- | Options to generate an overview from the input specification(s). |
| 64 | +data CommandOpts = CommandOpts |
| 65 | + { overviewFileName :: FilePath |
| 66 | + , overviewFormat :: String |
| 67 | + , overviewPropFormat :: String |
| 68 | + , overviewPropVia :: Maybe String |
| 69 | + } |
| 70 | + |
| 71 | +-- | Print an overview of the input specification(s). |
| 72 | +command :: CommandOpts -> IO (Result ErrorCode) |
| 73 | +command c = do |
| 74 | + (mOutput, result) <- |
| 75 | + Command.Overview.command (overviewFileName c) internalCommandOpts |
| 76 | + |
| 77 | + case (mOutput, outputString) of |
| 78 | + (Just output, Right template) -> |
| 79 | + T.putStr $ renderMustache template (toJSON output) |
| 80 | + _ -> putStrLn "Error" |
| 81 | + return result |
| 82 | + |
| 83 | + where |
| 84 | + internalCommandOpts :: Command.Overview.CommandOptions |
| 85 | + internalCommandOpts = Command.Overview.CommandOptions |
| 86 | + { Command.Overview.commandFormat = overviewFormat c |
| 87 | + , Command.Overview.commandPropFormat = overviewPropFormat c |
| 88 | + , Command.Overview.commandPropVia = overviewPropVia c |
| 89 | + } |
| 90 | + |
| 91 | + outputString = compileMustacheText "output" $ T.unlines |
| 92 | + [ "The file has:" |
| 93 | + , " - {{commandExternalVariables}} external variables." |
| 94 | + , " - {{commandInternalVariables}} internal variables." |
| 95 | + , " - {{commandRequirements}} requirements." |
| 96 | + ] |
| 97 | + |
| 98 | +-- * CLI |
| 99 | + |
| 100 | +-- | Command description for CLI help. |
| 101 | +commandDesc :: String |
| 102 | +commandDesc = "Generate an overview of the input specification(s)" |
| 103 | + |
| 104 | +-- | Subparser for the @overview@ command, used to generate an overview |
| 105 | +-- of the input specifications. |
| 106 | +commandOptsParser :: Parser CommandOpts |
| 107 | +commandOptsParser = CommandOpts |
| 108 | + <$> strOption |
| 109 | + ( long "file-name" |
| 110 | + <> metavar "FILENAME" |
| 111 | + <> help strOverviewFilenameDesc |
| 112 | + ) |
| 113 | + <*> strOption |
| 114 | + ( long "input-format" |
| 115 | + <> short 'f' |
| 116 | + <> metavar "FORMAT_NAME" |
| 117 | + <> help strOverviewFormatDesc |
| 118 | + <> showDefault |
| 119 | + <> value "fcs" |
| 120 | + ) |
| 121 | + <*> strOption |
| 122 | + ( long "prop-format" |
| 123 | + <> short 'p' |
| 124 | + <> metavar "FORMAT_NAME" |
| 125 | + <> help strOverviewPropFormatDesc |
| 126 | + <> showDefault |
| 127 | + <> value "smv" |
| 128 | + ) |
| 129 | + <*> optional |
| 130 | + ( strOption |
| 131 | + ( long "parse-prop-via" |
| 132 | + <> metavar "COMMAND" |
| 133 | + <> help strOverviewPropViaDesc |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | +-- | Filename flag description. |
| 138 | +strOverviewFilenameDesc :: String |
| 139 | +strOverviewFilenameDesc = "File with properties or requirements" |
| 140 | + |
| 141 | +-- | Format flag description. |
| 142 | +strOverviewFormatDesc :: String |
| 143 | +strOverviewFormatDesc = "Format of the input file" |
| 144 | + |
| 145 | +-- | Property format flag description. |
| 146 | +strOverviewPropFormatDesc :: String |
| 147 | +strOverviewPropFormatDesc = "Format of temporal or boolean properties" |
| 148 | + |
| 149 | +-- | External command to pre-process individual properties. |
| 150 | +strOverviewPropViaDesc :: String |
| 151 | +strOverviewPropViaDesc = |
| 152 | + "Command to pre-process individual properties" |
0 commit comments