summaryrefslogtreecommitdiff
path: root/src/Conftrack/Source/Yaml.hs
diff options
context:
space:
mode:
authorstuebinm2024-06-06 20:16:09 +0200
committerstuebinm2024-06-06 20:24:15 +0200
commite3cafcd9a1fc8621fdaaf33d1772bdcd8390d2bf (patch)
tree1a8c593c18c4e0477dd8277da6240a4eceecbc00 /src/Conftrack/Source/Yaml.hs
parent4321bb0b5b90c0f92217ccd07a67f17fce44b388 (diff)
add yaml source
Diffstat (limited to 'src/Conftrack/Source/Yaml.hs')
-rw-r--r--src/Conftrack/Source/Yaml.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Conftrack/Source/Yaml.hs b/src/Conftrack/Source/Yaml.hs
new file mode 100644
index 0000000..6adc798
--- /dev/null
+++ b/src/Conftrack/Source/Yaml.hs
@@ -0,0 +1,42 @@
+{-# LANGUAGE DerivingStrategies #-}
+{-# LANGUAGE QuantifiedConstraints #-}
+{-# LANGUAGE ImpredicativeTypes #-}
+{-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE AllowAmbiguousTypes #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE UndecidableInstances #-}
+
+module Conftrack.Source.Yaml (YamlSource(..), mkYamlSource, mkYamlSourceWith, mkYamlFileSource) where
+
+import Conftrack.Source (SomeSource(..), ConfigSource (..))
+import Conftrack.Source.Aeson
+
+import Prelude hiding (readFile)
+import qualified Data.Aeson as A
+import qualified Data.Yaml as Y
+import Data.Text (Text)
+import qualified Data.Aeson.Text as A
+import qualified Data.Text.Lazy as LT
+import Data.Functor ((<&>))
+import System.OsPath (OsPath)
+import qualified System.OsPath as OS
+import System.File.OsPath (readFile)
+import qualified Data.ByteString as BS
+
+newtype YamlSource = YamlSource JsonSource
+ deriving newtype (ConfigSource, Show)
+
+mkYamlSource :: A.Value -> SomeSource
+mkYamlSource value = mkYamlSourceWith ("Yaml string " <> LT.toStrict (A.encodeToLazyText value)) value
+
+mkYamlSourceWith :: Text -> A.Value -> SomeSource
+mkYamlSourceWith description value = SomeSource (source, [])
+ where source = YamlSource (JsonSource value description)
+
+mkYamlFileSource :: OsPath -> IO (Either Y.ParseException SomeSource)
+mkYamlFileSource path = do
+ bytes <- readFile path <&> BS.toStrict
+ pathAsText <- OS.decodeUtf path <&> LT.toStrict . LT.pack
+ pure $ Y.decodeEither' bytes
+ <&> mkYamlSourceWith ("YAML file " <> pathAsText)
+