module Shrun.Configuration.Args.Parsing.FileLogging
( fileLoggingParser,
)
where
import Effects.Optparse.Completer qualified as EOC
import Options.Applicative (Parser)
import Options.Applicative qualified as OA
import Shrun.Configuration.Args.Parsing.Utils qualified as Utils
import Shrun.Configuration.Data.FileLogging
( DeleteOnSuccessSwitch (MkDeleteOnSuccessSwitch),
FileLogInitP
( MkFileLogInitP,
path,
sizeMode
),
FileLogMultiSwitch (MkFileLogMultiSwitch),
FileLoggingArgs,
FileLoggingP
( MkFileLoggingP,
commandNameTrunc,
deleteOnSuccess,
file,
lineTrunc,
mode,
multi,
stripControl
),
)
import Shrun.Configuration.Data.FileLogging.FileMode (FileMode)
import Shrun.Configuration.Data.FileLogging.FileMode qualified as FileMode
import Shrun.Configuration.Data.FileLogging.FilePathDefault (FilePathDefault)
import Shrun.Configuration.Data.FileLogging.FilePathDefault qualified as FilePathDefault
import Shrun.Configuration.Data.FileLogging.FileSizeMode (FileSizeMode)
import Shrun.Configuration.Data.FileLogging.FileSizeMode qualified as FileSizeMode
import Shrun.Configuration.Data.StripControl (FileLogStripControl)
import Shrun.Configuration.Data.StripControl qualified as StripControl
import Shrun.Configuration.Data.Truncation
( LineTruncation,
TruncRegion (TruncCommandName),
Truncation,
)
import Shrun.Configuration.Data.Truncation qualified as Trunc
import Shrun.Configuration.Data.WithDisabled (WithDisabled)
import Shrun.Prelude
import Shrun.Utils qualified as ShrunUtils
fileLoggingParser :: Parser FileLoggingArgs
fileLoggingParser :: Parser FileLoggingArgs
fileLoggingParser = do
Maybe (WithDisabled FilePathDefault)
path <- Parser (Maybe (WithDisabled FilePathDefault))
fileLogParser
Maybe (WithDisabled (Truncation 'TruncCommandName))
commandNameTrunc <- Parser (Maybe (WithDisabled (Truncation 'TruncCommandName)))
fileLogCommandNameTruncParser
Maybe DeleteOnSuccessSwitch
deleteOnSuccess <- Parser (Maybe DeleteOnSuccessSwitch)
deleteOnSuccessParser
Maybe (WithDisabled LineTruncation)
lineTrunc <- Parser (Maybe (WithDisabled LineTruncation))
lineTruncParser
Maybe FileMode
mode <- Parser (Maybe FileMode)
fileLogModeParser
Maybe FileLogMultiSwitch
multi <- Parser (Maybe FileLogMultiSwitch)
multiParser
Maybe FileSizeMode
sizeMode <- Parser (Maybe FileSizeMode)
fileLogSizeModeParser
Maybe (StripControl 'StripControlFileLog)
stripControl <- Parser (Maybe (StripControl 'StripControlFileLog))
fileLogStripControlParser
pure
$ MkFileLoggingP
{ file :: FileLogFileF 'ConfigPhaseArgs
file =
MkFileLogInitP
{ Maybe (WithDisabled FilePathDefault)
FileLogPathF 'ConfigPhaseArgs
path :: FileLogPathF 'ConfigPhaseArgs
path :: Maybe (WithDisabled FilePathDefault)
path,
Maybe FileSizeMode
ConfigPhaseF 'ConfigPhaseArgs FileSizeMode
sizeMode :: ConfigPhaseF 'ConfigPhaseArgs FileSizeMode
sizeMode :: Maybe FileSizeMode
sizeMode
},
Maybe (WithDisabled (Truncation 'TruncCommandName))
ConfigPhaseDisabledMaybeF
'ConfigPhaseArgs (Truncation 'TruncCommandName)
commandNameTrunc :: ConfigPhaseDisabledMaybeF
'ConfigPhaseArgs (Truncation 'TruncCommandName)
commandNameTrunc :: Maybe (WithDisabled (Truncation 'TruncCommandName))
commandNameTrunc,
Maybe DeleteOnSuccessSwitch
SwitchF 'ConfigPhaseArgs DeleteOnSuccessSwitch
deleteOnSuccess :: SwitchF 'ConfigPhaseArgs DeleteOnSuccessSwitch
deleteOnSuccess :: Maybe DeleteOnSuccessSwitch
deleteOnSuccess,
Maybe (WithDisabled LineTruncation)
LineTruncF 'ConfigPhaseArgs
lineTrunc :: LineTruncF 'ConfigPhaseArgs
lineTrunc :: Maybe (WithDisabled LineTruncation)
lineTrunc,
Maybe FileMode
ConfigPhaseF 'ConfigPhaseArgs FileMode
mode :: ConfigPhaseF 'ConfigPhaseArgs FileMode
mode :: Maybe FileMode
mode,
Maybe FileLogMultiSwitch
FileLogMultiF 'ConfigPhaseArgs
multi :: FileLogMultiF 'ConfigPhaseArgs
multi :: Maybe FileLogMultiSwitch
multi,
Maybe (StripControl 'StripControlFileLog)
ConfigPhaseF 'ConfigPhaseArgs (StripControl 'StripControlFileLog)
stripControl :: ConfigPhaseF 'ConfigPhaseArgs (StripControl 'StripControlFileLog)
stripControl :: Maybe (StripControl 'StripControlFileLog)
stripControl
}
fileLogParser :: Parser (Maybe (WithDisabled FilePathDefault))
fileLogParser :: Parser (Maybe (WithDisabled FilePathDefault))
fileLogParser =
ReadM FilePathDefault
-> [Mod OptionFields (WithDisabled FilePathDefault)]
-> (Bool, [String])
-> Parser (Maybe (WithDisabled FilePathDefault))
forall a.
ReadM a
-> [Mod OptionFields (WithDisabled a)]
-> (Bool, [String])
-> Parser (Maybe (WithDisabled a))
Utils.mWithDisabledParser
(ReadM Text
forall s. IsString s => ReadM s
OA.str ReadM Text
-> (Text -> ReadM FilePathDefault) -> ReadM FilePathDefault
forall a b. ReadM a -> (a -> ReadM b) -> ReadM b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> ReadM FilePathDefault
forall (m :: Type -> Type).
MonadFail m =>
Text -> m FilePathDefault
FilePathDefault.parseFilePathDefault)
[Mod OptionFields (WithDisabled FilePathDefault)]
opts
(Bool
True, [String
"default", String
"PATH"])
where
opts :: [Mod OptionFields (WithDisabled FilePathDefault)]
opts =
[ String -> Mod OptionFields (WithDisabled FilePathDefault)
forall (f :: Type -> Type) a. HasName f => String -> Mod f a
OA.long String
"file-log",
[String] -> Mod OptionFields (WithDisabled FilePathDefault)
forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a
OA.completeWith [String
"default"],
Completer -> Mod OptionFields (WithDisabled FilePathDefault)
forall (f :: Type -> Type) a.
HasCompleter f =>
Completer -> Mod f a
OA.completer Completer
EOC.compgenCwdDirsCompleter,
Char -> Mod OptionFields (WithDisabled FilePathDefault)
forall (f :: Type -> Type) a. HasName f => Char -> Mod f a
OA.short Char
'f',
String -> Mod OptionFields (WithDisabled FilePathDefault)
forall (f :: Type -> Type) a. String -> Mod f a
Utils.mkHelp String
helpTxt
]
helpTxt :: String
helpTxt =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"If a path is supplied, all logs will additionally be written to ",
String
"the supplied file. Furthermore, 'command' logs will be written to ",
String
"the file irrespective of --console-log-command. Console logging is ",
String
"unaffected. This can be useful for investigating command failures. ",
String
"If the string 'default' is given, then we write to the XDG state ",
String
"directory e.g. ~/.local/state/shrun/shrun.log."
]
fileLogCommandNameTruncParser :: Parser (Maybe (WithDisabled (Truncation TruncCommandName)))
fileLogCommandNameTruncParser :: Parser (Maybe (WithDisabled (Truncation 'TruncCommandName)))
fileLogCommandNameTruncParser =
ReadM (Truncation 'TruncCommandName)
-> [Mod OptionFields (WithDisabled (Truncation 'TruncCommandName))]
-> (Bool, [String])
-> Parser (Maybe (WithDisabled (Truncation 'TruncCommandName)))
forall a.
ReadM a
-> [Mod OptionFields (WithDisabled a)]
-> (Bool, [String])
-> Parser (Maybe (WithDisabled a))
Utils.mWithDisabledParser
(ReadM Natural -> ReadM (Truncation 'TruncCommandName)
forall (m :: Type -> Type) (r :: TruncRegion).
MonadFail m =>
m Natural -> m (Truncation r)
Trunc.parseTruncation ReadM Natural
forall a. Read a => ReadM a
Utils.autoStripUnderscores)
[Mod OptionFields (WithDisabled (Truncation 'TruncCommandName))]
opts
(Bool
True, [String
"NATURAL"])
where
opts :: [Mod OptionFields (WithDisabled (Truncation 'TruncCommandName))]
opts =
[ String
-> Mod OptionFields (WithDisabled (Truncation 'TruncCommandName))
forall (f :: Type -> Type) a. HasName f => String -> Mod f a
OA.long String
"file-log-command-name-trunc",
String
-> Mod OptionFields (WithDisabled (Truncation 'TruncCommandName))
forall (f :: Type -> Type) a. String -> Mod f a
Utils.mkHelp String
helpTxt
]
helpTxt :: String
helpTxt = String
"Like --console-log-command-name-trunc, but for --file-logs."
deleteOnSuccessParser :: Parser (Maybe DeleteOnSuccessSwitch)
deleteOnSuccessParser :: Parser (Maybe DeleteOnSuccessSwitch)
deleteOnSuccessParser =
(Bool -> DeleteOnSuccessSwitch)
-> String -> String -> Parser (Maybe DeleteOnSuccessSwitch)
forall a. (Bool -> a) -> String -> String -> Parser (Maybe a)
Utils.switchParser Bool -> DeleteOnSuccessSwitch
MkDeleteOnSuccessSwitch String
"file-log-delete-on-success" String
helpTxt
where
helpTxt :: String
helpTxt =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"If --file-log is active, deletes the file on a successful exit. ",
String
"Does not delete the file if shrun exited via failure."
]
lineTruncParser :: Parser (Maybe (WithDisabled LineTruncation))
lineTruncParser :: Parser (Maybe (WithDisabled LineTruncation))
lineTruncParser =
ReadM LineTruncation
-> [Mod OptionFields (WithDisabled LineTruncation)]
-> (Bool, [String])
-> Parser (Maybe (WithDisabled LineTruncation))
forall a.
ReadM a
-> [Mod OptionFields (WithDisabled a)]
-> (Bool, [String])
-> Parser (Maybe (WithDisabled a))
Utils.mWithDisabledParser
(ReadM Natural -> ReadM Text -> ReadM LineTruncation
forall (m :: Type -> Type).
(Alternative m, MonadFail m) =>
m Natural -> m Text -> m LineTruncation
Trunc.parseLineTruncation ReadM Natural
forall a. Read a => ReadM a
Utils.autoStripUnderscores ReadM Text
forall s. IsString s => ReadM s
OA.str)
[Mod OptionFields (WithDisabled LineTruncation)]
opts
(Bool, [String])
forall a. IsString a => (Bool, [a])
Trunc.lineTruncMeta
where
opts :: [Mod OptionFields (WithDisabled LineTruncation)]
opts =
[ String -> Mod OptionFields (WithDisabled LineTruncation)
forall (f :: Type -> Type) a. HasName f => String -> Mod f a
OA.long String
"file-log-line-trunc",
[String] -> Mod OptionFields (WithDisabled LineTruncation)
forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a
OA.completeWith [String
"detect"],
String -> Mod OptionFields (WithDisabled LineTruncation)
forall (f :: Type -> Type) a. String -> Mod f a
Utils.mkHelp String
helpTxt
]
helpTxt :: String
helpTxt = String
"Like --console-log-line-trunc, but for --file-log. Defaults to 'off'."
multiParser :: Parser (Maybe FileLogMultiSwitch)
multiParser :: Parser (Maybe FileLogMultiSwitch)
multiParser =
(Bool -> FileLogMultiSwitch)
-> String -> String -> Parser (Maybe FileLogMultiSwitch)
forall a. (Bool -> a) -> String -> String -> Parser (Maybe a)
Utils.switchParser Bool -> FileLogMultiSwitch
MkFileLogMultiSwitch String
"file-log-multi" String
helpTxt
where
helpTxt :: String
helpTxt =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"Logs each command to its own file, rather than all logs being ",
String
"sent to the same file. This allows usage with ",
String
"'--command-log-read-strategy block-line-buffer'. The filename is ",
String
"based on --file-log. Note that --file-log-mode applies to the ",
String
"generated file. Only activates with multiple commands."
]
fileLogStripControlParser :: Parser (Maybe FileLogStripControl)
fileLogStripControlParser :: Parser (Maybe (StripControl 'StripControlFileLog))
fileLogStripControlParser = Parser (Maybe (StripControl 'StripControlFileLog))
mainParser
where
mainParser :: Parser (Maybe (StripControl 'StripControlFileLog))
mainParser =
Parser (StripControl 'StripControlFileLog)
-> Parser (Maybe (StripControl 'StripControlFileLog))
forall (f :: Type -> Type) a. Alternative f => f a -> f (Maybe a)
OA.optional
(Parser (StripControl 'StripControlFileLog)
-> Parser (Maybe (StripControl 'StripControlFileLog)))
-> Parser (StripControl 'StripControlFileLog)
-> Parser (Maybe (StripControl 'StripControlFileLog))
forall a b. (a -> b) -> a -> b
$ ReadM (StripControl 'StripControlFileLog)
-> Mod OptionFields (StripControl 'StripControlFileLog)
-> Parser (StripControl 'StripControlFileLog)
forall a. ReadM a -> Mod OptionFields a -> Parser a
OA.option
(ReadM Text -> ReadM (StripControl 'StripControlFileLog)
forall (m :: Type -> Type) (t :: StripControlType).
MonadFail m =>
m Text -> m (StripControl t)
StripControl.parseStripControl ReadM Text
forall s. IsString s => ReadM s
OA.str)
( [Mod OptionFields (StripControl 'StripControlFileLog)]
-> Mod OptionFields (StripControl 'StripControlFileLog)
forall a. Monoid a => [a] -> a
mconcat
[ String -> Mod OptionFields (StripControl 'StripControlFileLog)
forall (f :: Type -> Type) a. HasName f => String -> Mod f a
OA.long String
"file-log-strip-control",
[String] -> Mod OptionFields (StripControl 'StripControlFileLog)
forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a
OA.completeWith [String
"all", String
"smart", String
"off"],
String -> Mod OptionFields (StripControl 'StripControlFileLog)
forall (f :: Type -> Type) a. String -> Mod f a
Utils.mkHelpNoLine String
helpTxt,
String -> Mod OptionFields (StripControl 'StripControlFileLog)
forall (f :: Type -> Type) a. HasMetavar f => String -> Mod f a
OA.metavar ((Bool, [String]) -> String
forall a. (IsString a, Monoid a) => (Bool, [a]) -> a
ShrunUtils.mkMetaStr (Bool, [String])
forall a. IsString a => (Bool, [a])
StripControl.stripControlMeta)
]
)
helpTxt :: String
helpTxt =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"--console-log-strip-control for file logs created with --file-log. ",
String
"Defaults to all."
]
fileLogModeParser :: Parser (Maybe FileMode)
fileLogModeParser :: Parser (Maybe FileMode)
fileLogModeParser = Parser (Maybe FileMode)
mainParser
where
mainParser :: Parser (Maybe FileMode)
mainParser =
Parser FileMode -> Parser (Maybe FileMode)
forall (f :: Type -> Type) a. Alternative f => f a -> f (Maybe a)
OA.optional
(Parser FileMode -> Parser (Maybe FileMode))
-> Parser FileMode -> Parser (Maybe FileMode)
forall a b. (a -> b) -> a -> b
$ ReadM FileMode -> Mod OptionFields FileMode -> Parser FileMode
forall a. ReadM a -> Mod OptionFields a -> Parser a
OA.option
(ReadM Text -> ReadM FileMode
forall (m :: Type -> Type). MonadFail m => m Text -> m FileMode
FileMode.parseFileMode ReadM Text
forall s. IsString s => ReadM s
OA.str)
( [Mod OptionFields FileMode] -> Mod OptionFields FileMode
forall a. Monoid a => [a] -> a
mconcat
[ String -> Mod OptionFields FileMode
forall (f :: Type -> Type) a. HasName f => String -> Mod f a
OA.long String
"file-log-mode",
[String] -> Mod OptionFields FileMode
forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a
OA.completeWith [String
"append", String
"rename", String
"write"],
String -> Mod OptionFields FileMode
forall (f :: Type -> Type) a. String -> Mod f a
Utils.mkHelp String
helpTxt,
String -> Mod OptionFields FileMode
forall (f :: Type -> Type) a. HasMetavar f => String -> Mod f a
OA.metavar ((Bool, [String]) -> String
forall a. (IsString a, Monoid a) => (Bool, [a]) -> a
ShrunUtils.mkMetaStr (Bool, [String])
forall a. IsString a => (Bool, [a])
FileMode.fileModeMeta)
]
)
helpTxt :: String
helpTxt =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"Mode in which to open the log file. Defaults to 'write'. The 'rename' ",
String
"option will rename the requested log file if there is a collision ",
String
"e.g. '-f shrun.log' will become 'shrun (1).log'."
]
fileLogSizeModeParser :: Parser (Maybe FileSizeMode)
fileLogSizeModeParser :: Parser (Maybe FileSizeMode)
fileLogSizeModeParser = Parser (Maybe FileSizeMode)
mainParser
where
mainParser :: Parser (Maybe FileSizeMode)
mainParser =
Parser FileSizeMode -> Parser (Maybe FileSizeMode)
forall (f :: Type -> Type) a. Alternative f => f a -> f (Maybe a)
OA.optional
(Parser FileSizeMode -> Parser (Maybe FileSizeMode))
-> Parser FileSizeMode -> Parser (Maybe FileSizeMode)
forall a b. (a -> b) -> a -> b
$ ReadM FileSizeMode
-> Mod OptionFields FileSizeMode -> Parser FileSizeMode
forall a. ReadM a -> Mod OptionFields a -> Parser a
OA.option
(ReadM Text -> ReadM FileSizeMode
forall (m :: Type -> Type). MonadFail m => m Text -> m FileSizeMode
FileSizeMode.parseFileSizeMode ReadM Text
forall s. IsString s => ReadM s
OA.str)
( [Mod OptionFields FileSizeMode] -> Mod OptionFields FileSizeMode
forall a. Monoid a => [a] -> a
mconcat
[ String -> Mod OptionFields FileSizeMode
forall (f :: Type -> Type) a. HasName f => String -> Mod f a
OA.long String
"file-log-size-mode",
[String] -> Mod OptionFields FileSizeMode
forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a
OA.completeWith [String
"warn", String
"delete"],
String -> Mod OptionFields FileSizeMode
forall (f :: Type -> Type) a. String -> Mod f a
Utils.mkHelp String
helpTxt,
String -> Mod OptionFields FileSizeMode
forall (f :: Type -> Type) a. HasMetavar f => String -> Mod f a
OA.metavar ((Bool, [String]) -> String
forall a. (IsString a, Monoid a) => (Bool, [a]) -> a
ShrunUtils.mkMetaStr (Bool, [String])
forall a. IsString a => (Bool, [a])
FileSizeMode.fileSizeModeMeta)
]
)
helpTxt :: String
helpTxt =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"Sets a threshold for the file log size, upon which we either ",
String
"print a warning or delete the file, if it is exceeded. ",
String
"The BYTES should include the value and units e.g. ",
String
"'warn 10 mb', 'warn 5 gigabytes', 'delete 20.5B'. Defaults to warning ",
String
"at 50 mb."
]