ghcup-hs/app/ghcup-gen/Main.hs

119 lines
3.3 KiB
Haskell
Raw Normal View History

2020-04-09 17:53:22 +00:00
{-# LANGUAGE CPP #-}
2020-01-11 20:15:05 +00:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
2020-01-11 20:15:05 +00:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# 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
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 )
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
| ValidateTarballs ValidateYAMLOpts
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
<$> (strOption
(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
opts :: Parser Options
opts = Options <$> com
com :: Parser Command
com = subparser
( (command
"check"
2020-08-09 15:39:02 +00:00
( ValidateYAML
<$> (info (validateYAMLOpts <**> helper)
(progDesc "Validate the YAML")
2020-01-11 20:15:05 +00:00
)
)
)
<> (command
"check-tarballs"
( ValidateTarballs
<$> (info
2020-08-09 15:39:02 +00:00
(validateYAMLOpts <**> helper)
2020-01-11 20:15:05 +00:00
(progDesc "Validate all tarballs (download and checksum)")
)
)
)
)
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
2020-01-11 20:15:05 +00:00
ValidateTarballs vopts -> case vopts of
2020-08-09 15:39:02 +00:00
ValidateYAMLOpts { vInput = Nothing } ->
B.getContents >>= valAndExit validateTarballs
ValidateYAMLOpts { vInput = Just StdInput } ->
B.getContents >>= valAndExit validateTarballs
ValidateYAMLOpts { vInput = Just (FileInput file) } ->
B.readFile file >>= valAndExit validateTarballs
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