module Handler.Raw (getRawCommandR, postRawCommandR) where
import Import
import qualified Data.Text as T
rawForm :: AForm (HandlerT App IO) String
rawForm = T.unpack <$> areq textField cmd Nothing
where cmd = FieldSettings (SomeMessage MsgRaw) Nothing (Just "Command") Nothing []
getRawCommandR :: Handler Html
getRawCommandR = defaultLayout $ do
setTitleI MsgRaw
((result,widget),enctype) <- liftHandlerT $ runFormPost $ renderBootstrap rawForm
[whamlet|
<p>_{MsgRawWarning}
<form method=post enctype=#{enctype} action=@{RawCommandR}>
<fieldset>
$case result
$of FormFailure reasons
$forall r <- reasons
<div .alert .alerterror>#{r}
$of _
^{widget}
<input .btn .primary type=submit value=_{MsgRunCmd}>
|]
postRawCommandR :: Handler Html
postRawCommandR = do
((result,_),_) <- runFormPost $ renderBootstrap rawForm
command <- case result of
FormMissing -> invalidArgs ["Form is missing"]
FormFailure msg -> invalidArgs msg
FormSuccess command -> return command
(code,out,err) <- notmuchRaw (words command)
defaultLayout $ do
setTitleI MsgRaw
[whamlet|
<h1>#{command}
<p>_{MsgExitCode}: #{show code}
<h3>_{MsgStdOut}
<pre>
#{out}
<h3>_{MsgStdErr}
<pre>
#{err}
|]