Add --userPasswordFile option to migration utility

This commit is contained in:
Berk Ozkutuk 2022-04-02 00:17:25 +03:00
parent b2b19cfaff
commit 59956c6b59
3 changed files with 39 additions and 4 deletions

View file

@ -12,12 +12,42 @@ import ClassyPrelude
import Lens.Micro
import Options.Generic
import qualified Options.Applicative as OA
import qualified Data.Text as T
data Password
= PasswordText Text
| PasswordFile FilePath
deriving (Show, Read)
parsePassword :: OA.Parser Password
parsePassword = passwordText <|> passwordFile
where
passwordText = PasswordText <$> OA.strOption
( OA.long "userPassword"
<> OA.metavar "PASSWORD"
<> OA.help "Password in plain-text"
)
passwordFile = PasswordFile <$> OA.strOption
( OA.long "userPasswordFile"
<> OA.metavar "FILE"
<> OA.help "Password file"
)
instance ParseFields Password
instance ParseRecord Password where
parseRecord = fmap getOnly parseRecord
instance ParseField Password where
parseField _ _ _ _ = parsePassword
data MigrationOpts
= CreateDB { conn :: Text }
| CreateUser { conn :: Text
, userName :: Text
, userPassword :: Text
, userPassword :: Password
, privateDefault :: Maybe Bool
, archiveDefault :: Maybe Bool
, privacyLock :: Maybe Bool }
@ -54,7 +84,10 @@ main = do
CreateUser{..} ->
P.runSqlite conn $ do
hash' <- liftIO (hashPassword userPassword)
passwordText <- liftIO . fmap T.strip $ case userPassword of
PasswordText s -> pure s
PasswordFile f -> readFileUtf8 f
hash' <- liftIO (hashPassword passwordText)
void $ P.upsertBy
(UniqueUserName userName)
(User userName hash' Nothing False False False)

View file

@ -1,10 +1,10 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.4.
-- This file has been generated from package.yaml by hpack version 0.34.6.
--
-- see: https://github.com/sol/hpack
--
-- hash: 6176e4be5a9b09fa50173b5bb2f280e5d8bc4c70374c91f1eac67ab03a49426f
-- hash: 2d724440a0ea54ad278837400547fc97cde79d5998c51d59b4882db32eeb5372
name: espial
version: 0.0.9
@ -412,6 +412,7 @@ executable migration
, microlens
, monad-logger ==0.3.*
, mtl
, optparse-applicative
, optparse-generic >=1.2.3
, parser-combinators
, persistent >=2.8 && <2.14

View file

@ -185,6 +185,7 @@ executables:
dependencies:
- espial
- optparse-generic >= 1.2.3
- optparse-applicative
# Test suite
tests: