Add fenv starter

This commit is contained in:
Garrett Mills 2021-09-30 13:39:47 -05:00
parent cc5a1154be
commit 4df7458362

53
fenv.hs Normal file
View File

@ -0,0 +1,53 @@
import System.Environment
import System.Directory
import System.IO
import Data.List
type EnvKey = String
type EnvValue = String
data EnvFile =
End
| Entry EnvKey EnvValue EnvFile
deriving (Show,Eq,Read)
main = do
let f = (Entry "foo" "bar" End)
putStrLn (show f)
setEntry :: EnvFile -> EnvKey -> EnvValue -> EnvFile
setEntry env key value = let env'=(removeEntry env key) in
(Entry key value env')
removeEntry :: EnvFile -> EnvKey -> EnvFile
removeEntry End _ = End
removeEntry (Entry entryKey entryValue rest) key = if entryKey==key
then rest
else (Entry entryKey entryValue (removeEntry rest key))
getEntry :: EnvFile -> EnvKey -> Maybe EnvValue
getEntry End _ = Nothing
getEntry (Entry entryKey entryValue rest) key = if entryKey==key
then return entryValue
else (getEntry rest key)
replaceEnvFile :: EnvFile -> IO ()
replaceEnvFile env = do
path <- getEnvFilePath
(tempName, tempHandle) <- openTempFile "/tmp" ".fenv"
hPutStr tempHandle (show env)
hClose tempHandle
removeFile path
renameFile tempName path
getEnvFile :: IO EnvFile
getEnvFile = do
path <- getEnvFilePath
contents <- readFile path
return $ read contents
getEnvFilePath :: IO FilePath
getEnvFilePath = do
home <- getHomeDirectory
return (home ++ "/.fenv")