mirror of
https://code.naskya.net/repos/ndqEd
synced 2025-01-11 06:16:46 +09:00
47 lines
1.4 KiB
Haskell
47 lines
1.4 KiB
Haskell
|
{- This file is part of Vervis.
|
||
|
-
|
||
|
- Written in 2019 by fr33domlover <fr33domlover@riseup.net>.
|
||
|
-
|
||
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
||
|
-
|
||
|
- The author(s) have dedicated all copyright and related and neighboring
|
||
|
- rights to this software to the public domain worldwide. This software is
|
||
|
- distributed without any warranty.
|
||
|
-
|
||
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
||
|
- with this software. If not, see
|
||
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||
|
-}
|
||
|
|
||
|
module Data.KeyFile
|
||
|
( KeyFile (..)
|
||
|
, loadKeyFile
|
||
|
)
|
||
|
where
|
||
|
|
||
|
import Prelude
|
||
|
|
||
|
import Data.ByteString (ByteString)
|
||
|
import System.Directory (doesFileExist)
|
||
|
|
||
|
import qualified Data.ByteString as B (readFile, writeFile)
|
||
|
|
||
|
class KeyFile a where
|
||
|
generateKey :: IO a
|
||
|
parseKey :: ByteString -> IO a
|
||
|
renderKey :: a -> ByteString
|
||
|
|
||
|
loadKeyFile :: KeyFile a => Bool -> FilePath -> IO a
|
||
|
loadKeyFile setup path = do
|
||
|
e <- doesFileExist path
|
||
|
if e
|
||
|
then if setup
|
||
|
then fail $ "loadKeyFile: Initial setup but file already exists: " ++ path
|
||
|
else parseKey =<< B.readFile path
|
||
|
else if setup
|
||
|
then do
|
||
|
k <- generateKey
|
||
|
B.writeFile path $ renderKey k
|
||
|
return k
|
||
|
else fail $ "loadKeyFile: File not found: " ++ path
|