-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
4 changed files
with
93 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
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 |
---|---|---|
@@ -1,18 +1,34 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module DConf2Nix where | ||
|
||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
import DConf.Data | ||
import DConf ( dconfParser ) | ||
import qualified Nix | ||
import Text.Parsec.Text ( parseFromFile ) | ||
import Text.Parsec ( ParseError | ||
, runParser | ||
) | ||
|
||
dconf2nixFile :: InputFilePath -> OutputFilePath -> Verbosity -> IO () | ||
dconf2nixFile (InputFilePath input) (OutputFilePath output) v = do | ||
parsed <- parseFromFile (dconfParser v) input | ||
handler (T.writeFile output) (T.appendFile output) parsed | ||
|
||
dconf2nixStdin :: Verbosity -> IO () | ||
dconf2nixStdin v = do | ||
input <- T.getContents | ||
handler T.putStr T.putStr $ runParser (dconfParser v) () "<stdin>" input | ||
|
||
dconf2nix :: InputFilePath -> OutputFilePath -> Verbosity -> IO () | ||
dconf2nix (InputFilePath input) (OutputFilePath output) v = do | ||
parseFromFile (dconfParser v) input >>= \case | ||
Left err -> error (show err) | ||
handler | ||
:: (T.Text -> IO ()) | ||
-> (T.Text -> IO ()) | ||
-> Either ParseError [Entry] | ||
-> IO () | ||
handler writer appender parsed = do | ||
case parsed of | ||
Left err -> error $ show err | ||
Right xs -> do | ||
T.writeFile output Nix.renderHeader | ||
traverse (\e -> T.appendFile output (unNix $ Nix.renderEntry e)) xs | ||
T.appendFile output Nix.renderFooter | ||
writer Nix.renderHeader | ||
traverse (\e -> appender (unNix $ Nix.renderEntry e)) xs | ||
appender Nix.renderFooter |