Add --userPasswordFile option to migration utility
This commit is contained in:
parent
8fc2433d5d
commit
c98946ac99
|
@ -12,12 +12,42 @@ import ClassyPrelude
|
||||||
import Lens.Micro
|
import Lens.Micro
|
||||||
|
|
||||||
import Options.Generic
|
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
|
data MigrationOpts
|
||||||
= CreateDB { conn :: Text }
|
= CreateDB { conn :: Text }
|
||||||
| CreateUser { conn :: Text
|
| CreateUser { conn :: Text
|
||||||
, userName :: Text
|
, userName :: Text
|
||||||
, userPassword :: Text
|
, userPassword :: Password
|
||||||
, privateDefault :: Maybe Bool
|
, privateDefault :: Maybe Bool
|
||||||
, archiveDefault :: Maybe Bool
|
, archiveDefault :: Maybe Bool
|
||||||
, privacyLock :: Maybe Bool }
|
, privacyLock :: Maybe Bool }
|
||||||
|
@ -54,7 +84,10 @@ main = do
|
||||||
|
|
||||||
CreateUser{..} ->
|
CreateUser{..} ->
|
||||||
P.runSqlite conn $ do
|
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
|
void $ P.upsertBy
|
||||||
(UniqueUserName userName)
|
(UniqueUserName userName)
|
||||||
(User userName hash' Nothing False False False)
|
(User userName hash' Nothing False False False)
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
cabal-version: 1.12
|
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
|
-- see: https://github.com/sol/hpack
|
||||||
--
|
--
|
||||||
-- hash: 6176e4be5a9b09fa50173b5bb2f280e5d8bc4c70374c91f1eac67ab03a49426f
|
-- hash: 2d724440a0ea54ad278837400547fc97cde79d5998c51d59b4882db32eeb5372
|
||||||
|
|
||||||
name: espial
|
name: espial
|
||||||
version: 0.0.9
|
version: 0.0.9
|
||||||
|
@ -412,6 +412,7 @@ executable migration
|
||||||
, microlens
|
, microlens
|
||||||
, monad-logger ==0.3.*
|
, monad-logger ==0.3.*
|
||||||
, mtl
|
, mtl
|
||||||
|
, optparse-applicative
|
||||||
, optparse-generic >=1.2.3
|
, optparse-generic >=1.2.3
|
||||||
, parser-combinators
|
, parser-combinators
|
||||||
, persistent >=2.8 && <2.14
|
, persistent >=2.8 && <2.14
|
||||||
|
|
|
@ -185,6 +185,7 @@ executables:
|
||||||
dependencies:
|
dependencies:
|
||||||
- espial
|
- espial
|
||||||
- optparse-generic >= 1.2.3
|
- optparse-generic >= 1.2.3
|
||||||
|
- optparse-applicative
|
||||||
|
|
||||||
# Test suite
|
# Test suite
|
||||||
tests:
|
tests:
|
||||||
|
|
Loading…
Reference in a new issue