module B9.Artifact.Content.Readable where
import Control.Parallel.Strategies
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
import B9.Artifact.Content
import B9.Artifact.Content.AST
import B9.Artifact.Content.CloudConfigYaml
import B9.Artifact.Content.ErlangPropList
import B9.Artifact.Content.StringTemplate
import B9.Artifact.Content.YamlObject
import B9.B9Logging
import B9.QCUtil
import Control.Monad.IO.Class
import Data.Binary as Binary
import qualified Data.ByteString as Strict
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Lazy.Char8 as Lazy
import Data.Data
import GHC.Generics (Generic)
import System.Exit
import System.Process
import Test.QuickCheck
data Content
= RenderErlang (AST Content ErlangPropList)
| RenderYamlObject (AST Content YamlObject)
| RenderCloudConfig (AST Content CloudConfigYaml)
| FromByteString Lazy.ByteString
| FromString String
| FromTextFile SourceFile
| RenderBase64BinaryFile FilePath
| RenderBase64Binary Lazy.ByteString
| FromURL String
deriving (Read, Show, Typeable, Eq, Data, Generic)
instance NFData Content
instance Arbitrary Content where
arbitrary =
oneof
[ FromTextFile <$> smaller arbitrary
, RenderBase64BinaryFile <$> smaller arbitrary
, RenderErlang <$> smaller arbitrary
, RenderYamlObject <$> smaller arbitrary
, RenderCloudConfig <$> smaller arbitrary
, FromString <$> smaller arbitrary
, FromByteString . Lazy.pack <$> smaller arbitrary
, RenderBase64Binary . Lazy.pack <$> smaller arbitrary
, FromURL <$> smaller arbitrary
]
instance ToContentGenerator Content Lazy.ByteString where
toContentGenerator (RenderErlang ast) = Binary.encode <$> fromAST ast
toContentGenerator (RenderYamlObject ast) = Binary.encode <$> fromAST ast
toContentGenerator (RenderCloudConfig ast) = Binary.encode <$> fromAST ast
toContentGenerator (FromTextFile s) = readTemplateFile s
toContentGenerator (RenderBase64BinaryFile s) = readBinaryFileAsBase64 s
where
readBinaryFileAsBase64 :: MonadIO m => FilePath -> m Lazy.ByteString
readBinaryFileAsBase64 f = Lazy.fromStrict . B64.encode <$> liftIO (Strict.readFile f)
toContentGenerator (RenderBase64Binary b) = pure (Lazy.fromStrict $ B64.encode $ Lazy.toStrict b)
toContentGenerator (FromString str) = pure (Lazy.pack str)
toContentGenerator (FromByteString str) = pure str
toContentGenerator (FromURL url) = do
dbgL ("Downloading: " ++ url)
(exitCode, out, err) <- liftIO (readProcessWithExitCode "curl" [url] "")
if exitCode == ExitSuccess
then do
dbgL ("Download finished. Bytes read: " ++ show (length out))
traceL ("Downloaded (truncated to first 4K): \n\n" ++ take 4096 out ++ "\n\n")
pure (Lazy.pack out)
else do
errorL ("Download failed: " ++ err)
liftIO (exitWith exitCode)