Skip to content

Commit

Permalink
refactor(app): Update Main to use GenPass
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed Mar 8, 2024
1 parent 22f53bd commit 4a0ae98
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Main (main) where

import Data.Elocrypt hiding (genPassword, genPasswords)
import Data.Gibberish.Format qualified as Fmt
import Data.Gibberish.GenPass (genPassword)
import Data.Gibberish.GenPass (genPassphrase, genPassword)
import Data.Gibberish.MonadPass (Pass (), usingPass)
import Data.Gibberish.Trigraph (Language (..), loadTrigraph)
import Data.Gibberish.Types (GenPasswordOpts (..))
import Data.Gibberish.Types (GenPassphraseOpts (..), GenPasswordOpts (..))

import Control.Monad.IO.Class (MonadIO (..))
import Data.Maybe (fromMaybe)
import Data.Text (Text ())
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
Expand Down Expand Up @@ -94,29 +92,39 @@ passwords (CommonOpts {..}) (WordOpts {..}) gen = do
optExactWords = Fmt.ExactNumberWords <$> optNumber
}

pure $
fst $
usingPass gen (genPasswords genOpts formatOpts)
pure $ fst $ usingPass gen (genPasswords genOpts formatOpts)

genPasswords :: RandomGen gen => GenPasswordOpts -> Fmt.FormatOpts -> Pass gen Text
genPasswords genOpts formatOpts = do
res <- sequence $ repeat (genPassword genOpts)
pure (Fmt.formatWords formatOpts res)

passphrases :: RandomGen gen => CommonOpts -> PhraseOpts -> gen -> IO Text
passphrases opts@(CommonOpts {..}) (PhraseOpts {..}) gen =
pure $ Text.intercalate " " $ map Text.pack passphrases'
where
passphrases' = newPassphrase num optMinLength optMaxLength (getGenOptions opts) gen
num = fromMaybe termLen optNumber

getGenOptions :: CommonOpts -> GenOptions
getGenOptions CommonOpts {..} =
genOptions
{ genCapitals = optCapitals,
genDigits = optDigits,
genSpecials = optSpecials
}
passphrases (CommonOpts {..}) (PhraseOpts {..}) gen = do
trigraph <- liftIO $ loadTrigraph English

let genOpts =
GenPassphraseOpts
{ poptsCapitals = optCapitals,
poptsDigits = optDigits,
poptsSpecials = optSpecials,
poptsTrigraph = trigraph,
poptsMinLength = optMinLength,
poptsMaxLength = optMaxLength
}
formatOpts =
Fmt.FormatOpts
{ optMaxLen = Fmt.MaxLen termLen,
optMaxHeight = Fmt.MaxHeight termHeight,
optSeparator = Fmt.Separator " ",
optExactWords = Fmt.ExactNumberWords <$> optNumber
}

let (res, _) = usingPass gen $ do
words' <- genPassphrase genOpts
pure (Fmt.formatWords formatOpts words')

pure res

execParser' :: ParserInfo a -> IO a
execParser' info' =
Expand Down

0 comments on commit 4a0ae98

Please sign in to comment.