module Ecluse.CLI (
AppCommand (..),
commandParser,
execCLI,
) where
import Data.Scientific (Scientific, base10Exponent)
import Options.Applicative
import Ecluse.Composition.Types (MirrorRole (MirrorOnly, ServeAndMirror, ServeOnly))
import Ecluse.Core.BuildIdentity (productVersion)
import Ecluse.Core.Osv.Publication (ObjectRevision (ObjectRevision), Percent, Rebaseline (Rebaseline), mkPercent)
import Ecluse.Dredger.Plan (
DredgerOptions (DredgerOptions),
SweepMode (SweepDeletes, SweepPreviews),
SweepRepetition (SweepContinuously, SweepOnce),
)
import Ecluse.Pilot (PilotCompileOptions (..), UploadRequest (..))
data AppCommand
=
RunService MirrorRole
| RunPilot
| RunPilotCompile PilotCompileOptions
|
RunDredger DredgerOptions
| RunCheckConfig
deriving stock (AppCommand -> AppCommand -> Bool
(AppCommand -> AppCommand -> Bool)
-> (AppCommand -> AppCommand -> Bool) -> Eq AppCommand
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AppCommand -> AppCommand -> Bool
== :: AppCommand -> AppCommand -> Bool
$c/= :: AppCommand -> AppCommand -> Bool
/= :: AppCommand -> AppCommand -> Bool
Eq, Int -> AppCommand -> ShowS
[AppCommand] -> ShowS
AppCommand -> String
(Int -> AppCommand -> ShowS)
-> (AppCommand -> String)
-> ([AppCommand] -> ShowS)
-> Show AppCommand
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AppCommand -> ShowS
showsPrec :: Int -> AppCommand -> ShowS
$cshow :: AppCommand -> String
show :: AppCommand -> String
$cshowList :: [AppCommand] -> ShowS
showList :: [AppCommand] -> ShowS
Show)
commandParser :: Parser AppCommand
commandParser :: Parser AppCommand
commandParser =
Mod CommandFields AppCommand -> Parser AppCommand
forall a. Mod CommandFields a -> Parser a
hsubparser
( String -> ParserInfo AppCommand -> Mod CommandFields AppCommand
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"proxy" (Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (MirrorRole -> AppCommand
RunService (MirrorRole -> AppCommand)
-> Parser MirrorRole -> Parser AppCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser MirrorRole
proxyRoleParser) (String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Run the Écluse proxy server"))
Mod CommandFields AppCommand
-> Mod CommandFields AppCommand -> Mod CommandFields AppCommand
forall a. Semigroup a => a -> a -> a
<> String -> ParserInfo AppCommand -> Mod CommandFields AppCommand
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"mirror" (Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (AppCommand -> Parser AppCommand
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorRole -> AppCommand
RunService MirrorRole
MirrorOnly)) (String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Run the Écluse mirror worker alone, for a worker fleet scaled on queue depth"))
Mod CommandFields AppCommand
-> Mod CommandFields AppCommand -> Mod CommandFields AppCommand
forall a. Semigroup a => a -> a -> a
<> String -> ParserInfo AppCommand -> Mod CommandFields AppCommand
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"pilot" (Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info Parser AppCommand
pilotCommandParser (String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Run the Écluse Pilot (OSV ingestion pipeline)"))
Mod CommandFields AppCommand
-> Mod CommandFields AppCommand -> Mod CommandFields AppCommand
forall a. Semigroup a => a -> a -> a
<> String -> ParserInfo AppCommand -> Mod CommandFields AppCommand
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"dredger" (Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (DredgerOptions -> AppCommand
RunDredger (DredgerOptions -> AppCommand)
-> Parser DredgerOptions -> Parser AppCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser DredgerOptions
dredgerOptionsParser) (String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Run the Écluse Dredger (mirror pruning worker)"))
Mod CommandFields AppCommand
-> Mod CommandFields AppCommand -> Mod CommandFields AppCommand
forall a. Semigroup a => a -> a -> a
<> String -> ParserInfo AppCommand -> Mod CommandFields AppCommand
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"check-config" (Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (AppCommand -> Parser AppCommand
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AppCommand
RunCheckConfig) (String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Validate the configuration and print the resolved posture, then exit (0 valid, 2 refused)"))
)
Parser AppCommand -> Parser AppCommand -> Parser AppCommand
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AppCommand -> Parser AppCommand
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorRole -> AppCommand
RunService MirrorRole
ServeAndMirror)
proxyRoleParser :: Parser MirrorRole
proxyRoleParser :: Parser MirrorRole
proxyRoleParser =
MirrorRole
-> MirrorRole -> Mod FlagFields MirrorRole -> Parser MirrorRole
forall a. a -> a -> Mod FlagFields a -> Parser a
flag
MirrorRole
ServeAndMirror
MirrorRole
ServeOnly
(String -> Mod FlagFields MirrorRole
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"no-worker" Mod FlagFields MirrorRole
-> Mod FlagFields MirrorRole -> Mod FlagFields MirrorRole
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields MirrorRole
forall (f :: * -> *) a. String -> Mod f a
help String
"Serve without the embedded mirror worker; needs a durable ECLUSE_QUEUE__URL and an 'ecluse mirror' fleet to drain it")
dredgerOptionsParser :: Parser DredgerOptions
dredgerOptionsParser :: Parser DredgerOptions
dredgerOptionsParser =
SweepMode -> SweepRepetition -> DredgerOptions
DredgerOptions
(SweepMode -> SweepRepetition -> DredgerOptions)
-> Parser SweepMode -> Parser (SweepRepetition -> DredgerOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SweepMode
-> SweepMode -> Mod FlagFields SweepMode -> Parser SweepMode
forall a. a -> a -> Mod FlagFields a -> Parser a
flag
SweepMode
SweepDeletes
SweepMode
SweepPreviews
(String -> Mod FlagFields SweepMode
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"dry-run" Mod FlagFields SweepMode
-> Mod FlagFields SweepMode -> Mod FlagFields SweepMode
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields SweepMode
forall (f :: * -> *) a. String -> Mod f a
help String
"Report what a cycle would delete and delete nothing; it holds no delete, writes no walk marker, and needs no deletion consent")
Parser (SweepRepetition -> DredgerOptions)
-> Parser SweepRepetition -> Parser DredgerOptions
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SweepRepetition
-> SweepRepetition
-> Mod FlagFields SweepRepetition
-> Parser SweepRepetition
forall a. a -> a -> Mod FlagFields a -> Parser a
flag
SweepRepetition
SweepContinuously
SweepRepetition
SweepOnce
(String -> Mod FlagFields SweepRepetition
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"once" Mod FlagFields SweepRepetition
-> Mod FlagFields SweepRepetition -> Mod FlagFields SweepRepetition
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields SweepRepetition
forall (f :: * -> *) a. String -> Mod f a
help String
"Run one cycle, then exit: 0 when it completed, 1 when it halted")
pilotCommandParser :: Parser AppCommand
pilotCommandParser :: Parser AppCommand
pilotCommandParser =
Mod CommandFields AppCommand -> Parser AppCommand
forall a. Mod CommandFields a -> Parser a
hsubparser
( String -> ParserInfo AppCommand -> Mod CommandFields AppCommand
forall a. String -> ParserInfo a -> Mod CommandFields a
command
String
"compile"
(Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (PilotCompileOptions -> AppCommand
RunPilotCompile (PilotCompileOptions -> AppCommand)
-> Parser PilotCompileOptions -> Parser AppCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser PilotCompileOptions
pilotCompileOptionsParser) (String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Compile one ecosystem's OSV export into a local osv.db artifact, then exit"))
)
Parser AppCommand -> Parser AppCommand -> Parser AppCommand
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AppCommand -> Parser AppCommand
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AppCommand
RunPilot
pilotCompileOptionsParser :: Parser PilotCompileOptions
pilotCompileOptionsParser :: Parser PilotCompileOptions
pilotCompileOptionsParser =
Text -> String -> UploadRequest -> PilotCompileOptions
PilotCompileOptions
(Text -> String -> UploadRequest -> PilotCompileOptions)
-> Parser Text
-> Parser (String -> UploadRequest -> PilotCompileOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mod OptionFields Text -> Parser Text
forall s. IsString s => Mod OptionFields s -> Parser s
strOption (String -> Mod OptionFields Text
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"ecosystem" Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Text
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"ECOSYSTEM" Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> Text -> Mod OptionFields Text
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Text
"npm" Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields Text
forall a (f :: * -> *). Show a => Mod f a
showDefault Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Text
forall (f :: * -> *) a. String -> Mod f a
help String
"Ecosystem whose OSV export to compile")
Parser (String -> UploadRequest -> PilotCompileOptions)
-> Parser String -> Parser (UploadRequest -> PilotCompileOptions)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod OptionFields String -> Parser String
forall s. IsString s => Mod OptionFields s -> Parser s
strOption (String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"out" Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"DIR" Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields String
forall (f :: * -> *) a. String -> Mod f a
help String
"Directory the artifact is written into")
Parser (UploadRequest -> PilotCompileOptions)
-> Parser UploadRequest -> Parser PilotCompileOptions
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser UploadRequest
uploadRequestParser
uploadRequestParser :: Parser UploadRequest
uploadRequestParser :: Parser UploadRequest
uploadRequestParser =
() -> Mod FlagFields () -> Parser ()
forall a. a -> Mod FlagFields a -> Parser a
flag' () (String -> Mod FlagFields ()
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"upload" Mod FlagFields () -> Mod FlagFields () -> Mod FlagFields ()
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields ()
forall (f :: * -> *) a. String -> Mod f a
help String
"After compiling, publish the artifact to the configured advisory store under the publication guard")
Parser () -> Parser UploadRequest -> Parser UploadRequest
forall a b. Parser a -> Parser b -> Parser b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (UploadRequest
-> (Rebaseline -> UploadRequest)
-> Maybe Rebaseline
-> UploadRequest
forall b a. b -> (a -> b) -> Maybe a -> b
maybe UploadRequest
Upload Rebaseline -> UploadRequest
UploadRebaselined (Maybe Rebaseline -> UploadRequest)
-> Parser (Maybe Rebaseline) -> Parser UploadRequest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Rebaseline -> Parser (Maybe Rebaseline)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser Rebaseline
rebaselineParser)
Parser UploadRequest
-> Parser UploadRequest -> Parser UploadRequest
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> UploadRequest -> Parser UploadRequest
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure UploadRequest
NoUpload
rebaselineParser :: Parser Rebaseline
rebaselineParser :: Parser Rebaseline
rebaselineParser =
(ObjectRevision -> Percent -> Rebaseline
Rebaseline (ObjectRevision -> Percent -> Rebaseline)
-> (Text -> ObjectRevision) -> Text -> Percent -> Rebaseline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ObjectRevision
ObjectRevision (Text -> Percent -> Rebaseline)
-> Parser Text -> Parser (Percent -> Rebaseline)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mod OptionFields Text -> Parser Text
forall s. IsString s => Mod OptionFields s -> Parser s
strOption (String -> Mod OptionFields Text
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"rebaseline-etag" Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Text
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"ETAG" Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Text
forall (f :: * -> *) a. String -> Mod f a
help String
"Replace only the stored object with this ETag, then restart the size baseline at the new artifact"))
Parser (Percent -> Rebaseline)
-> Parser Percent -> Parser Rebaseline
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM Percent -> Mod OptionFields Percent -> Parser Percent
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Percent
percentReader (String -> Mod OptionFields Percent
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"rebaseline-max-shrink-percent" Mod OptionFields Percent
-> Mod OptionFields Percent -> Mod OptionFields Percent
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Percent
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"PERCENT" Mod OptionFields Percent
-> Mod OptionFields Percent -> Mod OptionFields Percent
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Percent
forall (f :: * -> *) a. String -> Mod f a
help String
"The shrink this one rebaselining upload may take, from 0 to 100")
percentReader :: ReadM Percent
percentReader :: ReadM Percent
percentReader = (String -> Either String Percent) -> ReadM Percent
forall a. (String -> Either String a) -> ReadM a
eitherReader ((String -> Either String Percent) -> ReadM Percent)
-> (String -> Either String Percent) -> ReadM Percent
forall a b. (a -> b) -> a -> b
$ \String
raw -> case String -> Maybe Scientific
forall a. Read a => String -> Maybe a
readMaybe String
raw :: Maybe Scientific of
Just Scientific
n | Int -> Int
forall a. Num a => a -> a
abs (Scientific -> Int
base10Exponent Scientific
n) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
9, Just Percent
percent <- Rational -> Maybe Percent
mkPercent (Scientific -> Rational
forall a. Real a => a -> Rational
toRational Scientific
n) -> Percent -> Either String Percent
forall a b. b -> Either a b
Right Percent
percent
Maybe Scientific
_ -> String -> Either String Percent
forall a b. a -> Either a b
Left String
"expected a percentage from 0 to 100"
execCLI :: IO AppCommand
execCLI :: IO AppCommand
execCLI =
ParserInfo AppCommand -> IO AppCommand
forall a. ParserInfo a -> IO a
execParser (ParserInfo AppCommand -> IO AppCommand)
-> ParserInfo AppCommand -> IO AppCommand
forall a b. (a -> b) -> a -> b
$
Parser AppCommand -> InfoMod AppCommand -> ParserInfo AppCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info
(Parser AppCommand
commandParser Parser AppCommand
-> Parser (AppCommand -> AppCommand) -> Parser AppCommand
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser (AppCommand -> AppCommand)
forall a. Parser (a -> a)
helper Parser AppCommand
-> Parser (AppCommand -> AppCommand) -> Parser AppCommand
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser (AppCommand -> AppCommand)
forall a. Parser (a -> a)
versionOption)
( InfoMod AppCommand
forall a. InfoMod a
fullDesc
InfoMod AppCommand -> InfoMod AppCommand -> InfoMod AppCommand
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod AppCommand
forall a. String -> InfoMod a
progDesc String
"Écluse - supply-chain resilience proxy"
InfoMod AppCommand -> InfoMod AppCommand -> InfoMod AppCommand
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod AppCommand
forall a. String -> InfoMod a
header String
"ecluse - a configurable policy gate for package registries"
)
where
versionOption :: Parser (a -> a)
versionOption = String -> Mod OptionFields (a -> a) -> Parser (a -> a)
forall a. String -> Mod OptionFields (a -> a) -> Parser (a -> a)
infoOption (Text -> String
forall a. ToString a => a -> String
toString Text
productVersion) (String -> Mod OptionFields (a -> a)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"version" Mod OptionFields (a -> a)
-> Mod OptionFields (a -> a) -> Mod OptionFields (a -> a)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (a -> a)
forall (f :: * -> *) a. String -> Mod f a
help String
"Show version")