module System.LXD.Client.Config (
defaultFile
, parseDefaultFile
, parseFile
, Config(..)
, Remote(..)
) where
import Control.Exception (throwIO)
import Data.Aeson
import Data.Map.Strict (Map)
import Data.Text (Text)
import qualified Data.Yaml as Yaml
import System.Directory (getHomeDirectory)
import System.FilePath ((</>))
defaultFile :: IO FilePath
defaultFile = do
h <- getHomeDirectory
return $ h </> ".config" </> "lxc" </> "config.yml"
parseDefaultFile :: IO Config
parseDefaultFile = defaultFile >>= parseFile
parseFile :: FilePath -> IO Config
parseFile f = do
r <- Yaml.decodeFileEither f
case r of
Left exc -> throwIO exc
Right v -> return v
data Config = Config {
configDefaultRemote :: Text
, configRemotes :: Map Text Remote
, configAliases :: Map Text Text
} deriving (Eq, Show)
instance FromJSON Config where
parseJSON = withObject "Config" $ \v -> do
configDefaultRemote <- v .: "default-remote"
configRemotes <- v .: "remotes"
configAliases <- v .: "aliases"
return Config{..}
instance ToJSON Config where
toJSON Config{..} = object [
"default-remote" .= configDefaultRemote
, "remotes" .= configRemotes
, "aliases" .= configAliases
]
data Remote = Remote {
remoteAddr :: Text
, remotePublic :: Bool
, remoteProtocol :: Maybe Text
} deriving (Eq, Show)
instance FromJSON Remote where
parseJSON = withObject "Remote" $ \v -> do
remoteAddr <- v .: "addr"
remotePublic <- v .: "public"
remoteProtocol <- v .:? "protocol"
return Remote{..}
instance ToJSON Remote where
toJSON Remote{..} = object [
"addr" .= remoteAddr
, "public" .= remotePublic
, "protocol" .= remoteProtocol
]