Skip to content

Commit

Permalink
[chore] some tiny improvements
Browse files Browse the repository at this point in the history
- always show help on empty comand
- use `hsubparser`
  • Loading branch information
MangoIV committed Feb 4, 2024
1 parent 23c7af5 commit 3233d1f
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions code/hsec-tools/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ import Security.Advisories.Generate.HTML
import qualified Command.Reserve

main :: IO ()
main = join $ execParser cliOpts
main = join $
customExecParser
(prefs showHelpOnEmpty)
cliOpts

cliOpts :: ParserInfo (IO ())
cliOpts = info (commandsParser <**> helper) (fullDesc <> header "Haskell Advisories tools")
where
commandsParser :: Parser (IO ())
commandsParser =
subparser
hsubparser
( command "check" (info commandCheck (progDesc "Syntax check a single advisory"))
<> command "reserve" (info commandReserve (progDesc "Reserve an HSEC ID"))
<> command "osv" (info commandOsv (progDesc "Convert a single advisory to OSV"))
Expand Down Expand Up @@ -70,13 +73,11 @@ commandReserve =
( long "commit"
<> help "Commit the reservation file"
)
<**> helper

commandCheck :: Parser (IO ())
commandCheck =
withAdvisory go
<$> optional (argument str (metavar "FILE"))
<**> helper
where
go mPath advisory = do
for_ mPath $ \path -> do
Expand All @@ -89,7 +90,6 @@ commandOsv :: Parser (IO ())
commandOsv =
withAdvisory go
<$> optional (argument str (metavar "FILE"))
<**> helper
where
go _ adv = do
L.putStr (Data.Aeson.encode (OSV.convert adv))
Expand All @@ -99,22 +99,19 @@ commandRender :: Parser (IO ())
commandRender =
withAdvisory (\_ -> T.putStrLn . advisoryHtml)
<$> optional (argument str (metavar "FILE"))
<**> helper

commandQuery :: Parser (IO ())
commandQuery =
subparser
( command "is-affected" (info isAffected (progDesc "Check if a package/version range is marked vulnerable"))
)
<**> helper
where
isAffected :: Parser (IO ())
isAffected =
go
<$> argument str (metavar "PACKAGE")
<*> optional (option versionRangeReader (metavar "VERSION-RANGE" <> short 'v' <> long "version-range"))
<*> optional (option str (metavar "ADVISORIES-PATH" <> short 'p' <> long "advisories-path"))
<**> helper
where go :: T.Text -> Maybe VersionRange -> Maybe FilePath -> IO ()
go packageName versionRange advisoriesPath = do
let versionRange' = fromMaybe anyVersion versionRange
Expand All @@ -140,7 +137,6 @@ commandGenerateIndex =
)
<$> argument str (metavar "SOURCE-DIR")
<*> argument str (metavar "DESTINATION-DIR")
<**> helper

commandHelp :: Parser (IO ())
commandHelp =
Expand All @@ -149,7 +145,6 @@ commandHelp =
in void $ handleParseResult $ execParserPure defaultPrefs cliOpts args
)
<$> optional (argument str (metavar "COMMAND"))
<**> helper

versionRangeReader :: ReadM VersionRange
versionRangeReader = eitherReader eitherParsec
Expand Down

0 comments on commit 3233d1f

Please sign in to comment.