2020-04-09 17:53:22 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2020-04-13 13:25:50 +00:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2020-04-13 13:25:50 +00:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
2020-04-10 15:36:27 +00:00
|
|
|
import GHCup.Types
|
2020-01-11 20:15:05 +00:00
|
|
|
import GHCup.Types.JSON ( )
|
|
|
|
import GHCup.Utils.Logger
|
|
|
|
|
2021-01-02 06:58:08 +00:00
|
|
|
import Data.Char ( toLower )
|
2020-04-09 17:53:22 +00:00
|
|
|
#if !MIN_VERSION_base(4,13,0)
|
2020-01-11 20:15:05 +00:00
|
|
|
import Data.Semigroup ( (<>) )
|
2020-04-09 17:53:22 +00:00
|
|
|
#endif
|
2020-01-11 20:15:05 +00:00
|
|
|
import Options.Applicative hiding ( style )
|
|
|
|
import System.Console.Pretty
|
|
|
|
import System.Exit
|
|
|
|
import System.IO ( stdout )
|
2021-01-02 04:05:05 +00:00
|
|
|
import Text.Regex.Posix
|
2020-01-11 20:15:05 +00:00
|
|
|
import Validate
|
|
|
|
|
|
|
|
import qualified Data.ByteString as B
|
2020-08-09 15:39:02 +00:00
|
|
|
import qualified Data.Yaml as Y
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
data Options = Options
|
|
|
|
{ optCommand :: Command
|
|
|
|
}
|
|
|
|
|
2020-08-09 15:39:02 +00:00
|
|
|
data Command = ValidateYAML ValidateYAMLOpts
|
2021-01-02 04:53:11 +00:00
|
|
|
| ValidateTarballs ValidateYAMLOpts TarballFilter
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
data Input
|
|
|
|
= FileInput FilePath -- optsparse-applicative doesn't handle ByteString correctly anyway
|
|
|
|
| StdInput
|
|
|
|
|
|
|
|
fileInput :: Parser Input
|
|
|
|
fileInput =
|
|
|
|
FileInput
|
2021-03-11 16:03:51 +00:00
|
|
|
<$> strOption
|
2020-01-11 20:15:05 +00:00
|
|
|
(long "file" <> short 'f' <> metavar "FILENAME" <> help
|
|
|
|
"Input file to validate"
|
|
|
|
)
|
|
|
|
|
|
|
|
stdInput :: Parser Input
|
|
|
|
stdInput = flag'
|
|
|
|
StdInput
|
|
|
|
(short 'i' <> long "stdin" <> help "Validate from stdin (default)")
|
|
|
|
|
|
|
|
inputP :: Parser Input
|
|
|
|
inputP = fileInput <|> stdInput
|
|
|
|
|
2020-08-09 15:39:02 +00:00
|
|
|
data ValidateYAMLOpts = ValidateYAMLOpts
|
|
|
|
{ vInput :: Maybe Input
|
2020-01-11 20:15:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 15:39:02 +00:00
|
|
|
validateYAMLOpts :: Parser ValidateYAMLOpts
|
|
|
|
validateYAMLOpts = ValidateYAMLOpts <$> optional inputP
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2021-01-02 04:53:11 +00:00
|
|
|
tarballFilterP :: Parser TarballFilter
|
|
|
|
tarballFilterP = option readm $
|
|
|
|
long "tarball-filter" <> short 'u' <> metavar "<tool>-<version>" <> value def
|
2021-01-02 06:58:08 +00:00
|
|
|
<> help "Only check certain tarballs (format: <tool>-<version>)"
|
2021-01-02 04:53:11 +00:00
|
|
|
where
|
2021-01-02 06:58:08 +00:00
|
|
|
def = TarballFilter Nothing (makeRegex ("" :: String))
|
2021-01-02 04:53:11 +00:00
|
|
|
readm = do
|
|
|
|
s <- str
|
2021-01-02 06:58:08 +00:00
|
|
|
case span (/= '-') s of
|
|
|
|
(_, []) -> fail "invalid format, missing '-' after the tool name"
|
|
|
|
(t, v) | [tool] <- [ tool | tool <- [minBound..maxBound], low (show tool) == low t ] ->
|
2021-03-11 16:03:51 +00:00
|
|
|
pure (TarballFilter $ Just tool) <*> makeRegexOptsM compIgnoreCase execBlank (drop 1 v)
|
2021-01-02 06:58:08 +00:00
|
|
|
_ -> fail "invalid tool"
|
|
|
|
low = fmap toLower
|
|
|
|
|
2021-01-01 04:45:58 +00:00
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
opts :: Parser Options
|
|
|
|
opts = Options <$> com
|
|
|
|
|
|
|
|
com :: Parser Command
|
|
|
|
com = subparser
|
2021-03-11 16:03:51 +00:00
|
|
|
( command
|
2020-01-11 20:15:05 +00:00
|
|
|
"check"
|
2020-08-09 15:39:02 +00:00
|
|
|
( ValidateYAML
|
2021-03-11 16:03:51 +00:00
|
|
|
<$> info (validateYAMLOpts <**> helper)
|
|
|
|
(progDesc "Validate the YAML")
|
2020-01-11 20:15:05 +00:00
|
|
|
)
|
2021-03-11 16:03:51 +00:00
|
|
|
<> command
|
2020-01-11 20:15:05 +00:00
|
|
|
"check-tarballs"
|
2021-01-01 04:45:58 +00:00
|
|
|
(info
|
2021-01-02 04:53:11 +00:00
|
|
|
((ValidateTarballs <$> validateYAMLOpts <*> tarballFilterP) <**> helper)
|
2021-01-01 04:45:58 +00:00
|
|
|
(progDesc "Validate all tarballs (download and checksum)")
|
2020-01-11 20:15:05 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2020-08-09 15:39:02 +00:00
|
|
|
_ <- customExecParser (prefs showHelpOnError) (info (opts <**> helper) idm)
|
2020-01-11 20:15:05 +00:00
|
|
|
>>= \Options {..} -> case optCommand of
|
2020-08-09 15:39:02 +00:00
|
|
|
ValidateYAML vopts -> case vopts of
|
|
|
|
ValidateYAMLOpts { vInput = Nothing } ->
|
|
|
|
B.getContents >>= valAndExit validate
|
|
|
|
ValidateYAMLOpts { vInput = Just StdInput } ->
|
|
|
|
B.getContents >>= valAndExit validate
|
|
|
|
ValidateYAMLOpts { vInput = Just (FileInput file) } ->
|
|
|
|
B.readFile file >>= valAndExit validate
|
2021-01-02 04:53:11 +00:00
|
|
|
ValidateTarballs vopts tarballFilter -> case vopts of
|
2020-08-09 15:39:02 +00:00
|
|
|
ValidateYAMLOpts { vInput = Nothing } ->
|
2021-01-02 04:53:11 +00:00
|
|
|
B.getContents >>= valAndExit (validateTarballs tarballFilter)
|
2020-08-09 15:39:02 +00:00
|
|
|
ValidateYAMLOpts { vInput = Just StdInput } ->
|
2021-01-02 04:53:11 +00:00
|
|
|
B.getContents >>= valAndExit (validateTarballs tarballFilter)
|
2020-08-09 15:39:02 +00:00
|
|
|
ValidateYAMLOpts { vInput = Just (FileInput file) } ->
|
2021-01-02 04:53:11 +00:00
|
|
|
B.readFile file >>= valAndExit (validateTarballs tarballFilter)
|
2020-01-11 20:15:05 +00:00
|
|
|
pure ()
|
|
|
|
|
|
|
|
where
|
|
|
|
valAndExit f contents = do
|
2020-08-09 15:39:02 +00:00
|
|
|
(GHCupInfo _ av) <- case Y.decodeEither' contents of
|
2020-01-11 20:15:05 +00:00
|
|
|
Right r -> pure r
|
|
|
|
Left e -> die (color Red $ show e)
|
|
|
|
myLoggerT (LoggerConfig True (B.hPut stdout) (\_ -> pure ())) (f av)
|
|
|
|
>>= exitWith
|