module Dhall.Secret.IO where
import           Control.Lens       (set)
import qualified Data.Text          as T
import           Data.Version       (showVersion)
import           Data.Void          (Void, vacuous)
import           Dhall              (InputSettings, defaultInputSettings,
                                     evaluateSettings, inputExprWithSettings,
                                     substitutions)
import           Dhall.Core         (Directory (Directory), Expr (..),
                                     File (File), Import (Import),
                                     ImportHashed (ImportHashed),
                                     ImportMode (Code), ImportType (Remote),
                                     Scheme (HTTPS), URL (URL), freeIn,
                                     makeBinding, normalize, pretty)
import           Dhall.Freeze       (Intent (Secure), Scope (OnlyRemoteImports),
                                     freezeExpression)
import           Dhall.Import       (load)
import qualified Dhall.Map
import           Dhall.Secret.Type  (secretTypes)
import           Dhall.Src          (Src)
import qualified Paths_dhall_secret as P

version :: String
version :: String
version = Version -> String
showVersion Version
P.version

inputsetting :: InputSettings
inputsetting :: InputSettings
inputsetting = ASetter
  InputSettings
  InputSettings
  (Substitutions Src Void)
  (Substitutions Src Void)
-> Substitutions Src Void -> InputSettings -> InputSettings
forall s t a b. ASetter s t a b -> b -> s -> t
set (LensLike' Identity InputSettings EvaluateSettings
forall s (f :: * -> *).
(HasEvaluateSettings s, Functor f) =>
LensLike' f s EvaluateSettings
forall (f :: * -> *).
Functor f =>
LensLike' f InputSettings EvaluateSettings
evaluateSettings LensLike' Identity InputSettings EvaluateSettings
-> ((Substitutions Src Void -> Identity (Substitutions Src Void))
    -> EvaluateSettings -> Identity EvaluateSettings)
-> ASetter
     InputSettings
     InputSettings
     (Substitutions Src Void)
     (Substitutions Src Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Substitutions Src Void -> Identity (Substitutions Src Void))
-> EvaluateSettings -> Identity EvaluateSettings
forall (f :: * -> *) s.
(Functor f, HasEvaluateSettings s) =>
LensLike' f s (Substitutions Src Void)
substitutions ) ([(Text, Expr Src Void)] -> Substitutions Src Void
forall k v. Ord k => [(k, v)] -> Map k v
Dhall.Map.fromList ([(Text
"dhall-secret", Expr Src Void
secretTypes)])) InputSettings
defaultInputSettings

defineVar :: Expr Src Void -> Expr Src Import
defineVar :: Expr Src Void -> Expr Src Import
defineVar = Binding Src Import -> Expr Src Import -> Expr Src Import
forall s a. Binding s a -> Expr s a -> Expr s a
Let (Text -> Expr Src Import -> Binding Src Import
forall s a. Text -> Expr s a -> Binding s a
makeBinding Text
"dhall-secret" (Import -> Expr Src Import
forall s a. a -> Expr s a
Embed (ImportHashed -> ImportMode -> Import
Import (Maybe SHA256Digest -> ImportType -> ImportHashed
ImportHashed Maybe SHA256Digest
forall a. Maybe a
Nothing (URL -> ImportType
Remote (Scheme
-> Text -> File -> Maybe Text -> Maybe (Expr Src Import) -> URL
URL Scheme
HTTPS Text
"raw.githubusercontent.com" (Directory -> Text -> File
File ([Text] -> Directory
Directory ([Text] -> Directory) -> [Text] -> Directory
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. [a] -> [a]
reverse [Text
"jcouyang", Text
"dhall-secret", Text
tag]) Text
"Type.dhall") Maybe Text
forall a. Maybe a
Nothing Maybe (Expr Src Import)
forall a. Maybe a
Nothing))) ImportMode
Code))) (Expr Src Import -> Expr Src Import)
-> (Expr Src Void -> Expr Src Import)
-> Expr Src Void
-> Expr Src Import
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr Src Void -> Expr Src Import
forall (f :: * -> *) a. Functor f => f Void -> f a
vacuous
  where
    tag :: Text
tag = if String
version String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"0.1.0.0" then Text
"master" else Text
"v" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack String
version

addLetbinding :: Expr Src Void ->  IO (Expr Src Import)
addLetbinding :: Expr Src Void -> IO (Expr Src Import)
addLetbinding Expr Src Void
x
  | Var -> Expr Src Void -> Bool
forall a s. Eq a => Var -> Expr s a -> Bool
freeIn Var
"dhall-secret" Expr Src Void
x =  String
-> Scope -> Intent -> Expr Src Import -> IO (Expr Src Import)
forall s.
String -> Scope -> Intent -> Expr s Import -> IO (Expr s Import)
freezeExpression String
"." Scope
OnlyRemoteImports Intent
Secure (Expr Src Import -> IO (Expr Src Import))
-> Expr Src Import -> IO (Expr Src Import)
forall a b. (a -> b) -> a -> b
$ Expr Src Void -> Expr Src Import
defineVar Expr Src Void
x
  | Bool
otherwise = Expr Src Import -> IO (Expr Src Import)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Expr Src Import -> IO (Expr Src Import))
-> Expr Src Import -> IO (Expr Src Import)
forall a b. (a -> b) -> a -> b
$ Expr Src Void -> Expr Src Import
forall (f :: * -> *) a. Functor f => f Void -> f a
vacuous (Expr Src Void -> Expr Src Import)
-> Expr Src Void -> Expr Src Import
forall a b. (a -> b) -> a -> b
$ Expr Src Void -> Expr Src Void
forall a s t. Eq a => Expr s a -> Expr t a
normalize Expr Src Void
x

parseExpr :: T.Text -> IO (Expr Src Void)
parseExpr :: Text -> IO (Expr Src Void)
parseExpr Text
text = InputSettings -> Text -> IO (Expr Src Void)
inputExprWithSettings InputSettings
inputsetting Text
text IO (Expr Src Void)
-> (Expr Src Void -> IO (Expr Src Import)) -> IO (Expr Src Import)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Expr Src Void -> IO (Expr Src Import)
addLetbinding IO (Expr Src Import)
-> (Expr Src Import -> IO (Expr Src Void)) -> IO (Expr Src Void)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Expr Src Import -> IO (Expr Src Void)
load

prettyExpr :: Expr Src Void -> IO T.Text
prettyExpr :: Expr Src Void -> IO Text
prettyExpr =  (Expr Src Import -> Text) -> IO (Expr Src Import) -> IO Text
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Expr Src Import -> Text
forall a. Pretty a => a -> Text
pretty (IO (Expr Src Import) -> IO Text)
-> (Expr Src Void -> IO (Expr Src Import))
-> Expr Src Void
-> IO Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr Src Void -> IO (Expr Src Import)
addLetbinding