Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor argument handling #103

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ Type `--help` for some more information.
```shell
dconf2nix - Nixify dconf configuration files

Usage: dconf2nix [-v|--version]
[[-r|--root ARG] [--verbose] | (-i|--input ARG)
(-o|--output ARG) [-r|--root ARG] [--verbose]]
Usage: dconf2nix [-v|--version] [-r|--root ARG] [--verbose]
[(-i|--input ARG) (-o|--output ARG)]

Convert a dconf file into a Nix file, as expected by Home Manager.

Available options:
Expand All @@ -117,8 +117,6 @@ Available options:
--verbose Verbose mode (debug)
-i,--input ARG Path to the dconf file (input)
-o,--output ARG Path to the Nix output file (to be created)
-r,--root ARG Custom root path. e.g.: system/locale/
--verbose Verbose mode (debug)
```

#### Custom root
Expand Down
8 changes: 4 additions & 4 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Main where

import CommandLine ( FileArgs(..)
import CommandLine ( Args(..)
, FileArgs(..)
, Input(..)
, StdinArgs(..)
, runArgs
)
import DConf2Nix ( dconf2nixFile
Expand All @@ -13,7 +13,7 @@ import DConf2Nix ( dconf2nixFile

main :: IO ()
main = runArgs >>= \case
FileInput (FileArgs i o r v) ->
Args r v (FileInput (FileArgs i o)) ->
dconf2nixFile i o r v
StdinInput (StdinArgs r v) ->
Args r v StdinInput ->
dconf2nixStdin r v
30 changes: 14 additions & 16 deletions src/CommandLine.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CommandLine
( FileArgs(..)
( Args(..)
, FileArgs(..)
, Input(..)
, StdinArgs(..)
, runArgs
)
where
Expand All @@ -12,18 +12,17 @@ import DConf.Data
import Options.Applicative
import Paths_dconf2nix ( version )

data Input = FileInput FileArgs | StdinInput StdinArgs
data Args = Args
{ argsRoot :: Root
, argsVerbosity :: Verbosity
, argsInput :: Input
}

data Input = FileInput FileArgs | StdinInput

data FileArgs = FileArgs
{ fileInput :: InputFilePath
, fileOutput :: OutputFilePath
, fileRoot :: Root
, fileVerbosity :: Verbosity
}

data StdinArgs = StdinArgs
{ stdinRoot :: Root
, stdinVerbosity :: Verbosity
}

verbosityArgs :: Parser Verbosity
Expand All @@ -46,12 +45,9 @@ fileArgs = fmap FileInput $ FileArgs
"Path to the Nix output file (to be created)"
)
)
<*> rootArgs
<*> verbosityArgs

stdinArgs :: Parser Input
stdinArgs =
StdinInput <$> (StdinArgs <$> rootArgs <*> verbosityArgs)
stdinArgs = pure StdinInput

versionInfo :: String
versionInfo = unlines
Expand All @@ -72,11 +68,13 @@ versionOpt :: Parser (a -> a)
versionOpt = infoOption versionInfo
(long "version" <> short 'v' <> help "Show the current version")

runArgs :: IO Input
runArgs :: IO Args
runArgs =
let
args = Args <$> rootArgs <*> verbosityArgs <*> (stdinArgs <|> fileArgs)

opts = info
(helper <*> versionOpt <*> (stdinArgs <|> fileArgs))
(helper <*> versionOpt <*> args)
( fullDesc
<> progDesc
"Convert a dconf file into a Nix file, as expected by Home Manager."
Expand Down