-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
244 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
-- Copyright (C) 2018 Red Hat, Inc. | ||
-- | ||
-- This file is part of bdcs-api. | ||
-- | ||
-- bdcs-api is free software: you can redistribute it and/or modify | ||
-- it under the terms of the GNU General Public License as published by | ||
-- the Free Software Foundation, either version 3 of the License, or | ||
-- (at your option) any later version. | ||
-- | ||
-- bdcs-api is distributed in the hope that it will be useful, | ||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
-- GNU General Public License for more details. | ||
-- | ||
-- You should have received a copy of the GNU General Public License | ||
-- along with bdcs-api. If not, see <http://www.gnu.org/licenses/>. | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module BDCS.API.ComposeConfig( | ||
ComposeConfig(..), | ||
|
||
parseComposeConfig, | ||
composeConfigTOML | ||
) where | ||
|
||
import Data.Aeson | ||
import qualified Data.Text as T | ||
import Text.Printf(printf) | ||
import Text.Toml(parseTomlDoc) | ||
|
||
|
||
-- | Information about the compose configuration not available in other results files | ||
data ComposeConfig = ComposeConfig { | ||
ccCommit :: T.Text, -- ^ Commit hash for Blueprint | ||
ccExportType :: T.Text -- ^ Export type | ||
} deriving (Show, Eq) | ||
|
||
instance ToJSON ComposeConfig where | ||
toJSON ComposeConfig{..} = object | ||
[ "commit" .= ccCommit | ||
, "export_type" .= ccExportType | ||
] | ||
|
||
instance FromJSON ComposeConfig where | ||
parseJSON = withObject "Compose configuration data" $ \o -> do | ||
ccCommit <- o .: "commit" | ||
ccExportType <- o .: "export_type" | ||
return ComposeConfig{..} | ||
|
||
-- | Parse a TOML formatted compose config string and return a ComposeConfig | ||
-- | ||
-- If there is an error the details will be returned in the Left | ||
parseComposeConfig :: T.Text -> Either String ComposeConfig | ||
parseComposeConfig xs = | ||
case parseTomlDoc "" xs of | ||
Left err -> Left ("Parsing TOML document failed. " ++ show err) | ||
Right table -> do | ||
let jsonValue = toJSON table | ||
case (fromJSON jsonValue :: Result ComposeConfig) of | ||
Error err -> Left ("Converting from JSON to ComposeConfig failed. " ++ show err) | ||
Success r -> Right r | ||
|
||
-- | Return a TOML string from a ComposeConfig record | ||
composeConfigTOML :: ComposeConfig -> T.Text | ||
composeConfigTOML ComposeConfig{..} = T.concat [commitText, exportTypeText] | ||
where | ||
commitText = T.pack $ printf "commit = \"%s\"\n" ccCommit | ||
exportTypeText = T.pack $ printf "export_type = \"%s\"\n" ccExportType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.